r0adkll / Slidr

Easily add slide to dismiss functionality to an Activity
Apache License 2.0
2.68k stars 389 forks source link

Slidr and ViewPager conflict #62

Open 2hamed opened 6 years ago

2hamed commented 6 years ago

Hi there,

There is a problem with using Slidr with a ViewPager in which Slidr intercepts touch events from ViewPager making it impossible for ViewPager to swipe left.

Slidr should consider the ViewPager and lock the dragging when the ViewPager is not at its first page.

GGLabCenter commented 6 years ago

Hello, this is how I solved this problem (it's working):

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if(position == 1)
                    enableSliding(false);
                else 
                    enableSliding(true);
            }
            @Override
            public void onPageSelected(int position) {
                if(position == 1)
                    enableSliding(false);
                else 
                    enableSliding(true);
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

public void enableSliding(boolean enable){
        if (enable)
            slidrInterface.unlock();
        else
            slidrInterface.lock();
    }

slidrInterface is the interface obtained from the method of the library. This is how I use it with two tabs of a tablayout + viewpager. All perfectly working. If you have morethan two tabs you have only to do some little edit with the position variable.

2hamed commented 6 years ago

@GGLabCenter What about nested ViewPagers or ViewPagers inside fragments or horizontal RecyclerViews. I suspect this problem has to be solved at the library level. For example one should do a view traversal inside tryCaptureView and check if there is any horizontally scrollable view inside the sliding parent.

GGLabCenter commented 6 years ago

Hello @2hamed , nested viewpagers: you should look for some function available to set the listening view (child/parent) from the user. Then it's the same code as above;

viewpagers inside fragments: same code as above, i used also in this way. the viewpager in fact is a statefragmentpageradapter that shows the current fragment, you can build your login from that;

I did not try the library functions inside a recyclerview. You could try to disable the interface inside the recyclerview's listener and before exiting from the listener you can reactivate it.

I believe in same way you can avoid to modify the library and achieve the same results. That said, you could clone the main repository and modify the library, then submit a pull request to the creator. In this way an eventual bug could be fixed :-)