Elias
Elias

Reputation: 472

Disable ItemTouchHelper for a part of a viewholder

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?

Upvotes: 6

Views: 4344

Answers (2)

Elias
Elias

Reputation: 472

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;

    }
});

Upvotes: 6

Ravi Gadipudi
Ravi Gadipudi

Reputation: 1455

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) {

        }
    });

Upvotes: 10

Related Questions