如何强制 ViewPager 重新实例化其项

我使用 ViewPager允许用户在其视图之间滑动。有没有一种方法可以强制这个 ViewPager重新加载/重新实例化它的视图,以防它们不再有效或者需要刷新?我尝试在它的适配器上调用 notifyDataSetChanged(),但这不会再次调用 instantiateItem()方法。

剪辑

下面是从 ViewPager 及其适配器定义扩展的类。Bellow 是我想强制刷新项时调用的 refresh()方法。

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();
}
}
151560 次浏览
public class DayFlipper extends ViewPager {


private Flipperadapter adapter;
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);
adpter = FlipperAdapter.this;
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() {
adapter().notifyDataSetChanged();
}
}

试试这个。

我已经找到了一个解决方案。这只是一个解决我的问题的办法,但目前唯一的解决方案。

ViewPager PagerAdapter 没有更新视图

public int getItemPosition(Object object) {
return POSITION_NONE;
}

有人知道这是不是窃听器吗?

有同样的问题。对我来说,它工作打电话

viewPage.setAdapter( adapter );

再次导致重新实例化页面。