1

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
        }
    }
}
Arun J
  • 687
  • 4
  • 14
  • 27
Abdulrahman
  • 301
  • 1
  • 3
  • 12
  • 2
    In your list add a flag isSelected with every item. And on selection of any item set isSelected false for all other items and call notifyDataSetChange on adapter. Also show your tick mark based on isSelected flag from the list – Arshad Apr 25 '18 at 10:16
  • Please refer to https://stackoverflow.com/a/28651847/4232337 – NSimon Apr 25 '18 at 10:19
  • @Arshad if you can show me an example it would be great! – Abdulrahman Apr 25 '18 at 10:22
  • @Abdulrahman NSimon has given a good reference. check this answer of that query it explain exactly what I told https://stackoverflow.com/a/42020046/3136282 – Arshad Apr 25 '18 at 10:25
  • See this one also https://stackoverflow.com/a/29030776/1427776 – Deven Apr 25 '18 at 10:32
  • @Abdulrahman try my solution – Jyoti JK Apr 25 '18 at 11:38

4 Answers4

1

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.

Jyoti JK
  • 2,141
  • 1
  • 17
  • 40
Jaymin Soni
  • 451
  • 3
  • 10
0

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.

Vijay Makwana
  • 506
  • 5
  • 14
0

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();
}
Jyoti JK
  • 2,141
  • 1
  • 17
  • 40
0

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();
        }

    });