0
我想在瀏覽器適配器中可以從兩側(從右到左)和(從左到右)滑動的Viewpager圖像滑塊。 下面的代碼只能從第一個位置開始並向右移動,但我希望它能夠從兩個方向移動,即從任意位置的左側到右側和右側。無限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);
}
}
但這實現比較複雜。我使用了幻燈片的viewpager,只能從右向左移動一個方向。我希望的輸出是能夠從第一個位置從左向右滑動。它的一個例子是在flipkart應用程序的主頁上。兩種方式都是無限的圖像滑塊。順便說一句,謝謝你的回覆。 –