Recyclerview单选和多选

Recyclerview单选和多选

思路
单选的时候:在adapter中声明一个变量lastClickPosition记住点击的是哪个位置的item,在getView()方法中,如果lastClickPosition =position,则imageVIew.setImageResource(R.drawable.icon)
多选的时候:建立一个bean文件,生明一个 isSelected变量,通过在数据中保存这个状态,来知道哪个item被选中了。

单选代码:

public class ListViewActivity extends AppCompatActivity {

    private List  mDate=new ArrayList<>();
    private ListAdapter listAdapter;

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

        setContentView(R.layout.list_activity);

       ListView listView =  findViewById(R.id.lv);

       for(int i=0;i<100;i++){
           mDate.add("item"+i);
       }
        listAdapter = new ListAdapter(this,mDate);
       listView.setAdapter(listAdapter);

       listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView parent, View view, int position, long id) {
               listAdapter.singleChoose(position);

           }
       });
    }


}

adapter

public class ListAdapter extends BaseAdapter {

    private Context context;
    private List mDates;
    private LayoutInflater mLayoutInflater;
    private int lastClickPosition=-1;

    public ListAdapter(Context context, List mDates) {
        this.context = context;
        this.mDates = mDates;
        mLayoutInflater = LayoutInflater.from(context);

    }

    public void singleChoose(int position){
        lastClickPosition = position;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mDates==null?0:mDates.size();
    }

    @Override
    public Object getItem(int position) {
        return getItem(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView==null){
             viewHolder = new ViewHolder();
            convertView = mLayoutInflater.inflate(R.layout.item_list,parent,false);
            viewHolder.tvItem = convertView.findViewById(R.id.tvItem);
            viewHolder.imageView = convertView.findViewById(R.id.imageView);
            convertView.setTag(viewHolder);
        }else {

          viewHolder = (ViewHolder) convertView.getTag();
        }


        if(position==lastClickPosition){
            viewHolder.imageView.setImageResource(R.drawable.check);
        }else {
            viewHolder.imageView.setImageResource(R.drawable.nocheck);
        }
        viewHolder.tvItem.setText(mDates.get(position));

        return convertView;
    }

    static class ViewHolder{

        private TextView tvItem;
        private ImageView imageView;

    }
}

多选代码

public class ListMultipleViewActivity extends AppCompatActivity {

    private List  mDate=new ArrayList<>();
    private ListMultipleAdapter listAdapter;
    private TextView tvClickNum;

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

        setContentView(R.layout.list_activity);

       ListView listView =  findViewById(R.id.lv);
        tvClickNum = findViewById(R.id.tvClickNum);

       for(int i=0;i<100;i++){
           ListItemBean listItemBean = new ListItemBean();
           listItemBean.setSelected(false);
           listItemBean.setItemName("item"+i);
           mDate.add(listItemBean);
       }
        listAdapter = new ListMultipleAdapter(this,mDate);
       listView.setAdapter(listAdapter);

       listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView parent, View view, int position, long id) {
               listAdapter.multipleChoose(position);

           }
       });

        tvClickNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num=0;
               Iterator iterator = mDate.iterator();
                while (iterator.hasNext()){
                    ListItemBean listItemBean = (ListItemBean) iterator.next();
                    if(listItemBean.isSelected()){
                        num++;
                    }
                }
                if(num>0){
                    tvClickNum.setText("统计:被选中的item数量是"+num+"个");
                }
            }
        });
    }


}

多选adapter

public class ListMultipleAdapter extends BaseAdapter {

    private Context context;
    private List mDates;
    private LayoutInflater mLayoutInflater;
    private int lastClickPosition = -1;

    public ListMultipleAdapter(Context context, List mDates) {
        this.context = context;
        this.mDates = mDates;
        mLayoutInflater = LayoutInflater.from(context);
    }

    public void multipleChoose(int position) {
        ListItemBean listItemBean = mDates.get(position);
        if (listItemBean.isSelected()) {
            listItemBean.setSelected(false);
        } else {
            listItemBean.setSelected(true);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mDates == null ? 0 : mDates.size();
    }

    @Override
    public Object getItem(int position) {
        return getItem(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = mLayoutInflater.inflate(R.layout.item_list, parent, false);
            viewHolder.tvItem = convertView.findViewById(R.id.tvItem);
            viewHolder.imageView = convertView.findViewById(R.id.imageView);
            convertView.setTag(viewHolder);
        } else {

            viewHolder = (ViewHolder) convertView.getTag();
        }


        if (mDates.get(position).isSelected()) {
            viewHolder.imageView.setImageResource(R.drawable.check);
        } else {
            viewHolder.imageView.setImageResource(R.drawable.nocheck);
        }
        viewHolder.tvItem.setText(mDates.get(position).getItemName());
        return convertView;
    }

    static class ViewHolder {

        private TextView tvItem;
        private ImageView imageView;

    }
}

item布局文件





    

        


        


    



listview_activity



    

    


demo下载
https://download.csdn.net/download/qq_26296197/10840545

你可能感兴趣的:(Android)