Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 422
Elias Avatar asked Sep 28 '15 07:09

Elias


2 Answers

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

        }
    });
like image 140
Ravi Gadipudi Avatar answered Sep 21 '22 23:09

Ravi Gadipudi


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;

    }
});
like image 41
Elias Avatar answered Sep 18 '22 23:09

Elias