Abdulrahman
Abdulrahman

Reputation: 301

Select from adapter Android

I want to implement this UI to make the user select one branch at a time. What I'm doing now is setCompoundDrawablesWithIntrinsicBounds for the text in each item when the user selects it.

enter image description here

The issues are "If I select item number 1 and then selected item number 4, how can I remove the selection from the item number 1? I don't want to click the item number 1 again to remove the selection"

Note that this screen makes the user select only one item.

This is my code in Kotlin But it is OK if you suggest a solution in JAVA:

var selectedBranch = false
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
    holder.packageNumber.text = branches?.get(position)?.nameEn ?: ""
    check = ContextCompat.getDrawable(context, R.drawable.ic_select_branch)
    holder.itemView.setOnClickListener {

        if(!selectedBranch) {
        holder.packageNumber.setCompoundDrawablesWithIntrinsicBounds(null, null, check, null)
            selectedBranch = true
        }
        else {
            holder.packageNumber.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null)
            selectedBranch = false
        }
    }
}

Upvotes: 1

Views: 382

Answers (4)

Jaymin Soni
Jaymin Soni

Reputation: 451

1.Create One Model Class which contain

String text;
boolean isTextSelected;

2.On Click of item in adapter, Update your model class 'isTextSelected' field.

3.Notify your adpater.

Upvotes: 1

user4571931
user4571931

Reputation:

Take one boolean variable into branche pojo class. then make interface into adapter to handle click event into recyclerview like below code ..

  onItemClickListner onItemClickListner;

public void setOnItemClickListner(RecyclerViewAdpater.onItemClickListner onItemClickListner) {
    this.onItemClickListner = onItemClickListner;
}

public interface onItemClickListner {
    void onClick(Branch str);//pass your object types.
}



@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
    // below code handle click event on recycler view item.
    Branch data=branches.get(position);
    if (data.isSelected()){
    }
    else{

    }
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onItemClickListner.onClick(data);
        }
    });
}

then after adapter bind into recycler view call below code..

recyclerViewAdpater.setOnItemClickListner(new RecyclerViewAdpater.onItemClickListner() {
        @Override
        public void onClick(Branch str)
        {
            str.setSelected(true);
            recyclerViewAdpater.notifyDataSetChanged();
        }

    });

Upvotes: 0

Jyoti JK
Jyoti JK

Reputation: 2171

Instead of using a flag in each item, Use integer variable to store the last clicked position

Initially clickedposition=-1

Use this in your adapter class,

if(position == clickedposition) {

        holder.packageNumber.setCompoundDrawablesWithIntrinsicBounds(null, null, check, null)
}
else {
        holder.packageNumber.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null)

}

holder.itemView.setOnClickListener {
      clickedposition=position;
      notifyDataSetChanged();
}

Upvotes: 0

Vijay Makwana
Vijay Makwana

Reputation: 486

You need to add this "selectedBranch" Boolean in your data class which you have used as a model in branches list

add this code to your bindViewHolder

if(branches?.get(position)?.selectedBranch == false) {

            holder.packageNumber.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null)
        }
        else {
            holder.packageNumber.setCompoundDrawablesWithIntrinsicBounds(null, null, check, null)
      }

holder.itemView.setOnClickListener {

    if(branches?.get(position)?.selectedBranch == false) {

        holder.packageNumber.setCompoundDrawablesWithIntrinsicBounds(null, null, check, null)
        branches?.get(position)?.selectedBranch = true
    }
    else {
        holder.packageNumber.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null)
        branches?.get(position)?.selectedBranch = false
    }
 adapter.notifyDatasetChange()
}

I hope it works.

Upvotes: 0

Related Questions