我想用动态视图实现一个Viewpager,即当用户向左/右滑动/拖动时视图会自动更新。视图使用预存数据库中的下一行进行更新。如何实现它?登记/> 我想这样做的原因是:我在db中有大约3000个条目,我想一次显示一个。我不能一次创建这么多的视图。
答案 0 :(得分:1)
public class BottomAdatper extends PagerAdapter {
@Override
public int getCount() {
return 10000;
}
@Override
public boolean isViewFromObject(View view, Object o) {
return view == o;
}
ArrayList<TextView> caches = new ArrayList<>();
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
caches.add((TextView) object);
container.removeView((View) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
TextView tv = null;
if (caches.size() == 0) {
tv = new TextView(container.getContext());
tv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
tv.setGravity(Gravity.CENTER);
tv.setTextSize(60);
tv.setBackgroundColor(Color.YELLOW);
} else {
tv = caches.remove(0);
}
tv.setText("" + position);
container.addView(tv);
return tv;
}
}