In iOS when data changes, i.e. a record is being deleted, calling UITableView, deleteRowsAtIndexPaths method, will show a quick animation and that GUI row will dismiss at the end.
Is it any similar approach in Android? So I do not want just 'rerender' the RecycleView, but 'rerender' only certain rows, and show an 'insert' or 'delete', or 'update' animation on it.
Put this method inside your adapter.
public void removeAt(int position) {
YOUR_LIST.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, YOUR_LIST.size());
}
and call removeAt(POSITION_TO_REMOVE) from where you want to perform delete operation.
It will remove view from RecycleView and animate with slideUp animation.
Related
I have a RecyclerView and I'm trying to implement delete functionality. In order to get the correct positions of the itemViews, I call notifyItemRangeChanged() after deleting. However when I do that the cool animation of the itemView sliding to the right and being deleted is now gone. It looks like the notifyItemRangeChanged() cuts off the animation created by notifyItemRemoved(). Is there any way I can have the animation and the items delete properly?
Activity Code:
circuitsObject?.circuits?.remove(circuitsObject?.circuits?.get(position))
recyclerView.adapter?.notifyItemRemoved(position)
recyclerView.adapter?.notifyItemRangeChanged(position, circuitsObject?.circuits!!.size)
if (recyclerView.adapter?.itemCount!! == 0) {
loadEmptyUI()
}
Adapter Code:
class CircuitViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
...
itemView.setOnClickListener { clickListener(circuit) }
itemView.setOnLongClickListener {
onLongClickListener(position)
true
}
The parameter "position" comes straight from the ViewHolder. To compensate for the fact that the position doesn't match the index of my data I am using "notifyItemRangeChanged". However, doing so gets rid of the animation. So I'm wondering if there's any way to bypass this such that I can still have the proper position logic as well as the animation for delete.
You don't need to call notifyItemRangeChanged(...) after calling notifyItemRemoved(...).
I'm using pagination to get data from network as the only datasource. In the PagedListAdapter I have a button which on click will update the data in the network now I need to get the updated data and change the view for that item. How do I do that?. Is there a way to achieve this without using db?...
lets say I have a Model named Post and it has a field named isSaved. so what I'm doing rt now is when I click the save button in the PagedListAdapter view then I'm calling an api to update that field ..so now I want to get the updated value for that particular post item and change the button's text as saved....since i'm using adapter.submit(listOfPost), the list is the same so DiffUtil itemCallback isn't being called...What should I do??
Suppose when you pass page 1 then take example its returning you 30 items. So all you have to check here is whether recyclerview can scroll vertically or not. If not then increase the page number and call api method again.
And to check whether recyclerView have more item to scroll below method will help you.
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(#NonNull RecyclerView recyclerView, int newState) {
if(!mRecyclerView.canScrollVertically(1)){
// search the next page
searchNextPage();
}
}
});
For more details this link will be helpful for you: https://github.com/mitchtabian/RestApiMVVM
I have a RecyclerView with its RecyclerView.Adapter and view holder. I am trying to delete an item from list, code as follows inside onClick() on delete button in the ViewHolder
int position = getAdapterPosition();
if(position > -1)
{
Place place = placeList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, getItemCount());
}
Despite removing the view and doing the animation (list also gets affected), the old view (or lower one) still exist or drawn again.
For example, if the list starts with size = 5, then i try to remove index 4, he remove 4, then still draw 5 views.
EDIT
If i remove notifyItemRangeChanged() then it does that bug only if i do the following
1- click on delete
2- click button very quickly that takes me to new view
3- going back to the list where i can delete
4- start deleting, and bug happens. 1 item still remain even though the List size = 0 (getItemCount is called with 0).
If i only call NotifyDataSetChanged(), then it removes item, but view just stays there !!
Any help or suggestion is appreciated.
Thanks.
EDIT complete class LINK
Try this:
placeList.remove(position);
notifyItemRemoved(position);
use below code it will solve your problem.
holder.deleteImg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(list.size()!=0){
list.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position,list.size());
}
}
});
I've had the same problem, notifyItemRangeChanged() call didn't help while notifyDataSetChanged() did (though stopped the animation). But I am using the ItemTouchHelper on the RecyclerView (to allow for moving items around), and it became obvious that this class was causing the trouble.
The only difference in my case is that to reproduce this overlap-after-delete bug, user has to long press on an item while deleting it.
After I modified the overriden isLongPressDragEnabled() method of ItemTouchHelper.Callback to return false instead of true, the issue was solved.
lastImages.remove(position); (lastImages equals your array list)
newContentAdapter.notifyDataSetChanged();
it is works. You have to remove it in your array not item. then notify adapter. Thats all
for me notifyItemRemoved and notifyDataSetChanged doesnt work. I tried everything , only solution that worked for me is calling or reloading recycler view inside onSwiped method!
you don't have to create new reference of place list.that need to declared as array list. you can't add or remove a object from list. you can do that only with arraylist.
change this lines
private List<Place> placeList;
Place place = placeList.remove(position);
to
private ArrayList<Place> placeList;
// other codes
placeList.remove(position);
// print placeList to confirm
notifyItemRemoved(position);
I had this issue
when I'm using notifyDataSetChanged() it works fine but I wanted the animation so I used notifyItemRemoved(position) for animation then I used notifyDataSetChanged() but I delayed the interval between those functions
I made extension for it you can use it on any RecylcerView
fun RecyclerView.removeItem(position: Int){
adapter?.notifyItemRemoved(position)
handler.postDelayed({
adapter?.notifyDataSetChanged()
},300)
}
You need to update you adapter;
Create method in Adapter class where you will be put your data.
For example : setData(List<Place> data);
When last item in list you need to write: adapter.setData(null);
I had the same problem. Just set list = null. It's all.
For instance, you have an adapter and in onBindViewHolder method you set OnClickListener to some views (and do some actions there depending on view position). You should assign final to position param of method onBindViewHolder so it could be accessible from onClick().
After changing dataset (remove or add item in list) you call onItemInserted or onItemRemoved and this really adds/removes a view in the recyclerview, BUT it does not refresh other viewitems so when you click on a neighbor viewitem it will open a screen or show data with invalid index. To avoid this I basically call notifyDatasetChanged to call onBind to all visible views and remove/add some views.
So how to refresh other views when you call notifyItemInserted/removed or how to work with these methots appropriately?
Assigning the position to a variable in onBindViewHolder will lead to an inconsistent state if items in the dataset are inserted or deleted without calling notifyDataSetChanged.
To use onItemInserted or onItemRemoved the data in the viewholder should remain consistent since it will not be redrawn and onClick would use this invalid position assigned before an item was added or removed.
For this and other use cases the RecyclerView.ViewHolder provides methods to access its position and id:
Use getAdapterPosition() or getItemId() to get valid positions and ids.
Also have a look on the other methods available in RecyclerView.ViewHolder.
So, the way I fix the problem I had is by changing the position into viewHolder.getAdapterPosition()
Cheers!
I advise you to add notifyItemRangeChanged after insert or remove list inside adapter. This work for my project.
Example in remove item :
public void removeItem (int pos) {
simpanList.remove(pos);
notifyItemRemoved(pos);
notifyItemRangeChanged(pos, simpanList.size());//add here, this can refresh position cmiiw
}
For future readers, this is what I do when inserting/removing in recyclerview
For example, my model class is CarsModel
In my adapter
ArrayList<CarsModel> carsModel;
In onBindViewHolder
CardModel model = carsModel.get(position);
When removing data in list using button in holder:
int position = holder.getAdapterPosition();
carsModel.remove(position);
notifyItemRemoved(position);
Then when inserting
carsModel.add(0, model);
notifyItemInserted(0);
or insert in last row
carsModel.add(carsModel.size() - 1 , model);
notifyItemInserted(carsModel.size()-1);
I need to be able to programmatically dismiss an item inside a RecyclerView without the user actually swiping (instead I want to dismiss the item when they tap a button in the card). A lot of libraries I've seen only seem to support actual swipes.
I've tried using an existing library and just simulate a MotionEvent by creating a swipe on my own programmatically, but this interferes with another horizontal-swipe listener, so I'm wondering how else this could be done, ideally for a RecyclerView but if anyone knows how to for a ListView instead I can try to adapt that.
I've looked at this library as well as others for inspiration but I can't figure out how to trigger the swipes programmatically instead.
Use a ListView or RecyclerView with custom adapter, and call notifyDataSetChanged after removing an item from the datalist:
private void removeListItem(View rowView, final int position) {
Animation anim = AnimationUtils.loadAnimation(this,
android.R.anim.slide_out_right);
anim.setDuration(500);
rowView.startAnimation(anim);
new Handler().postDelayed(new Runnable() {
public void run() {
values.remove(position); //Remove the current content from the array
adapter.notifyDataSetChanged(); //Refresh list
}
}, anim.getDuration());
}
Use one of the libraries that offer the swipe to dissmis funcionality ad extract the animation part, if im not mistaken its at the action_up at the onTouch(). Then call it from your onClick of the button.