FragmentPagerAdapter Swipe to show ListView 1/3 Screen Width

前端 未结 6 774
走了就别回头了
走了就别回头了 2020-12-23 22:18

EDIT: See my answer below-->

I am wanting to have a view that when swiped to the right, the listView is shown. Very much similar to what is implemen

6条回答
  •  无人及你
    2020-12-23 22:55

    With a little Trick, the behavior can be achieved with the ScrollView-Behavior inside the ViewPager. If you only want to restrict the area of the most left fragment, you can restrict the scroll limits of the ScrollView.

    In your case: in the onPageChangeListener of the ViewPager do something like that:

    @Override
    public void onPageScrollStateChanged(int arg0) {
        restrictLeftScroll();
    }
    
    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        restrictLeftScroll();
    }
    
    @Override
    public void onPageSelected(int arg0) {
    
    }
    
    private void restrictLeftScroll() {
    
        if (display != null) {
    
            /* get display size */
            Point size = new Point();
            display.getSize(size);
    
            /* get desired Width of left fragment */
            int fragmentWidth = getResources().getDimensionPixelSize(R.dimen.category_fragment_width);
    
            if (mViewPager.getScrollX() < size.x - fragmentWidth) {
                mViewPager.scrollTo(size.x - fragmentWidth, mViewPager.getScrollY());
            }
        }
    }
    

    This piece of code worked for me without problems. ;)

提交回复
热议问题