在程序中,用戶會使用數據的單一視圖開啟。我想添加一個 ViewPager ,並且允許用戶添加所要求的更多的視圖。如何實現?但是我不想使用 FragmentPagerAdapter。
MainActivity 創建一個 ViewPager 和 PagerAdapter:
ViewPager pager = null;
MainPagerAdapter adapter = null;
public void onCreate (Bundle savedInstanceState)
{
super.onCreate (savedInstanceState);
pager = new ViewPager (this);
setContentView (pager);
adapter = new MainPagerAdapter();
pager.setAdapter (adapter);
View v0 = code_to_create_initial_view();
adapter.add (v0, 0);
}
使用一個 PagerAdapter 來提供視圖集。我需要添加和移除視圖。 ViewPager的改變:
class MainPagerAdapter extends PagerAdapter
{
// 這包含了所有目前可顯示的視圖,從左到右的順序
private ArrayList<View> views = new ArrayList<View>();
public void addView (View v, int position)
{
views.add (position, v);
}
public void removeView (int position)
{
views.remove (position);
}
}
此外,我需要實現下面的虛擬方法。
public object instantiateItem (ViewGroup pager, int position);
public void destroyItem (ViewGroup, int, Object);
public int getCount ();
public boolean isViewFromObject (View, Object);
ViewGroup params是用來做什麼的?是包含 ViewPager 嗎?
isViewFromObject是用來做什麼的呢?一個對象和一個視圖如何結合?
當添加或刪除視圖時,是否要調用 startUpdate 和 finishUdate?
可選擇方案:
My adapter:
private class PagerAdapter extends FragmentPagerAdapter implements
ViewPager.OnPageChangeListener, TabListener {
private List<Fragment> mFragments = new ArrayList<Fragment>();
private ViewPager mPager;
private ActionBar mActionBar;
private Fragment mPrimaryItem;
public PagerAdapter(FragmentManager fm, ViewPager vp, ActionBar ab) {
super(fm);
mPager = vp;
mPager.setAdapter(this);
mPager.setOnPageChangeListener(this);
mActionBar = ab;
}
public void addTab(PartListFragment frag) {
mFragments.add(frag);
mActionBar.addTab(mActionBar.newTab().setTabListener(this).
setText(frag.getPartCategory()));
}
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
@Override
public int getCount() {
return mFragments.size();
}
/** (non-Javadoc)
* @see android.support.v4.app.FragmentStatePagerAdapter#setPrimaryItem(android.view.ViewGroup, int, java.lang.Object)
*/
@Override
public void setPrimaryItem(ViewGroup container, int position,
Object object) {
super.setPrimaryItem(container, position, object);
mPrimaryItem = (Fragment) object;
}
/** (non-Javadoc)
* @see android.support.v4.view.PagerAdapter#getItemPosition(java.lang.Object)
*/
@Override
public int getItemPosition(Object object) {
if (object == mPrimaryItem) {
return POSITION_UNCHANGED;
}
return POSITION_NONE;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) { }
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) { }
@Override
public void onPageScrollStateChanged(int arg0) { }
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) { }
@Override
public void onPageSelected(int position) {
mActionBar.setSelectedNavigationItem(position);
}
/**
* This method removes the pages from ViewPager
*/
public void removePages() {
mActionBar.removeAllTabs();
//call to ViewPage to remove the pages
vp.removeAllViews();
mFragments.clear();
//make this to update the pager
vp.setAdapter(null);
vp.setAdapter(pagerAdapter);
}
}
動態的添加和刪除代碼:
<code>
//remove the pages. basically call to method removeAllViews from ViewPager
pagerAdapter.removePages();
pagerAdapter.addPage(pass your fragment);</code>