简体   繁体   English

具有大量视图或动态视图的Viewpager

[英]Viewpager with large number of views or dynamic views

I want to implement a Viewpager with a dynamic view ie the view updates itself when user flicks/drags it left/right.The view is updated using the next row in the pre-stored database.How can i implement it? 我想用动态视图实现Viewpager,即当用户向左/向右滑动/拖动时视图会自动更新。该视图使用预存储数据库中的下一行进行更新。如何实现?
The reason i want to do this is: I have around 3000 entries in db that i want to display one at a time.I can't create so many views at a time. 我要这样做的原因是:我想一次显示一个数据库中大约有3000个条目。我一次不能创建那么多视图。

such as bellow, hope to help you. 如波纹管,希望对您有所帮助。

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;
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM