I have a recyclerview with simple items - an item has an image, title and a button. When the user clicks on the button it needs to change its layout -> indicating that button is clicked (similar to checkbox functionality).
Problem is that when I click on a button, for example the second item, it behaves weirdly when I scroll - multiple items are toggled or the original one is untoggled. You can check it out in image here:
GIF PREVIEW
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
holder.button.setOnClickListener(view -> {
toggleButtonStyle((Button)view);
});
}
public void toggleButtonStyle(Button toggle)
{
Context ctx = toggle.getContext();
if (toggle.isActivated()) {
toggle.setActivated(false);
toggle.setBackground(ContextCompat.getDrawable(ctx, R.drawable.btn_purple_corners));
toggle.setTextColor(ContextCompat.getColor(ctx, R.color.purple_light));
} else {
toggle.setActivated(true);
toggle.setBackground(ContextCompat.getDrawable(ctx, R.drawable.btn_purple));
toggle.setTextColor(ContextCompat.getColor(ctx, R.color.white));
}
}
You need to specify position for a onBindView method, because it's called multiple times. If you are using AutoValue immutable types, you can try to make an Array of booleans where you'll be storing toggled states (true/false). Then in onBindViewMethod check a value of current position and style a button.
private boolean[] toggledChoices = new boolean[yourListWithItems.size()];
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (toggledChoices[position]) {
toggleButtonStyle(true, holder.toggleButton);
} else {
toggleButtonStyle(false, holder.toggleButton;
}
holderVote.bToggleItem.setOnClickListener(v -> {
if (!toggledChoices[position]) {
toggledChoices[position] = true;
} else {
toggledChoices[position] = false;
}
});
}
I had some strange behavior too in the past.
For me it was solved by simply calling
toggle.invalidate();
after your if/else block. This forces the redraw. Try it, I was surprised too, that this fixed it for me.
Check Out my code
#Override
public void onBindViewHolder(GifGridAdapter.ViewHolder holder, final int position) {
final DataModel model = arrayList.get(position);
if (model.isToggle()) {
//Code for changing the button background color and text color
} else {
//Code for changing the button background color and text color
}
holder.ivGif.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
model.setToggle(!model.isToggle());
notifyDataSetChanged();
}
});
}
Related
So, I followed this link for single click functionality using a radio button in my case inside a recyclerview. Its working almost fine. Only facing one issue, is when you try tapping twice on the first item of recyclerview, it always stays true even though you try hitting any other item after that. Below is the logic which I tried to implement.
private List<Profile> profileList;
private Context context;
private RadioButton lastChecked = null;
private int lastCheckedPos = 0;
public void onBindViewHolder(#NonNull MyHolder holder, int position) {
Profile profile = profileList.get(position);
holder.imgRadioButton.setChecked(profile.isSelected());
holder.imgRadioButton.setTag(position);
holder.imgRadioButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int clickpos = (Integer) holder.imgRadioButton.getTag();
if (lastCheckedPos != clickpos) {
if (holder.imgRadioButton.isChecked()) {
if (lastChecked != null) {
lastChecked.setChecked(false);
profileList.get(lastCheckedPos).setSelected(false);
}
lastChecked = holder.imgRadioButton;
lastCheckedPos = clickpos;
} else {
lastChecked = null;
}
profileList.get(clickpos).setSelected(holder.imgRadioButton.isChecked());
}
}
});
}
Dont know where its messing up any logic. But I want it to work in a regular way how radio button functions, but in a recyclerview.
You need to try this.
Add lastChecked.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
lastCheckededPos = getAdapterPosition();
notifyDataSetChanged();
}
});
to the ViewHolder() constructor.
Then use this in onBindViewHolder() method:
// this condition un-checks previous selections
holder.lastChecked.setChecked(lastCheckedPos == position);
That's it. Now your radio button is selected One at a time and selecting other radio button will un-select previous selection.
I'm working on a app that uses RecyclerView and ActionMode. On the RecyclerView item I have an Imageview (mMultipleSelectionBackground) that is set to GONE. Basically when I use long click / click on an item it will select it and highlight it (changing the ImageView to visible) (if multiple items are selected, it will change the ImageView to Visible on each particular one). I'm doing this in the Adapter class.
#Override
public boolean onLongClick(View view) {
int longClickedPosition = getAdapterPosition();
mLongClickListener.onToDoLongClick(longClickedPosition);
ToDo toDo = mToDos.get(longClickedPosition);
try {
/**
* Check to see if the item is selected
* #mMultipleSelections - use this to block the long click if the user already did it on an item
*/
if (!toDo.isSelected() && mMultipleSelections <= 0){
toDo.setSelected(true);
mMultipleSelections++;
selectedToDos.add(longClickedPosition);
mMultipleSelectionBackground.setVisibility(View.VISIBLE);
view.startActionMode(mActionModeCallback);
}
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
So far so good. The issue that I'm having is that when I'm using the onDestroyActionMode, I want the ImageView on all of the items to be reverted to GONE, but they are not. Only the first one is changed(and indeed, by this logic, it is normal).
#Override
public void onDestroyActionMode(ActionMode actionMode) {
Log.d(LOG_TAG, "DESTROY");
for (int x = 0; x < selectedToDos.size(); x++) {
mMultipleSelectionBackground.setVisibility(View.GONE);
mToDos.get(x).setSelected(false);
}
mMultipleSelections = 0;
}
My question is, how do I change the ImageView on all the items and not only the first one?
It will be better if you add one more extra boolean field inside your ToDo class which will be false by default for checking selection of that entry like
class ToDo{
...
private boolean isSelected;
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
...
}
So when you click or LongClick on your item just make that boolean value true/false accordingly and based on that value inside your adapter onBindViewHolder write following code
#Override
public void onBindViewHolder(#NonNull final UserViewHolder userViewHolder, int position) {
final Todo todo=mTodos.get(position);
if(todo.isSelected()){
viewHolder.imageView.setVisibility(View.VISIBLE);
}else{
viewHolder.imageView.setVisibility(View.GONE);
}
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
todo.setSelected(!todo.isSelected());
notifyDataSetChanged();
}
});
}
So Overall this boolean field will help you to easily manage your selection entries.
I am trying to create a quiz app. Could not figure out how to change the color of other item when one item in recycleview is clicked.When option 2 is clicked but the correct option is option 1 it should display as given below in picture. Solution please
Create two drawable files for selected and unselected buttons in your drawable folder.
Create a model class as below:
public class ModelDemo
{
//your declaration
boolean isClicked;
public void setIsClicked(boolean value) {
this.value = value ;
}
public boolean isClicked() {
return value;
}
}
//Now create an arraylist of type model like ArrayList<ModelDemo> , with other values you need to infalte your recyclerview. Set the isClicked to false initially. Inflate your recylerview as follows
public void onBindViewHolder(final Holder holder, final int position) {
final ModelDemo modelDemo= arrayZipModel.get(position);
//here write your code to inflate the data for button text
if (modelDemo.isClicked())
holder.yourButton.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.selected));
else
holder.yourButton.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.unselected));
holder.yourButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(modelDemo.isClicked())
modelDemo.setIsClicked(false)
else
modelDemo.setIsClicked(true)
notifyDataSetChanged();
}
});
}
I have a RecylerView which contains a textView as individual item. Based on the requirement, the background image of the textView will be changed on click after checking the present image.
In simple:
onClick textView TV--> check for the background image of the textview TV--> check if the background image of TV is a, then make it b and viceversa
Please find my code below:
viewHolder.addRemove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(viewHolder.clicked==0)
{
viewHolder.addRemove.setBackgroundResource(R.mipmap.added);
viewHolder.clicked=1;
groupSlcArray.add(model.getName().trim());
Log.d("groupArr", Arrays.toString(groupSlcArray.toArray()));
}
else
{
viewHolder.addRemove.setBackgroundResource(R.mipmap.addslcgroup);
viewHolder.clicked=0;
groupSlcArray.remove(model.getName().trim());
Log.d("groupArr", Arrays.toString(groupSlcArray.toArray()));
}
}
});
I am unable to find anyway to get the background resource of the textView for checking.Need your help.This question might seem too simple for some. So please before Outvoting, try to give a solution because I have tried all the possible solutions that I found.Thanks in advance
Try following code :
holder.textview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (holder.textview.getBackground() == getResources().getDrawable(R.drawable.a)){
holder.textview.setBackground(R.drawable.b);
}
else{
holder.textview.setBackground(R.drawable.a);
}
}
});
You have to understand about getView.
Sometimes If you scroll in ListView, This is not an exact Color
So I would like to recommand following ways like this:
1. Add Code on getView
if (groupSlcArray.contain(odel.getName().trim())) {
viewHolder.addRemove.setBackgroundResource(R.mipmap.addslcgroup);
} else {
viewHolder.addRemove.setBackgroundResource(R.mipmap.added);
}
2. Change OnClickListener Event Code
viewHolder.addRemove.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick (View v){
if (groupSlcArray.contain(odel.getName().trim())) {
viewHolder.addRemove.setBackgroundResource(R.mipmap.addslcgroup);
groupSlcArray.remove(model.getName().trim());
Log.d("groupArr", Arrays.toString(groupSlcArray.toArray()));
} else {
viewHolder.addRemove.setBackgroundResource(R.mipmap.added);
groupSlcArray.add(model.getName().trim());
Log.d("groupArr", Arrays.toString(groupSlcArray.toArray()));
}
}
});
Here is a solution that you can use for change background image of each RecyclerView item.
For example, currently, each row of your RecyclerView is a Item, you need to create 1 more field name textViewBackGround for hold the back ground of each row like this
public class Item{
...
public int textViewBackGround = R.drawable.a; // default image a
}
Then in your adapter
public class MyRecyclerViewAdapter extends RecyclerView.Adapter < MyRecyclerViewAdapter.CustomViewHolder > {
private List < Item > itemList;
private Context mContext;
public MyRecyclerViewAdapter(Context context, List < Item > itemList) {
this.itemList = itemList;
this.mContext = context;
}
#Override
public void onBindViewHolder(CustomViewHolder viewHolder, int i) {
Item item = itemList.get(i);
viewHolder.textView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// don't need to get the background of your TextView then change the background, just need to check the datasource. Later on, you absolutely will know that check the datasource for change background of item is a correct way not check the background resource
if (item.textViewBackGround == R.drawable.a) {
viewHolder.textView.setBackgroundResource(R.drawable.b);
item.textViewBackGround = R.drawable.b;
} else {
viewHolder.textView.setBackgroundResource(R.drawable.a);
item.textViewBackGround = R.drawable.a;
}
}
});
viewHolder.textView.setBackgroundResource(item.textViewBackGround); // need to set the background image for `TextView` everytime bind ViewHolder because RecyclerView item will recycle when you scroll (if you don't understand it, you can remove this line, run your app, scroll up/down and you will understand it)
...
}
class CustomViewHolder extends RecyclerView.ViewHolder {
protected TextView textView;
public CustomViewHolder(View view) {
super(view);
...
}
}
}
i facing a problem in recyclerview's item.
My Adapter's code :
#Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
Profile item = mListChatting.get(position);
Log.d("TAG", "CEK : " + viewable);
if(viewable==true){
holder.mFormBookingan.setVisibility(View.GONE);
holder.mDetailBookingan.setVisibility(View.VISIBLE);
}else{
//assume that one way is show first as default
holder.mViewOneWay.setVisibility(View.VISIBLE);
holder.mViewRoundTrip.setVisibility(View.GONE);
holder.mOneOway.setBackgroundResource(R.drawable.round_just_left_white_focus);
holder.mRoundTrip.setBackgroundResource(R.drawable.state_pressed_booking_button_left);
holder.mSendBooking.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewable = true;
Log.d("TAG", "CEK 2 : " + viewable);
}
});
}
Like my code above, I want to hide mFormBookingan after mSendBooking has pressed. mFormBookingan never show anymore until user calls it again.
I have tried with a lot of ways but still can't find like what i need. After i press mSendBooking the form hide, but when i send new item to recyclerview, the from mFormBookingan that has been hide, appears again.
My Question, how to hide mFormBookingan forever? Until user call it again.
Thank in advance, i will appreciate anyone who help me for this one.
I not sure what the clear situation you want.
But if you want to set View invisible you can try this code then check it.
You need to add ismFormBookingVisible in viewHolder class as a boolean attribute.
#Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
Profile item = mListChatting.get(position);
Log.d("TAG", "CEK : " + viewable);
if(holder.ismFormBookingVisible==true){
holder.mFormBookingan.setVisibility(View.GONE);
holder.mDetailBookingan.setVisibility(View.VISIBLE);
}else{
//assume that one way is show first as default
holder.mViewOneWay.setVisibility(View.VISIBLE);
holder.mViewRoundTrip.setVisibility(View.GONE);
holder.mOneOway.setBackgroundResource(R.drawable.round_just_left_white_focus);
holder.mRoundTrip.setBackgroundResource(R.drawable.state_pressed_booking_button_left);
holder.mSendBooking.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.ismFormBookingVisible = false;
Log.d("TAG", "CEK 2 : " + viewable);
}
Try this :
Create a boolean in your model class "Profile" to keep track of visibility of button : say boolean isBookingVisible;
#Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
Profile item = mListChatting.get(position);
if(!item.isBookingVisible){
holder.mFormBookingan.setVisibility(View.GONE);
holder.mDetailBookingan.setVisibility(View.VISIBLE);
}else{
//assume that one way is show first as default
holder.mViewOneWay.setVisibility(View.VISIBLE);
holder.mViewRoundTrip.setVisibility(View.GONE);
holder.mOneOway.setBackgroundResource(R.drawable.round_just_left_white_focus);
holder.mRoundTrip.setBackgroundResource(R.drawable.state_pressed_booking_button_left);
holder.mSendBooking.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
item.isBookingVisible = false;
//Use notiyItemChanged(position); or notifyDataSetChanged(); here as per your selection criterion
Log.d("TAG", "CEK 2 : " + viewable);
}
});
}
You may have to call notifyDataSetChanged() after changing the value of viewable.
onBindViewHolder will be called when you notify your data set.
So you need to save the viewable in mListChatting. When you click the button, change the viewable in the mListChatting.
And then, change the code in onBindViewHolder
holder.mFormBookingan.setVisibility(item.getViewable() ? View.VISIBLE : View.GONE);
On refresh android will destroy the view and create new view with new adapter data. So you have to track the current state (visibility) of mFormBookingan. You can use a simple visibility list. When mFormBookingan state (visibility) change update it in visibility list so that whenever the list is refreshed, you can use it to check and set the last state (visibility) of your mFormBookingan. Here is an example
private ArrayList<Boolean> isVisible;
public MyAdapter(ArrayList<Boolean> isVisible){
// initial state list of mFormBookingan for each row of list
this.isVisible = isVisible;
}
public void onBindViewHolder(final MyViewHolder holder, final int position) {
if (isVisible.get(position)) {
holder.mFormBookingan.setVisibility(View.VISIBLE);
}else {
holder.mFormBookingan.setVisibility(View.GONE);
}
holder.mSendBooking.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (holder.mFormBookingan.getVisibility() == View.GONE){
holder.mFormBookingan.setVisibility(View.VISIBLE);
isVisible.set(position, true);
}else {
holder.mFormBookingan.setVisibility(View.GONE);
isVisible.set(position, false);
}
}
});
}
when you click mSendBooking the mFormBookingan visibility will change and it will remain same after sending new item to recyclerview.