ViewPager 两边显示部分其他页

主要就是设置margin,Viewpager父节点设置   android :clipChildren= "false"

viewpager.setpagerMargin();



代码

public class MainActivity extends AppCompatActivity {
    ViewPager vp;
    int colors[]=new int[]{Color.BLACK,Color.BLUE,Color.GRAY,Color.GREEN,Color.RED};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vp= (ViewPager) findViewById(R.id.vp);
        vp.setPageMargin(30);//设置间距
        vp.setOffscreenPageLimit(3);
        vp.setAdapter(new Adapter());

    }

    class Adapter extends PagerAdapter{
        @Override
        public int getCount() {
            return colors.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.setBackgroundColor(colors[position]);
            container.addView(imageView);


            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
           // super.destroyItem(container, position, object);
            container.removeView((View) object);
        }


    }
}





   

   







你可能感兴趣的:(Andriod)