ViewPager,一屏显示多个page

原理就一个属性Android:clipChildren=”false”,该属性的意思就是在子View进行绘制时不要去裁切它们的显示范围。

这一篇只是简单的做多page显示,并没有加上特效,是可以加上很多很酷很炫的效果,如果想看可以查看

更多的效果:https://github.com/hongyangAndroid/MagicViewPager

  • XML中
 
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipChildren="false"
    android:id="@+id/rellay"
    >

        <android.support.v4.view.ViewPager
            android:id="@+id/vp"
            android:layout_width="match_parent"
            android:layout_height="140dp"
            android:clipChildren="false"
            android:layout_marginLeft="60dp"
            android:layout_marginRight="60dp"
            android:layout_gravity="center"
            />
RelativeLayout>
  • 在activity中


public class MainActivity extends AppCompatActivity {

    private Uri uri=ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    private ViewPager vp;
    private int[] imgids={R.mipmap.img1,R.mipmap.img2,R.mipmap.img3,R.mipmap.img4,R.mipmap.img5};
    private RelativeLayout rellay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        rellay = (RelativeLayout) findViewById(R.id.rellay);
        vp = (ViewPager) findViewById(R.id.vp);
        vp.setPageMargin(20);
        vp.setOffscreenPageLimit(3);
        vp.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return imgids.length;
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view==object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                ImageView imageView = new ImageView(MainActivity.this);
                imageView.setImageResource(imgids[position]);
                container.addView(imageView);
                return imageView;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View)object);
            }
        });
        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (rellay != null) {
                    rellay.invalidate();
                }
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //将父类的touch事件分发至viewPgaer,否则只能滑动中间的一个view对象
        rellay.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return vp.dispatchTouchEvent(event);
            }
        });

    }
}

你可能感兴趣的:(ViewPager,一屏显示多个page)