java 通用单选多选列表(RecyclerView)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 通用单选多选列表(RecyclerView)相关的知识,希望对你有一定的参考价值。

package com.wenmin92.test.publishgoodsremark;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

import com.wenmin92.test.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * Created by wenmin92 on 2018/1/19.
 * 通用单选多选
 */
public class RVSelectorAdapter extends RecyclerView.Adapter<RVSelectorAdapter.ViewHolder> {

    private Context mContext;
    private List<SelectableItem> mData;
    private OnItemSelectedListener mOnItemSelectedListener;
    private int mMaxSelectNum = 1;
    private int mSelectedNum;
    private int mLastSelectedIndex = -1;
    private boolean mNeedChangeTextSize = false;
    private boolean mCheckable = true;

    @SuppressWarnings({"unused", "WeakerAccess"})
    public RVSelectorAdapter(Context context, String[] data) {
        this(context, data, 1);
    }

    @SuppressWarnings({"unused", "WeakerAccess"})
    public RVSelectorAdapter(Context context, String[] data, int maxSelectNum) {
        mContext = context;
        mMaxSelectNum = maxSelectNum;
        mData = new ArrayList<>();
        for (String item : data) {
            mData.add(new SelectableItem(item));
        }
    }

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

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        SelectableItem item = mData.get(position);
        holder.cbItem.setText(item.name);
        holder.cbItem.setChecked(item.select);
        if (mNeedChangeTextSize && !TextUtils.isEmpty(item.name) && item.name.length() > 4) {
            holder.cbItem.setTextSize(TypedValue.COMPLEX_UNIT_SP, holder.cbItem.getTextSize() - 2);
        }
    }

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

    /**
     * 设置是否根据字数改变字体大小,默认false
     */
    @SuppressWarnings({"unused", "WeakerAccess", "SameParameterValue"})
    public void setNeedChangeTextSize(boolean needChangeTextSize) {
        mNeedChangeTextSize = needChangeTextSize;
    }

    /**
     * 设置是否显示选中状态,仅用于单选,默认true
     */
    @SuppressWarnings({"unused", "WeakerAccess", "SameParameterValue"})
    public void setCheckable(boolean checkable) {
        mCheckable = checkable;
    }

    /**
     * 设置最多可选择的item的数量,默认1
     */
    @SuppressWarnings({"unused", "WeakerAccess", "SameParameterValue"})
    public void setMaxSelectNum(int maxSelectNum) {
        mMaxSelectNum = maxSelectNum;
    }

    /**
     * 设置已经选择的item(单选)
     */
    @SuppressWarnings({"unused", "WeakerAccess", "SameParameterValue"})
    public void setSelectedItem(String item) {
        setSelectedItems(new String[]{item});
    }

    /**
     * 设置已经选择的item(多选),使用英文逗号隔开
     */
    @SuppressWarnings({"unused", "WeakerAccess", "SameParameterValue"})
    public void setSelectedItems(String items) {
        setSelectedItems(items, ",");
    }

    /**
     * 设置已经选择的item(多选),使用指定字符隔开
     */
    @SuppressWarnings({"unused", "WeakerAccess", "SameParameterValue"})
    public void setSelectedItems(String items, String separator) {
        setSelectedItems(items.split(separator));
    }

    /**
     * 设置已经选择的item(多选)
     */
    @SuppressWarnings({"unused", "WeakerAccess"})
    public void setSelectedItems(String[] items) {
        for (String item : items) {
            if (TextUtils.isEmpty(item)) {
                break;
            }
            for (int i = 0; i < mData.size(); i++) {
                SelectableItem rawItem = mData.get(i);
                if (item.trim().equals(rawItem.name)) {
                    rawItem.select = true;
                    notifyItemChanged(i);
                    break;
                }
            }
        }
    }

    /**
     * 获取已经选择的item,使用英文逗号分隔符拼成字符串
     */
    @SuppressWarnings({"unused", "WeakerAccess"})
    public String getSelectedStrings() {
        return getSelectedStrings(",");
    }

    /**
     * 获取已经选择的item,使用指定分隔符拼成字符串
     */
    @SuppressWarnings({"unused", "WeakerAccess", "SameParameterValue"})
    public String getSelectedStrings(String separator) {
        return TextUtils.join(separator, getSelectedArray());
    }

    /**
     * 获取已经选择的item
     */
    @SuppressWarnings({"unused", "WeakerAccess"})
    public String[] getSelectedArray() {
        List<String> selected = new ArrayList<>();
        for (SelectableItem item : mData) {
            if (item.select) selected.add(item.name);
        }
        String[] stockArr = new String[selected.size()];
        return selected.toArray(stockArr);
    }

    /**
     * 设置item选择后的监听器
     */
    @SuppressWarnings({"unused", "WeakerAccess"})
    public void setOnItemSelectedListener(OnItemSelectedListener onItemSelectedListener) {
        mOnItemSelectedListener = onItemSelectedListener;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private final CheckBox cbItem;

        ViewHolder(View itemView) {
            super(itemView);
            cbItem = itemView.findViewById(R.id.cb_item);
            cbItem.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    SelectableItem item = mData.get(getAdapterPosition());
                    if (isChecked == item.select) return;
                    item.select = isChecked;

                    // 如果是不显示选中状态,则取消选中并设置监听后,直接跳出。
                    if (!mCheckable && isChecked) {
                        buttonView.setChecked(false);
                        item.select = false;
                        setOnItemSelectedListener(item);
                        return;
                    }

                    if (mMaxSelectNum == 1) { // 单选模式
                        if (isChecked && mLastSelectedIndex >= 0) {
                            mData.get(mLastSelectedIndex).select = false;
                            notifyItemChanged(mLastSelectedIndex);
                        } else if (!isChecked && mLastSelectedIndex == getAdapterPosition()) {
                            mLastSelectedIndex = -1;
                        }
                        if (isChecked) {
                            mLastSelectedIndex = getAdapterPosition();
                            setOnItemSelectedListener(item);
                        }
                    } else { // 多选模式
                        if (isChecked && mSelectedNum >= mMaxSelectNum) {
                            Toast.makeText(mContext, String.format(Locale.CHINA, "最多选择%d个,先取消一个,再选择", mMaxSelectNum), Toast.LENGTH_SHORT).show();
                            item.select = false;
                            buttonView.setChecked(false);
                            return;
                        }

                        if (isChecked) mSelectedNum++;
                        else mSelectedNum--;

                        if (isChecked) setOnItemSelectedListener(item);
                    }
                }
            });
        }

        private void setOnItemSelectedListener(SelectableItem item) {
            if (mOnItemSelectedListener != null) {
                mOnItemSelectedListener.onItemSelected(item.name);
            }
        }
    }

    private class SelectableItem {
        String name;
        boolean select;

        SelectableItem(String item) {
            name = item;
        }
    }

    public interface OnItemSelectedListener {
        void onItemSelected(String item);
    }
}

以上是关于java 通用单选多选列表(RecyclerView)的主要内容,如果未能解决你的问题,请参考以下文章

jQuery操作下拉列表以及单选多选框

Jquery获取列表中的值和input单选多选框控制选中与取消

iOS——UITableView单选模式,多选模式,单选多选混合模式

单选多选

vue单选多选,文本域,选择框

单选多选样式写法