按下时,RecyclerView中的CheckBox不起作用

时间:2017-11-01 09:44:57

标签: android checkbox android-recyclerview

我有基本的RecyclerView项目,包含TextView和CheckBox。但是当我点击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);

        }
    }

}

2 个答案:

答案 0 :(得分:7)

我发现这里的一切正常。

package <your_package>;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import <your_package>.R;

import java.util.ArrayList;

public class TestActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.test_recyclerview);

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        ArrayList<String> data = new ArrayList<>();
        for(int i =0; i < 1000; i++) {
            data.add(String.valueOf(i));
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(new FilterTypeAdapter(getApplicationContext(), data));
    }

    public class FilterTypeAdapter extends RecyclerView.Adapter<MyViewHolder> {

    private ArrayList<String> mData;
    private HashMap<String, Boolean> mChecked;
    private Context mContext;

    public FilterTypeAdapter() {
    }

    public FilterTypeAdapter(Context mContext, ArrayList<String> mData) {
        this.mContext = mContext;
        this.mData = mData;
        this.mChecked = new HashMap<>();
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int i) {

        View v = TestActivity.this.getLayoutInflater().inflate(R.layout.test_recyclerview_item, parent, false);
        MyViewHolder vh = new MyViewHolder(v);

        return vh;

    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {

        holder.mText.setText(mData.get(position));
        holder.mCheckBox.setOnCheckedChangeListener(null);
        if(mChecked.containsKey(mData.get(position))) {
            holder.mCheckBox.setChecked(mChecked.get(mData.get(position)));
        }
        else {
            holder.mCheckBox.setChecked(false);
        }
        holder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mChecked.put(mData.get(holder.getAdapterPosition()), 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 AppCompatCheckBox mCheckBox;

        public MyViewHolder(View itemView) {
            super(itemView);

            mText = (TextView) itemView.findViewById(R.id.item_text);
            mCheckBox = (AppCompatCheckBox) itemView.findViewById(R.id.item_checkbox);

        }
    }
}

R.layout.test_recyclerview

<android.support.v7.widget.RecyclerView android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"
    android:layout_marginBottom="30dp"
    android:layout_marginEnd="10dp"
    android:layout_marginStart="10dp"
    android:layout_marginTop="10dp"

    xmlns:android="http://schemas.android.com/apk/res/android" />

R.layout.testrecyclerview_item

<?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">

    <TextView
        android:id="@+id/item_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        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"
        />

</LinearLayout>

答案 1 :(得分:-1)

从复选框小部件中删除这些行

 android:focusable="false"
    android:focusableInTouchMode="false"

我认为这可能会有所帮助