2013-03-02 78 views
1

有無限制Fragment + ViewPager使用無限滾動嗎?無限滾動使用限制片段

例如:

我要創建10 Fragment,當我滾動到第五Fragment,索引號爲0的片段被破壞並顯示在第十一Fragment

**1** 2 3 4 5 6 7 8 9 10 

滾動後到第五

2 3 4 **5** 6 7 8 9 10 11 

變量

private static final int NUM_PAGES = 10; 

這裏是我的Fragment adapter

這裏是包含Fragment的顯示與數字1

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    ViewGroup rootView = (ViewGroup) inflater.inflate(
      R.layout.fragment_view, container, false); 

    ((TextView) rootView.findViewById(R.id.tvUsername)).setText(
      ""+ (mPageNumber + 1)); 
    return rootView; 
} 

這裏開始是當所選事件發生

mPager.setOnPageChangeListener(new OnPageChangeListener() { 

    @Override 
    public void onPageSelected(int pageSelected) { 
     // TODO Auto-generated method stub 
     Log.d(TAG, "selected = " + pageSelected); 
     if (pageSelected > (NUM_PAGES - 6)) { 
        //Event do it here? 
     } 
    } 

    @Override 
    public void onPageScrolled(int arg0, float arg1, int arg2) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onPageScrollStateChanged(int arg0) { 
     // TODO Auto-generated method stub 

    } 
}); 

問題: 如何創建事件?

我認爲可以通過更改getItem()中的position值來完成。 但我不知道該怎麼做

回答

0

我能想出的唯一方法是隻做三頁,當用戶滾動到最後一頁時,您替換每個片段。事情是這樣的:

陣列

ArrayList<Integer> positions = new ArrayList<Integer>(); 

public void initPositions() { 
    positions.add(0); 
    positions.add(1); 
    positions.add(2); 
} 

適配器

}

聽者

mPager.setOnPageChangeListener(new OnPageChangeListener() { 

    @Override 
    public void onPageSelected(int pageSelected) { 
     //scrolled to first page 
     if(pageSelected == 0) { 
        //Make sure its not unlimited upwards 
        if(positions.get(pageSelected == 0)) 
         return; 
        //remove last and insert new at bottom 
      positions.remove(2); 
      positions.insert(0, positions.get(0) - 1); 
     //scrolled to last page 
     } else if(pageSelected == 2) { 
        //remove first and add one last 
      positions.remove(0); 
      positions.add((positions.get(1) + 1) % NUM_PAGES); 
     } 
      //make sure the adapter understands 
      mAdapter.notifyDataSetChanged(); 
      //move pager to the middle again 
      mPager.setCurrentItem(1); 
    } 

    @Override 
    public void onPageScrolled(int arg0, float arg1, int arg2) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onPageScrollStateChanged(int arg0) { 
     // TODO Auto-generated method stub 

    } 
    }); 

不知道如何做到這一點performancewise工作,或者如果它會 工作在所有,但給它一個:)

+0

謝謝你,我會嘗試 – Sieryuu 2013-03-05 01:13:45

+0

ArrayList沒有insert()函數,我可以知道你是怎麼做到的嗎? – Sieryuu 2013-03-05 01:27:51