How to force ViewPager to re-instantiate its items(如何强制 ViewPager 重新实例化其项目)
问题描述
我正在使用 ViewPager
允许用户在其视图之间滑动.有没有办法强制此 ViewPager
重新加载/重新实例化其视图,以防它们不再有效或需要刷新?我试图在其适配器上调用 notifyDataSetChanged()
但这不会再次调用 instantiateItem()
方法.
I am using ViewPager
to allow user to swipe between its views. Is there a way how to force this ViewPager
to reload/re-instantiate its views in case that they are no longer valid or needs to be refreshed? I tried to call notifyDataSetChanged()
on its adapter but this does not invoke instantiateItem()
method again.
这是从 ViewPager 及其适配器定义扩展而来的类.Bellow 是我想强制刷新项目时调用的 refresh()
方法.
Here is the class that extends from ViewPager and its adapter definision. Bellow is the refresh()
method that I call when I want to force to refresh items.
public class DayFlipper extends ViewPager {
public class FlipperAdapter extends PagerAdapter {
@Override
public int getCount() {
return DayFlipper.DAY_HISTORY;
}
@Override
public void startUpdate(View container) {
}
@Override
public Object instantiateItem(View container, int position) {
Log.d(TAG, "instantiateItem(): " + position);
Date d = DateHelper.getBot();
for (int i = 0; i < position; i++) {
d = DateHelper.getTomorrow(d);
}
d = DateHelper.normalize(d);
CubbiesView cv = new CubbiesView(mContext);
cv.setLifeDate(d);
((ViewPager) container).addView(cv, 0);
// add map
cv.setCubbieMap(mMap);
cv.initEntries(d);
return cv;
}
@Override
public void destroyItem(View container, int position, Object object) {
((ViewPager) container).removeView((CubbiesView) object);
}
@Override
public void finishUpdate(View container) {
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((CubbiesView) object);
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
}
}
...
public void refresh() {
getAdapter().notifyDataSetChanged();
}
}
推荐答案
我找到了解决方案.这只是我的问题的一种解决方法,但目前是唯一的解决方案.
I have found a solution. It is just a workaround to my problem but currently the only solution.
ViewPager PagerAdapter 不更新视图
public int getItemPosition(Object object) {
return POSITION_NONE;
}
有人知道这是否是一个错误吗?
Does anyone know whether this is a bug or not?
这篇关于如何强制 ViewPager 重新实例化其项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何强制 ViewPager 重新实例化其项目


基础教程推荐
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01