favourite and unfavourite image in recyclerview gets in initial state when scrolled - android

I have a recycler adapter in which there are 3 image buttons, one of them is for favorite and unfavorite. when the recyclerviews loads its loads proper also when I click on favorite it stores in my favorite list. But the problem is that when I scrolled it the states of image button changes to load time images.

Make sure you are setting favorite/unfavorite at onBindViewHolder.

RecyclerView efficiently recycles views while you scroll.
when you maintain this kind of scenario, where image view can have Fav or UnFav images. you need to maintain both in onBindVIewHolder() method such as
if (isFav) {
viewHolder.yourImage.setBackgroundDrawable(FavImage);
} else {
viewHolder.yourImage.setBackgroundDrawable(UnFavImage);
}

Try setting the image on the #onBindViewHolder on every case (don't assume that you know the current state).

Related

RecyclerView Change ImageView in a certain position

I am building a memory game with RecyclerView grid layout. When user clicks 2 time and didn't find the matched pictures, I want to change image view resource only in the clicked positions.
I built the logic sucessfully, but I don't know how to change imageview's resource in only certain positions.
You might have used the OnClickListener interface for card items click events.
So on click event, get the position of that clicked image tile using the getLayoutPosition() method and change its Drawable resource programmaticaLly through captured tile position.
I solved my problem with recyclerView.layoutManager?.findViewByPosition(position)

Prevent swipe to delete recyclerview item

I want to catch a vertical swipe in an horizontal RecyclerView. Each item is simply a CircleImageView.
I found many resources on internet (like Drag and swipe with RecyclerView, Android - Swipe to delete RecyclerView) but those solutions ends deleting the item. I don't know if the term swipe requires also that the item is deleted or not.
What I want to achieve is to catch the swipe action on an item in the RecyclerView, but without delete the item itself from the RecyclerView.
I think that a good idea is to override the method onChildDraw() like suggested here: How to detect if Recyclerview item is being swiped?, but I can't understand how to achieve the behaviour I want.
My idea is: while the user swipes an item, the item itself moves in that direction; when the user end the touch event, the item has to come back to the original position (maybe changing the background color).
EDIT 1:
How to have swipe to delete and swipe to archive in two colours in Recyclerview Android probably can help, but it doesn't achieve the behaviour that I need. The item has to come back to the original position.
Your RecyclerView has RecyclerView.Adapter attached to it. The adapter determines what information that the RecyclerView can see and display. So, if item number 10, out of a 100-item backing array (managed by you) is swiped, the adapter can report that the array now contains 99 items and not ever present the swiped item to the RecyclerView. That way the item appears to be deleted but is maintained internally and still accessible programmatically. How you manage that internal state is up to you and dependent upon your implementation.
If, however you want to not remove the item from the screen but just change its appearance, I think that you would need to look at the method onItemDismiss that actually removes the item and notifies the adapter of the data change.
public void onItemDismiss(int position) {
mItems.remove(position);
notifyItemRemoved(position);
}
It is here that you would make the change. The item would stay in the adapter. You would also need to flag that position as "swiped" in case the view holders are recycle so you can maintain the visual "swiped" state.
public void onItemDismiss(int position) {
// Change background color, etc.
}
Take a look at code that has a "swipe to delete" function with "undo" for some ideas. Here is an example with a dialog that is called before deletion actually occurs if "Cancel" is clicked. There are many other examples of "undo" available. What you are trying to do can be considered to be an immediate and implicit "undo" with a visual change to the background.
If you want to have the item move back into position after a swipe, the following should work:
public void onItemDismiss(int position) {
notifyItemChanged(position);
}

Setting border to a recylerview element reproduces on scrolling back to the same object

I have a recycler view of image views and upon longpress I try to give the image view a border. When I scrolldown and come back to the same object the boder is found on someother image.(It is ok coz... the recycler view deletes itself and creates back and so the position changes).
So what I did is I stored the image URl on long press and after scrolling back I drew the Border on the image based on the image URL.
Now the problem is the border that came previously(wrong position) is also drawn....how to get rid of it.
In simple terms. How to make the recyclerview forget the data changes?
(Notifydatasetchanged....Something like that)....
thankz in advance....
This is the problem caused by the position at which the boarder was made is not the same position after the scroll on RecyclerView you can easily fix this by allowing your object to remember if it was selected by long-press or not
Assume this is the class you have
class Item {
// here you already have your instance variables
// add another one
boolean isSelected;
}
when you long-press on an item make this instance variable of respective Item, true and provide it a boarder.
when you populate your List in RecyclerView's adapter what you can check is if the isSelected is true than make respective imageView have a boarder. otherwise don't
by doing so you will be independent of the position of the item with respective of the scroll. So your Item will retain the boarder which was actually selected.

Android Switch Widget: Loses custom thumb image when scrolling

I'm using a ListView with a custom Adapter and custom item layout where every item has a description with a switch.
Depending on what the item is, I give the switch different thumb selector-drawables per code in getView().
swtch.setThumbDrawable(getContext().getResources()
.getDrawable(R.drawable.green_switch)); // or red_switch
This works fine, but:
When I scroll the ListView, the custom images are gone, until I touch a switch again, then the custom image of it comes back immediately.
When the state list contains an element of the “disabled” state at first, the switch will have this image instead of the image it should.
It's either a bug or an improperly use of the ListAdapter by me, I think.
A workaround for me would be to include switches with different styles in the layout and show one of them depending on the item type I have and hide the others.
Is this the only possibility?
Sound like you have problems with view recycling. You have to set all the fields in the getView each time.
Please read How ListView's recycling mechanism works. Understanding view recycling is very important when using ListViews.

Continuously scrolling Gallery?

I have a Gallery object that scrolls from left to right and back again. However, I would like to make this Gallery circle back on itself, that way when I get to my last View, the very first one is next and I can just keep scrolling. Any ideas? Thanks.
Galleries use an Adapter to back the data that they display. You can create a custom adapter where getCount() returns Integer.MAX_VALUE and getView() does a modulos the position with the number of images you have. This way it always returns the appropriate image for a given position.

Categories

Resources