I have this RecyclerView with a viewholder that has a title/TextView and a ViewPager.
The items from the RecyclerView can get swiped away with an ItemTouchHelper.
I want to disable the swiping away part for the viewpager but not if you start dragging on the title/TextView.
Anyone who knows how to disable swipe for just a part of a viewholder?
I can disable swiping an entire viewholder with the ItemTouchHelper this way:
ItemTouchHelper()...
@Override
public int getSwipeDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder)
{
if (viewHolder instanceof UnswipeableViewHolder)
{
return 0;
}
}
return super.getSwipeDirs(recyclerView, viewHolder);
But how to disable it for just a part of the ViewHolder?
Try this code
recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
int action = e.getAction();
switch (action) {
case MotionEvent.ACTION_MOVE:
rv.getParent().requestDisallowInterceptTouchEvent(true);
break;
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
});
Solved my problem by adding the requestDisallowInterceptTouchEvent(true) on the child viewpager:
this.viewPager.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View view, MotionEvent motionEvent)
{
int action = motionEvent.getAction();
switch (action)
{
case MotionEvent.ACTION_MOVE:
view.getParent()
.requestDisallowInterceptTouchEvent(true);
break;
}
return false;
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With