可全选或取消全选和支持item单个的RecyclerView

RecyclerView.Adapter
public class SelectAdapter extends RecyclerView.Adapter<SelectAdapter.SelectHolder> implements View.OnClickListener, View.OnLongClickListener {
    private int mCurItem = -1;
    private List<ItemBean> itemBeans;
    private OnItemClickListener mOnItemClickListener;
    private OnSelectedAllListener mOnSelectedAllListener;

    public void setData(List<ItemBean> itemBeanList) {
        itemBeans = new ArrayList<>();
        itemBeans.addAll(itemBeanList);
    }

    @NonNull
    @Override
    public SelectHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_select_all, viewGroup, false);
        SelectHolder holder = new SelectHolder(view);
        if (!itemBeans.get(position).isIgnore()) {
            holder.selectLinearLayout.setOnClickListener(this);
            holder.selectLinearLayout.setOnLongClickListener(this);
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull SelectHolder holder, int position) {
        ItemBean itemBean = itemBeans.get(position);
        holder.selectLinearLayout.setTag(position);
        holder.itemTextView.setText(itemBean.getItemText());
        boolean ignore = itemBean.isIgnore();
        if (ignore) {
            holder.selectImageView.setEnabled(false);
            holder.selectImageView.setSelected(true);
            holder.itemTextView.setSelected(false);
            holder.itemTextView.setBackgroundResource(R.color.txt_FFFFFF);
        } else {
            boolean isSelected = itemBean.isSelected();
            holder.selectImageView.setEnabled(isSelected);
            holder.selectImageView.setSelected(isSelected);
            holder.itemTextView.setSelected(mCurItem == position);
            holder.itemTextView.setBackgroundResource(mCurItem == position ?
                    R.color.colorPrimaryDark : R.color.txt_FFFFFF);
        }
    }

    @Override
    public int getItemCount() {
        return itemBeans.size();
    }

    @Override
    public void onClick(View v) {
        setItemSelectState(v, true);
        changeSelectAllState();
    }

    @Override
    public boolean onLongClick(View v) {
        setItemSelectState(v, false);
        changeSelectAllState();
        return false;
    }

    private void setItemSelectState(View v, boolean isClick) {
        int position = (Integer) v.getTag();
        ItemBean itemBean = itemBeans.get(position);
        if (mOnItemClickListener != null) {
            if (isClick) {
                mOnItemClickListener.onItemClick(position);
            } else {
                mOnItemClickListener.onLongItemClick(position);
            }
        }
        boolean isSelect = itemBean.isSelected();
        itemBean.setSelected(!isSelect);
        notifyDataSetChanged();
    }

    private void changeSelectAllState() {
        int ignoreCount = 0;
        int selectCount = 0;
        for (int i = 0; i < itemBeans.size(); i++) {
            if (itemBeans.get(i).isIgnore()) {
                ignoreCount++;
            } else if (itemBeans.get(i).isSelected()) {
                selectCount++;
            }
        }
        if (mOnSelectedAllListener != null) {
            mOnSelectedAllListener.onSelectedAll(itemBeans.size() == ignoreCount + selectCount);
        }
    }


    public void selectAll(boolean selectAll) {
        for (int i = 0; i < itemBeans.size(); i++) {
            boolean ignore = itemBeans.get(i).isIgnore();
            if (!ignore) {
                itemBeans.get(i).setSelected(selectAll);
            }
        }
        notifyDataSetChanged();
    }

    public void setCurItem(int position) {
        this.mCurItem = position;
    }

    class SelectHolder extends RecyclerView.ViewHolder {
        LinearLayout selectLinearLayout;
        ImageView selectImageView;
        TextView itemTextView;

        SelectHolder(@NonNull View itemView) {
            super(itemView);
            selectLinearLayout = itemView.findViewById(R.id.item_select_all_layout);
            selectImageView = itemView.findViewById(R.id.item_select_all_img);
            itemTextView = itemView.findViewById(R.id.item_select_all_text);
        }
    }

    public void addItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    /**
     * RecyclerView 条目的点击事件
     */
    public interface OnItemClickListener {

        /**
         * item被点击
         *
         * @param position position
         */
        void onItemClick(int position);

        /**
         * item被长按点击
         *
         * @param position position
         */
        void onLongItemClick(int position);
    }

    public void addSelectedAllListener(OnSelectedAllListener onSelectedAllListener) {
        mOnSelectedAllListener = onSelectedAllListener;
    }

    /**
     * RecyclerView 条目是否处于全选的监听事件
     */
    public interface OnSelectedAllListener {

        /**
         * 所有子条目是否被选中
         *
         * @param isSelectAll isSelectAll
         */
        void onSelectedAll(boolean isSelectAll);
    }
}

Activity中的应用

public class SelectAllActivity extends AppCompatActivity implements View.OnClickListener, SelectAdapter.OnItemClickListener, SelectAdapter.OnSelectedAllListener {

    private List<ItemBean> itemBeans;
    private boolean needSelect = true;
    private ImageView selectImageView;
    private Context mContext;
    private SelectAdapter selectAdapter;

    public static void launch(Context context) {
        Intent intent = new Intent(context, SelectAllActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_all);
        initData();
        initView();
    }

    private void initData() {
        mContext = SelectAllActivity.this;
        itemBeans = ItemBean.getData();
    }

    private void initView() {
        LinearLayout linearLayout = findViewById(R.id.activity_select_all_layout);
        selectImageView = findViewById(R.id.activity_select_all_img);
        RecyclerView recyclerView = findViewById(R.id.activity_select_all_recycler);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        selectAdapter = new SelectAdapter();
        selectAdapter.setData(itemBeans);
        recyclerView.setAdapter(selectAdapter);
        linearLayout.setOnClickListener(this);
        selectAdapter.addSelectedAllListener(this);
        selectAdapter.addItemClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_select_all_layout:
                setSelectImgState();
                break;
            default:
                break;
        }
    }

    private void setSelectImgState() {
        selectImageView.setEnabled(needSelect);
        selectImageView.setSelected(needSelect);
        selectAdapter.selectAll(needSelect);
        needSelect = !needSelect;
    }

    @Override
    public void onSelectedAll(boolean isSelectAll) {
        selectImageView.setEnabled(isSelectAll);
        selectImageView.setSelected(isSelectAll);
    }

    @Override
    public void onItemClick(int position) {
        selectAdapter.setCurItem(position);
        selectAdapter.notifyDataSetChanged();
        Toast.makeText(this, "Short:" + position, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onLongItemClick(int position) {
        selectAdapter.setCurItem(position);
        selectAdapter.notifyDataSetChanged();
        Toast.makeText(this, "Long  " + position, Toast.LENGTH_SHORT).show();
    }
}

Recycler的数据

public class ItemBean {
    private boolean ignore;
    private boolean isSelected;
    private String itemText;

    public boolean isIgnore() {
        return ignore;
    }

    public void setIgnore(boolean ignore) {
        this.ignore = ignore;
    }

    public boolean isSelected() {
        return isSelected;
    }

    public void setSelected(boolean selected) {
        isSelected = selected;
    }

    public String getItemText() {
        return itemText;
    }

    public void setItemText(String itemText) {
        this.itemText = itemText;
    }

    public static List<ItemBean> getData() {
        List<ItemBean> itemBeans = new ArrayList<>();
        int a = 20;
        for (int i = 0; i < a; i++) {
            ItemBean itemBean = new ItemBean();
            itemBean.setIgnore(i == 4);
            itemBean.setItemText("我是RecyclerView条目的文本");
            itemBean.setSelected(i % 2 == 0);
            itemBeans.add(itemBean);
        }
        return itemBeans;
    }
}
drawable/selector_select
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/select_icon_disabled" android:state_enabled="false" android:state_selected="true" />
    <item android:drawable="@drawable/select_icon" android:state_enabled="true" android:state_selected="true" />
    <item android:drawable="@drawable/not_select_icon" />
</selector>

 

转载于:https://www.cnblogs.com/sj-anhui/p/11117314.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值