Single selection in RecyclerView

后端 未结 15 1091
深忆病人
深忆病人 2020-11-22 15:15

I know there are no default selection methods in recyclerview class, But I have tried in following way,

public void onBindViewHolder(ViewHolder holder, final         


        
15条回答
  •  温柔的废话
    2020-11-22 16:01

    I want to share the similar thing I have achieved, may be it will help someone. below code is from the application to select an address from a list of addresses that are displayed in cardview(cvAddress), so that on click of particular item(cardview) the imageView inside the item should set to different resource(select/unselect)

        @Override
    public void onBindViewHolder(final AddressHolder holder, final int position)
    {
        holderList.add(holder);
        holder.tvAddress.setText(addresses.get(position).getAddress());
        holder.cvAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectCurrItem(position);
            }
        });
    }
    
    private void selectCurrItem(int position)
    {
        int size = holderList.size();
        for(int i = 0; i

    I don't know this is best solution or not but this worked for me.

提交回复
热议问题