Recyclerview和CheckBox的完美

Recyclerview的

多选 单选 全选 去不选

mainactivity

public class MainActivity extends AppCompatActivity {
    private boolean isType = false;
    private List list = new ArrayList();
    private RecyclerviewAdapter recyclerviewAdapter;
    private Button selectAll;
    private Button cancal;
    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        selectAll = (Button) findViewById(R.id.select_all);
        cancal = (Button) findViewById(R.id.cancal);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        list.add(R.mipmap.ic_launcher);
        list.add(R.mipmap.ic_launcher);
        list.add(R.mipmap.ic_launcher);
        list.add(R.mipmap.ic_launcher);
        list.add(R.mipmap.ic_launcher);
        list.add(R.mipmap.ic_launcher);
        list.add(R.mipmap.ic_launcher);
        list.add(R.mipmap.ic_launcher);
        list.add(R.mipmap.ic_launcher);
        list.add(R.mipmap.ic_launcher);
        list.add(R.mipmap.ic_launcher);


        setRecyclerview();
        initData();

    }

    private void initData() {
        selectAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isType = true;
                recyclerviewAdapter.getAllSelect();
            }
        });
        cancal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isType = false;
                recyclerviewAdapter.setAllSelect();
            }
        });
    }

    private void setRecyclerview() {
        LinearLayoutManager layout = new LinearLayoutManager(this);
        layout.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(layout);
        recyclerviewAdapter = new RecyclerviewAdapter(list, this);
        //rvGroupPicture.addItemDecoration(recyclerViewItemDecoration);
        recyclerView.setAdapter(recyclerviewAdapter);
    }

    public void setResponseState() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {

                    int len = 0;
                    int lenght = list.size();
                    if (lenght > 1) {
                        for (int i = 0; i < lenght; i++) {
                            if (recyclerviewAdapter.ischeck.get(i, false)) {
                                len = len + 1;
                            }
                        }
                        if (len == 0) {
                            isType = false;

                        } else if (len > 0 & len < lenght) {
                            isType = false;

                        } else if (len == lenght) {
                            isType = true;

                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

}
mainactivity的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/select_all"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="全选" />

    <Button
        android:id="@+id/cancal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="取消" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>
adapter

public class RecyclerviewAdapter extends RecyclerView.Adapter<RecyclerViewHodler> {
    private List list;
    private Context context;
    public SparseArray<Boolean> ischeck;
    public SparseArray<CheckBox> listbox;
    private boolean selectedState = false;

    public RecyclerviewAdapter(List<String> list, Context context) {
        this.context = context;
        this.list = list;
        ischeck = new SparseArray<Boolean>();
        listbox = new SparseArray<CheckBox>();

    }

    @Override
    public int getItemCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public RecyclerViewHodler onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.picture_item, parent, false);
        return new RecyclerViewHodler(view);
    }

    @Override
    public void onBindViewHolder(RecyclerViewHodler holder, int position) {
        holder.iv.setBackgroundResource((Integer) list.get(position));

        holder.relativeLayour_item.setId(position);
        SetSelectedState(holder.checkBoxGroupPicture, ischeck.get(position, false));
        listbox.put(position, holder.checkBoxGroupPicture);
        holder.relativeLayour_item.setOnClickListener(onclick);

    }

    public View.OnClickListener onclick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int position = v.getId();
            setIsSelectedState(true);
            if (ischeck == null) {
                ischeck.put(position, true);
            } else {
                if (!ischeck.get(position, false)) {
                    ischeck.put(position, true);
                } else {
                    ischeck.put(position, false);
                }
            }
            SetSelectedState(listbox.get(position), ischeck.get(position));
            ((MainActivity) context).setResponseState();
        }
    };

    public void setIsSelectedState(boolean state) {
        selectedState = state;
    }

    public boolean getIsSelectedState() {
        return selectedState;
    }

    public void SetSelectedState(CheckBox checkBox, boolean type) {
        if (getIsSelectedState()) {
            checkBox.setChecked(type);
            if (type) {
                checkBox.setBackgroundResource(R.mipmap.selected);

            } else {
                checkBox.setBackgroundResource(R.mipmap.unselected);

            }
        }
    }

    public void getAllSelect() {
        try {
            setIsSelectedState(true);
            if (getIsSelectedState()) {
                int length = list.size();
                ischeck.clear();
                //groupPath.clear();
                for (int i = 0; i < length; i++) {
                    ischeck.put(i, true);
                    // ((GroupPictureEditActivity) context).setResponseState();
                    if (i < listbox.size()) {
                        SetSelectedState(listbox.get(i), ischeck.get(i));
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

        public void setAllSelect() {
        try {
            setIsSelectedState(true);
            if (getIsSelectedState()) {
                ischeck.clear();
                int length = list.size();
                for (int i = 0; i < length; i++) {
                    ischeck.put(i, false);
                    if (i < listbox.size()) {
                        SetSelectedState(listbox.get(i), ischeck.get(i));
                    }
                }
                ((MainActivity)context).setResponseState();
            }
        } catch (Exception e) {
            e.printStackTrace();
            /** 为避免index下标越界 */
        }
    }

}
item布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/relativeLayour_item"
        android:layout_width="60dp"
        android:layout_height="60dp">

        <ImageView
            android:id="@+id/iv"
            android:layout_width="60dp"
            android:layout_height="60dp" />

        <CheckBox
            android:id="@+id/checkbox"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:background="@mipmap/unselected"
            android:button="@null"
            android:clickable="false"
            android:duplicateParentState="true"
            android:focusable="false"
            android:focusableInTouchMode="false" />
    </RelativeLayout>


</LinearLayout>
viewholder

public class RecyclerViewHodler extends RecyclerView.ViewHolder {
    public ImageView iv;
    public CheckBox checkBoxGroupPicture;
    public RelativeLayout relativeLayour_item;

    public RecyclerViewHodler(View view){
        super(view);
        iv = (ImageView)view.findViewById(R.id.iv);
        iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
        checkBoxGroupPicture = (CheckBox)view.findViewById(R.id.checkbox);
        relativeLayour_item = (RelativeLayout)view.findViewById(R.id.relativeLayour_item);
    }
}






猜你喜欢

转载自blog.csdn.net/qq_35698774/article/details/74619298