【问题标题】:Adding fragment and set it as first position in ViewPager添加片段并将其设置为 ViewPager 中的第一个位置
【发布时间】:2015-11-09 19:01:57
【问题描述】:

我正在向 ViewPager 添加一个片段。如果我想创建和添加另一个片段,我只需在 ViewPagerAdapter 自定义类上调用 notifyDataSetChanged() 方法并更改返回项目的数量。效果很好..

问题是我希望每个新片段都出现在 ViewPager 的第一个位置,而不是最后一个。比如目前是这样的:

  • 片段1 ->添加Fragment2
  • 片段1,片段2 ->添加Fragment3
  • 片段1、片段2、片段3

我希望在 ViewPager 中是这样的:

  1. 片段1 ->添加Fragment2
  2. 片段2,片段1 ->添加Fragment3
  3. 片段3、片段2、片段1

如何使用 viewpager 实现这一点?

【问题讨论】:

    标签: android android-viewpager


    【解决方案1】:

    你应该看看this answer.

    基本上你需要扩展一个PagerAdapter 并公开该方法以在特定位置添加元素。

    class MainPagerAdapter extends PagerAdapter
    {
      // This holds all the currently displayable views, in order from left to right.
      private ArrayList<View> views = new ArrayList<View>();
    
      //-----------------------------------------------------------------------------
      // Used by ViewPager.  "Object" represents the page; tell the ViewPager where the
      // page should be displayed, from left-to-right.  If the page no longer exists,
      // return POSITION_NONE.
      @Override
      public int getItemPosition (Object object)
      {
        int index = views.indexOf (object);
        if (index == -1)
          return POSITION_NONE;
        else
          return index;
      }
    
      //-----------------------------------------------------------------------------
      // Used by ViewPager.  Called when ViewPager needs a page to display; it is our job
      // to add the page to the container, which is normally the ViewPager itself.  Since
      // all our pages are persistent, we simply retrieve it from our "views" ArrayList.
      @Override
      public Object instantiateItem (ViewGroup container, int position)
      {
        View v = views.get (position);
        container.addView (v);
        return v;
      }
    
      //-----------------------------------------------------------------------------
      // Used by ViewPager.  Called when ViewPager no longer needs a page to display; it
      // is our job to remove the page from the container, which is normally the
      // ViewPager itself.  Since all our pages are persistent, we do nothing to the
      // contents of our "views" ArrayList.
      @Override
      public void destroyItem (ViewGroup container, int position, Object object)
      {
        container.removeView (views.get (position));
      }
    
      //-----------------------------------------------------------------------------
      // Used by ViewPager; can be used by app as well.
      // Returns the total number of pages that the ViewPage can display.  This must
      // never be 0.
      @Override
      public int getCount ()
      {
        return views.size();
      }
    
      //-----------------------------------------------------------------------------
      // Used by ViewPager.
      @Override
      public boolean isViewFromObject (View view, Object object)
      {
        return view == object;
      }
    
      //-----------------------------------------------------------------------------
      // Add "view" to right end of "views".
      // Returns the position of the new view.
      // The app should call this to add pages; not used by ViewPager.
      public int addView (View v)
      {
        return addView (v, views.size());
      }
    
      //-----------------------------------------------------------------------------
      // Add "view" at "position" to "views".
      // Returns position of new view.
      // The app should call this to add pages; not used by ViewPager.
      public int addView (View v, int position)
      {
        views.add (position, v);
        return position;
      }
    
      //-----------------------------------------------------------------------------
      // Removes "view" from "views".
      // Retuns position of removed view.
      // The app should call this to remove pages; not used by ViewPager.
      public int removeView (ViewPager pager, View v)
      {
        return removeView (pager, views.indexOf (v));
      }
    
      //-----------------------------------------------------------------------------
      // Removes the "view" at "position" from "views".
      // Retuns position of removed view.
      // The app should call this to remove pages; not used by ViewPager.
      public int removeView (ViewPager pager, int position)
      {
        // ViewPager doesn't have a delete method; the closest is to set the adapter
        // again.  When doing so, it deletes all its views.  Then we can delete the view
        // from from the adapter and finally set the adapter to the pager again.  Note
        // that we set the adapter to null before removing the view from "views" - that's
        // because while ViewPager deletes all its views, it will call destroyItem which
        // will in turn cause a null pointer ref.
        pager.setAdapter (null);
        views.remove (position);
        pager.setAdapter (this);
    
        return position;
      }
    
      //-----------------------------------------------------------------------------
      // Returns the "view" at "position".
      // The app should call this to retrieve a view; not used by ViewPager.
      public View getView (int position)
      {
        return views.get (position);
      }
    
      // Other relevant methods:
    
      // finishUpdate - called by the ViewPager - we don't care about what pages the
      // pager is displaying so we don't use this method.
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多