Could someone explain me how recyclerView.scheduleLayoutAnimation() method works, and how to use it?
Per the documentation: https://developer.android.com/reference/android/view/ViewGroup#scheduleLayoutAnimation()
public void scheduleLayoutAnimation ()
Schedules the layout animation to be played after the next layout pass of this view group. This can be used to restart the layout animation when the content of the view group changes or when the activity is paused and resumed.
A typical use is to re-introduce the animation when you refresh the items in your recycler view.
Usage:
myAdapter.notifyDataSetChanged();
recyclerView.scheduleLayoutAnimation();
Related
I would like to show a custom animation to match the other animations that I have in my RecyclerView. I can’t find a straight forward way of adding custom animations for removal. I know that when RecyclerView replaced ListView one of the talking points was the ability to know when items are added or removed rather than simply saying the dataset has changed and updating everything, so I figured there would be an easy way to add an animation to a View when it is removed that I am missing.
When I add an animation to a view that is removed the animation will not play. This is likely because the View is removed and the animation stops. Is there an easy way to add an animation to a View that doesn’t get cut off? I'm assuming that I could use a second thread, but I want to know if I am missing something.
//code for removal
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//animation to be added here on view
list.remove(holder.getAdapterPosition());
notifyItemRemoved(position);
notifyItemRangeChanged(position,list.size()-position);
}
});
All add/change/delete/move animations in recyclerView are made by ItemAnimator (by default animator is DefaultItemAnimator). So if you need custom delete animation you should provide custom ItemAnimator via recyclerView.setItemAnimator(animator) method.
I suggest to extend from SimpleItemAnimator and override animateRemove method.
I have this type of activity layout
In my layout, the yellow portion is my recyclerview, which is currently working as the expandable list.
Now in my scenario, when the user clicks on any item in the recyclerview than the layout changes in the activity shown by, green area and its child in the image.
Is there any way to do so?
Here is the simple example of pass value from recyclerview to activity using eventbus. Try this maybe will help you to solve your problem.
You can use callback method.On click of item update UI using callback method.
Yes you can achieve this easily by using EventBus.
guys I am developing android TV app so I used recyclerview horizontally and vertically and I used a method to refresh adapter of vertical recyclerview by using adapter.notifyDataSetChanged() but I am getting one problem.
1. It's focus is being gone and on press any D-pad key it is not working only right key is working
2. I used adapter.notifyItemRangeChanged(0, categoryDataList.size()) but I have still one problem that it's focus is going on first element of layout otherwise last of layout.
So Please help me as soon as possible.
Thanks in advance.
Of course item will lose focus. Because no view to get focus when you refresh RecyclerView by calling method notify*Changed.
There is an imperfect way to keep focus in RecyclerView when you call notifyDatasetChanged().
Override method getItemId(), give a stable id to each item:
#Override public long getItemId(int position) { return position; }
Set has StableId:
adapter.setHasStableIds(true);
Now basically you can keep focus in a concrete item when call notifyDatasetChange, if not, disable animator:
mRecyclerView.setItemAnimator(null);
about stable id:Android: How to make an adapter with stable ids?
about disable animator:How to implement ItemAnimator of RecyclerView to disable the animation of notifyItemChanged
about the reason of step3 in google code:
https://code.google.com/p/android/issues/detail?id=204277
Good luck!
Use
notifyItemRangeInserted(position,size);
if you are inserting items.
I have already know we can set item's animation by call RecyclerView's setItemAnimator method. After that, we can call notifyItemRangeRemoved to let all the items "fly away" together with animation, here is the remove code.
int removedSize = dataCollection.size();
dataCollection.clear();
notifyItemRangeRemoved(0, removedSize);
However, my requirement is to remove item one by one and with some delay. I have try to use Timer + TimerTask + Handler classes to achieve it, however, each time I called notifyItemRemoved method, the RecyclerView will auto refill the item which have already disappear.
How can remove one item, and the blank place will not be replaced with another item directly?
Try to use notifyDataSetChanged() instead of notifyItemRangeRemoved()
I have implemented a RecyclerView and I have set it up to use CAB. But how can I highlight the selected items? If a certain position I checked I stored in a SparseBooleanArray.
My first thought was to store the specific View containg all the elements in my ViewHolder and then in onBindViewHolder set the background somehow to: ?android:attr/activatedBackgroundIndicator
But how can I do that? Is that a useful approach?
I finally solved this by simply adding some minor things:
First of all, the items of the RecyclerView have to use this as background:
android:background="?android:attr/activatedBackgroundIndicator"
Then for the RecyclerView simply call:
setSelected(true); on the indiviual views.
If you want to change the View itself, you need to dispatch adapter.notifyItemChanged(position) and in return, recycler view will call onBind method where you can set the background.
If you don't need to update the view itself, I would suggest using an item decorator.