2 markdowneide markdowneide 于 2013.05.28 11:13 提问

在viewpager 上动态的添加和移除视图

在程序中,用户会使用数据的单一视图开启。我想添加一个 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?

2个回答

csh_34
csh_34   2013.05.28 14:57
已采纳

可选择方案:
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>
Rockey723
Rockey723   2013.10.15 14:22

动态加载代码的代码,你能描述清楚不。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!