问题
I'm working on an app for playing audio files. Created a tab layout with 2 tabs. Used fragment in both of them with RecyclerView being used in both of them. The fragment named LibraryFragment has this RecycleView whose items when Swipped must pass on to the HomeFragment, that's what I wanna do. But even though swiping animation is happening, OnSwipe method doesn't works at all.
This is the method I've used to provide the swiping function to my recyclerview's items under the LibraryFragment. Under OnCreateView method I call this initSwipe() method.
private void initSwipe(){
ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT ) {
private boolean swipeBack = false;
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
int position = viewHolder.getAdapterPosition();
if (direction == ItemTouchHelper.LEFT){
HomeFragment.audioList.clear();
HomeFragment.audioList.add(audioList.get(position));
HomeFragment.libraryAdapter.notifyDataSetChanged();
Log.e(TAG, "onSwiped: " );
}
}
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView,
RecyclerView.ViewHolder viewHolder, float dX, float dY,
int actionState, boolean isCurrentlyActive) {
recyclerView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL)
{
swipeBack = true;
}
else
{
swipeBack = false;
}
return false;
}
});
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
@Override
public int convertToAbsoluteDirection(int flags, int layoutDirection) {
int value = swipeBack ? 0 : super.convertToAbsoluteDirection(flags, layoutDirection);
swipeBack = false;
return value;
}
};
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
itemTouchHelper.attachToRecyclerView(libraryRecyclerView);
}
please help out.
回答1:
The Event onSwiped() will be triggered when the item will be completely swiped out. Since you're holding the item view, this never happen
Here you can find an amazing tutorial where you can achieve this.
来源:https://stackoverflow.com/questions/46654609/onswipe-method-not-working-in-recyclerview