我正在尝试使用视图寻呼机制作画廊。尽管如此,位置 1 的视图总是被更新,而我正在尝试更新当前页面(默认为 0)。
首先,我创建了一个视图,该视图将被寻呼机充气。该视图由 4 个 ImageView 和 4 个 textview 组成。
然后我为寻呼机创建了一个适配器:
public class LivePagerAdapter extends PagerAdapter {
private CameraList cameras = null;
public LivePagerAdapter(CameraList cams)
{
this.cameras = cams;
}
@Override
public int getCount() {
return this.cameras.size() /4; // 4 imageview in a page
}
public void setCameras(CameraList cams)
{
this.cameras = cams;
}
@Override
public Object instantiateItem(View container, int position) {
LayoutInflater inflater = LayoutInflater.from(container.getContext());
View view = inflater.inflate(R.layout.pager,null);
((ViewPager) container).addView(view,0);
return view;
}
}
最后在我的活动中,我像这样实例化寻呼机:
adapter = new LivePagerAdapter(this.listeCamera);
pager = (ViewPager) findViewById(R.id.camspager);
pager.setAdapter(adapter);
pager.setCurrentItem(0);
之后我用我的 4 imageview 初始化一个数组:
tabImageView.add((ImageView) findViewById(R.id.cam1)); //Imageview located in the view loaded by the pager
tabImageView.add((ImageView) findViewById(R.id.cam2));
tabImageView.add((ImageView) findViewById(R.id.cam3));
tabImageView.add((ImageView) findViewById(R.id.cam4));
并且在需要时我在图像视图中添加图像。
它似乎有效,除了我要求寻呼机从位置 0 开始,这是位置 1 的视图,它用我的图像更新。
当我使用时findViewById(somethingInflatedByThePager)
,我需要它成为当前页面,我该怎么做?