[英]How to add nextset of fragments without disturbing current fragments in FragmentPagerAdapter?
這是第一次添加片段的代碼。
`if(mcursor == null){項目=(ArrayList)cnews.getItems(); mcursor = cnews.getNextPageToken();
for (int i = 0; i < items.size(); i++) {
Fragment cm = ContentFragment.newInstance(i, items.get(i).getTitle(),
items.get(i).getImageuri(), items.get(i).getDate(), items.get(i).getPublsher(), items.get(i).getDescription());
frags.add(cm);
}
viewPager.setAdapter(new ContentfragmentAdapter(getSupportFragmentManager(), frags));
}else{
items = (ArrayList<News>) cnews.getItems();
mcursor = cnews.getNextPageToken();
for (int i = 0; i < items.size(); i++) {
Fragment cm = ContentFragment.newInstance(i, items.get(i).getTitle(),
items.get(i).getImageuri(), items.get(i).getDate(), items.get(i).getPublsher(), items.get(i).getDescription());
frags.add(cm);
}
在其他情況下,我想在服務器調用后添加更多片段。如何在不刪除舊片段的情況下更新適配器?
請幫助我,我是android開發的新手。 感謝Adavance。
這里的關鍵部分之一是您必須致電
notifyDataSetChanged()
適配器上的數據,以便相應地更新基礎數據結構,包括觸發ViewPager的重繪。 同樣不要忘記, getCount()
還必須返回期望的元素數量(即,此處沒有硬編碼值)
public class SectionsPagerAdapter extends FragmentPagerAdapter {
List<String> tagList;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
tagList = new ArrayList<>();
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
return PlaceholderFragment.newInstance(tagList.get(position));
}
@Override
public int getCount() {
return tagList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return "SECTION "+(position+1);
}
public void add(int position, String key)
{
tagList.add(position, key);
notifyDataSetChanged();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.