Android ViewPager - Show preview of page on left and right

后端 未结 10 2515
旧巷少年郎
旧巷少年郎 2020-12-02 03:52

I\'m using Android\'s ViewPager. What I want to do is to show a preview of the page on both the left and the right. I\'ve seen where I can use a negative

相关标签:
10条回答
  • 2020-12-02 04:39

    Use this fragment adapter and class to view the viewpager in left and right scroll.add necessary classes to scroll to view next pages.

    package com.rmn.viewpager;
    
    import java.util.List;
    
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentPagerAdapter;
    
    /**
     * The <code>PagerAdapter</code> serves the fragments when paging.
     * @author mwho
     */
    public class PagerAdapter extends FragmentPagerAdapter {
    
        private List<Fragment> fragments;
        /**
         * @param fm
         * @param fragments
         */
        public PagerAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            this.fragments = fragments;
        }
        /* (non-Javadoc)
         * @see android.support.v4.app.FragmentPagerAdapter#getItem(int)
         */
        @Override
        public Fragment getItem(int position) {
            return this.fragments.get(position);
        }
    
        /* (non-Javadoc)
         * @see android.support.v4.view.PagerAdapter#getCount()
         */
        @Override
        public int getCount() {
            return this.fragments.size();
        }
    }
    
    
    
    package com.manishkpr.viewpager;
    
    
    import android.content.Context;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentPagerAdapter;
    
    public class ViewPagerAdapter extends FragmentPagerAdapter {
        private Context _context;
    
        public ViewPagerAdapter(Context context, FragmentManager fm) {
            super(fm);  
            _context=context;
    
            }
        @Override
        public Fragment getItem(int position) {
            Fragment f = new Fragment();
            switch(position){
            case 0:
                f=LayoutOne.newInstance(_context);  
                break;
            case 1:
                f=LayoutTwo.newInstance(_context);  
                break;
            }
            return f;
        }
        @Override
        public int getCount() {
            return 2;
        }
    
    }
    
    0 讨论(0)
  • 2020-12-02 04:49

    if someone still looking for solution, I had customized the ViewPage to achieve it without using negative margin, find a sample project here https://github.com/44kksharma/Android-ViewPager-Carousel-UI it should work in most cases but you can still define page margin with mPager.setPageMargin(margin in pixel);

    0 讨论(0)
  • 2020-12-02 04:49

    Note that when setting none zero paddings to a viewpager, the viewpager's edgeeffect placement is messed up, this is a bug of the viewpager, see here:viewpager edgeeffect bug

    You can either disable the edge effect by setting android:overScrollMode="never" to the viewpager, or you can fix the bug with a slightly modified version of the EdgeEffect class:ViewPagerEdgeEffect fix

    0 讨论(0)
  • 2020-12-02 04:51
     override fun getPageWidth(position: Int): Float {
        return 0.7f
    }
    

    Add this in adapter class file, This worked for me

    0 讨论(0)
提交回复
热议问题