【问题标题】:checkbox how to implement - android recyclerview复选框如何实现 - android recyclerview
【发布时间】:2016-11-20 09:21:12
【问题描述】:

我想实现我的 onItemSelected,这样当用户单击复选框时,它将被标记为“已选中” 然后将列 DBContract.EntryColumns.IS_CHECKED 更新为已选中(将其设置为 1)。一切正常,但问题是我不知道如何 实现 onItemSelected 方法。请有人告诉我如何解决这个问题。

这是定义 CheckBox 的我的 Activity 和 list_item_book.xml。

public class MainActivity extends AppCompatActivity implements
        BookAdapter.OnItemClickListener,
        View.OnClickListener {

    private BookAdapter mAdapter;
    private CheckBox mCheckBox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mAdapter = new BookAdapter(null);
        mAdapter.setOnItemClickListener(this);

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        recyclerView.setAdapter(mAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

    }


    /* This method will be called when I Click on checkboxes */
    @Override
    public void onItemSelected(boolean active, int position) {

        //what should I write here?

        //  DBContract.EntryColumns.IS_CHECKED

    //

    }  
}

//list_item_book.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingEnd="@dimen/activity_horizontal_margin"
    android:paddingStart="@dimen/activity_horizontal_margin">

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
     />

</RelativeLayout>

【问题讨论】:

    标签: android checkbox android-recyclerview onitemclicklistener


    【解决方案1】:

    您可以将复选框映射到 ID 并将其存储到 Map。然后只需在复选框上设置监听器。 (假设checkbox变量有Checkbox类型)

    CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    // Get the ID here
                    int id = yourMapCheckboxToId.get(buttonView);
                    // ... do what you are going to do having ID
                }
            }
        };
    checkbox.setOnCheckedChangeListener(listener);
    

    【讨论】:

      猜你喜欢
      • 2018-05-10
      • 1970-01-01
      • 1970-01-01
      • 2017-12-02
      • 1970-01-01
      • 2017-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多