Adding Custom Animation when Removing Item RecyclerView - android

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.

Related

How does recyclerView.scheduleLayoutAnimation() work?

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

Focus lost on refresh in recyclerview

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.

How to programmatically initiate a swipe to dismiss with RecyclerView

When I delete an item from a RecyclerView I want to show a "swipe" animation, that is that the item is moved sideways off the screen. I believe there is support for "swipe to dismiss" using ItemTouchHelper, but this is touch-initiated whereas I want to be able to initiate the swipe programmatically.
I have also tried setting the RecyclerView item animator by extending DefaultItemAnimator. Using this method I can get items to swipe left and right but unfortunately the gap in the list closes quickly such that the swipe does not finish before the list item gap has closed.
Anyone know how to do this?
You can use ItemAnimators offered by this library. More specifically, your would use their SlideInLeftAnimator or SlideInRightAnimator.
Although it's already answered with external library; probably someone wants to do it with raw java.. I have done it with the solution in here
The idea is that, you take off the list item view of the RecyclerView to be deleted and animate it; and normally remove it from the adapter
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());
}

Header in navigation view is clickable. How to disable it?

I'm trying to disable the "touch highlight animation" when touching the header. onNavigationItemSelected() is correctly only called when a menu item is touched. But when I touch the header, there is an animation.
I've tried setEnabled(false), setClickable(false) setLongClickable(false), setAnimation(null), setFocusable(false), but it still showing. I cannot find a methods for manipulating the header. (other than addHeader)
Any ideas? Thanks
It seems that updating to the latest version of the design library allowed for the following:
View headerView = LayoutInflater.from(getActivity()).inflate(R.layout.header_view, mNavigationView, false);
headerView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Consume input from header view. This disables the unwanted ripple effect.
}
});
I am quite confident that I tried this without luck before updating, though.

Only relayout children and not all the tree

I'm trying to recreate ListView (and AbsListView) logic, with view recycling.
I need this but we can say it's only for understand Android logic.
Suppose my children items are the same (same layout), using fixed height RelativeLayout.
During scrolling, I'm reusing ghost children view and set properties for current item.
It's working fine, since I'm using View.offsetTopAndBottom() and invalidate() instead of requesting layout during scroll for optimization.
My problem is updating the children (RelativeLayout).
Depends of item, I want to hide or show ImageView on this item. For that, I'm just using iconImage.setVisibility( GONE ) and iconImage.setVisibility( VISIBLE ).
Since I'm blocking requestLayout, it seems to be setVisibility() does not work properly.
If I use requestLayout, all the tree will measure and layout itself, and it's not a good way for a scrolling user experience.
Is there a way for only request layout on recycle child item ?
You need to notify your listView with changed of view so call notifyDataSetChanged(); from Uithread like this :
final ArrayAdapter adapter = ((ArrayAdapter) getListAdapter());
runOnUiThread(new Runnable() {
#Override
public void run() {
adapter.notifyDataSetChanged();
}
});

Categories

Resources