I noticed these libraries and was eager to use them in my app.
My RecyclerView
uses StaggeredGridLayoutManager
to organize viewHolders
and I've written down the following code:
ItemTouchHelper helper = new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(
ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.START | ItemTouchHelper.END,
ItemTouchHelper.START | ItemTouchHelper.END) {
@Override
public boolean onMove(RecyclerView recyclerView,
RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
mAdapter.move(viewHolder.getAdapterPosition(), target.getAdapterPosition());
return true;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
mAdapter.remove(viewHolder.getAdapterPosition());
}
});
helper.attachToRecyclerView(mRecyclerView);
There are two problems:
Swiping an item won't change its
alpha
bydeltaX
.Imagine that there are 3 items in the
RecyclerView
(which has 2 columns):- the first is a middle-size one;
- the second is smaller than the first one;
- the third is the biggest.
If I move the third one to the first place, scroll the RecyclerView
down (other items won't influence the first 3 ones) and then up, the second one and the third one will be totally the same.
It seems that I should write a customized ItemTouchHelper
to add alpha
animation to solve the first problem and that I didn't use these classes correctly and thus caused the second problem.
As a result, what is the best sample for using ItemTouchHelper
? Or how to implement a customized one? Thanks in advance for any answers or comments.
According to the comments in
ItemTouchHelper.Callback
you want to override the methodsonChildDraw
oronChildDrawOver
for those dX, dY values.Also see
getDefaultUIUtil
and the implementations ofItemTouchUIUtil
(which is whatonChildDraw
andonChildDrawOver
call). The comment ofgetDefaultUIUtil
demonstrates how to use it in conjunction with overridingonChildDraw
andonChildDrawOver
(even though the examples erroneously return a bool value for being handled).Here's a quick example if you want to add 5 to the default elevation in Lollipop: