【发布时间】:2017-11-01 09:44:56
【问题描述】:
我有带有 TextView 和 CheckBox 的基本 RecyclerView 项目。但是当我单击 CheckBox 时,它不起作用。当我长按时,它会更神奇。
我做了一个研究,发现android:descendantFocusability="blocksDescendants",android:clickable="false",android:focusable="false",android:longClickable="false",但他们没有帮助我
这是我的 RecyclerView 项目布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants">
<TextView
android:id="@+id/item_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:textColor="#000000"/>
<android.support.v7.widget.AppCompatCheckBox
android:id="@+id/item_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
app:buttonTint="@color/colorAccent"
android:focusable="false"
android:focusableInTouchMode="false"/>
</LinearLayout>
这是我的适配器类
public class FilterTypeAdapter extends RecyclerView.Adapter<FilterTypeAdapter.MyViewHolder> {
private ArrayList<String> mData;
private Context mContext;
public FilterTypeAdapter() {
}
public FilterTypeAdapter(Context mContext, ArrayList<String> mData) {
this.mContext = mContext;
this.mData = mData;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int i) {
View v = LayoutInflater.from(mContext).inflate(R.layout.recycler_view_single_row, parent, false);
MyViewHolder vh = new MyViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.mText.setText(mData.get(position));
holder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(mContext, "Status is: " + isChecked + "", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return mData.size();
}
// View Holder
public class MyViewHolder extends RecyclerView.ViewHolder {
private TextView mText;
private CheckBox mCheckBox;
public MyViewHolder(View itemView) {
super(itemView);
mText = itemView.findViewById(R.id.item_text);
mCheckBox = itemView.findViewById(R.id.item_checkbox);
}
}
}
【问题讨论】:
-
您在哪里设置 onClick,如何设置?
-
请分享您的 onCheckedChangedListener 的代码
-
请分享您的适配器代码。
-
添加了我的适配器类
-
尝试在 MyViewHolder 类中的 mCheckBox 变量上设置 OnCheckedChanged 侦听器。在 onBind 调用中设置点击监听器被认为是不好的做法。
标签: android checkbox android-recyclerview