我實現了由3個片段(0,1,2)組成的ViewPager。當我做setCurrentItem(1)時,它顯示好的片段,但不刷新片段。 如果我從標籤頁3手動滑動到標籤頁2,它會很好地刷新。 你的幫助將非常感激,我從一個星期以來卡住了。ViewPager CurrentItem片段不刷新
Oncreate()
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(mViewPagerAdapter);
pager.setCurrentItem(1,true);
mViewPagerAdapter.notifyDataSetChanged();
pager.setOnPageChangeListener(mPageChangeListener);
}
private FragmentPagerAdapter mViewPagerAdapter = new
FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public int getCount() {
return TOTAL_PAGES;
}
// Return the Fragment associated with a specified position.
@Override
public Fragment getItem(int position) {
if (position == 0) {
return client_Option_fragment.newInstance();
} else if (position == 1) {
return Profil_fragment.newInstance();
} else if (position == 2) {
return client_Partenaire_fragment.newInstance();
}
return null;
}
@Override
public void destroyItem(android.view.ViewGroup container, int position,
Object object) {
super.destroyItem(container, position, object);
};
@Override
// To update fragment in ViewPager, we should override getItemPosition()
method,
// in this method, we call the fragment's public updating method.
public int getItemPosition(Object object) {
if (object instanceof client_Option_fragment) {
((client_Option_fragment) object).update();
}
if (object instanceof Profil_fragment) {
((Profil_fragment) object).update();
}
if (object instanceof client_Partenaire_fragment) {
((client_Partenaire_fragment) object).update();
}
return super.getItemPosition(object);
};
};
private ViewPager.OnPageChangeListener mPageChangeListener = new
ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int
positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
mViewPagerAdapter.notifyDataSetChanged();
}
@Override
public void onPageScrollStateChanged(int state) {
}
};
編輯: 就找到了解決辦法,我添加下面的代碼片段中OnPageScrolled():在viewpager
if (start){
mViewPagerAdapter.notifyDataSetChanged();
start=false;
}
看一看到下面的網址方案http://stackoverflow.com/a/26517867/156790](http://stackoverflow。 com/a/26517867/156790)[http://stackoverflow.com/a/26517867/1567907](http://stackoverflow.com/a/26517867/1567907) –