两侧无限Viewpager图像滑块

时间:2017-07-28 15:55:28

标签: android android-viewpager slider

我想在寻呼机适配器中制作一个Viewpager图像滑块,该滑块可从两侧(从右到左)和(从左到右)滑动。 下面的代码只能从第一个位置开始并向右移动,但我希望它能同时进行,即从任何位置从左到右,从右到左。

公共类ViewPagerAdapter扩展了PagerAdapter {

private Context context;
private LayoutInflater layoutInflater;
private Integer[] images = {R.drawable.cake, R.drawable.cake1, R.drawable.cake2};


public ViewPagerAdapter(Context context) {
    this.context = context;
}


@Override
public int getCount() {
    return Integer.MAX_VALUE;

}

@Override


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

 private int pos = 0;


@Override
public Object instantiateItem(View container, int position) {

    layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = layoutInflater.inflate(R.layout.image_slide, null);
    ImageView imageView = view.findViewById(R.id.imageView);


    imageView.setImageResource(images[pos%images.length]);
    imageView.setPaddingRelative(15, 0, 15, 0);
    ViewPager vp = (ViewPager) container;
    vp.addView(view, 0);


    if (pos >= images.length - 1)
        pos = 0;
    else
        pos++;


    return view;


}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {

    ViewPager vp = (ViewPager) container;
    View view = (View) object;

    vp.removeView(view);
}

}

1 个答案:

答案 0 :(得分:0)