How to swipe xml layouts using ViewPager

后端 未结 2 1645
一个人的身影
一个人的身影 2021-01-25 06:27

I am building an app in which I want to enable swipe. I want to swipe xml layouts but in a single activity. Earlier I tried to put images, it worked. I created an ImageAdapter c

2条回答
  •  清歌不尽
    2021-01-25 07:05

    For your first question, the Infinite Scrolling, I figured out one solution to it.

    Here's how I am doing it:

    No looping is required in this case. No matter what your getCount() is.

    After setting the adapter, I am setting a setOnPageChangeListener to the view pager like this:

    myPager.setOnPageChangeListener(new OnPageChangeListener() {
    
                    @Override
                    public void onPageSelected(int position) {
    //Define the focused page before your onCreate()..private int focusedPage = 0;
                        focusedPage = position;
                    }
                    //We don't have to do anything here.
                    @Override
                    public void onPageScrolled(int arg0, float arg1, int arg2) {
                        // TODO Auto-generated method stub
    
                    }
                    //Here's where the magic is. After you reach the end of the page, you can scroll again and it will move your view to page 1 again.
                    @Override
                    public void onPageScrollStateChanged(int arg0) {
    
                        myPager.setCurrentItem(0,false);
    
                    }
                });
    

    See if this solution works.. It's working perfect for me. .:D

提交回复
热议问题