dynamically add and remove view to viewpager
2014-08-22 01:26
417 查看
原文地址:dynamically
add and remove view to viewpager
动态增加、删除 ViewPager
国人的代码质量确实差,加油~我当前的应用场景:由于业务的树状关系,让界面层级很多,就用ViewPager来做,类似视图栈的管理。转来一篇stackoverflow的解决方案,如下:
最后,这边在addview、removeview会报出error,加上PagerAdapter 的NotifyDataChanged就行,应该是状态检查的Exception。
另外,如果用Pager的地方多,也可以写个代理:PagerHelper(ViewPager, XXPagerAdapter),在这里面来实现各种方便的操作~
add and remove view to viewpager
动态增加、删除 ViewPager
国人的代码质量确实差,加油~我当前的应用场景:由于业务的树状关系,让界面层级很多,就用ViewPager来做,类似视图栈的管理。转来一篇stackoverflow的解决方案,如下:
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. }
class MainActivity extends Activity { private ViewPager pager = null; private MainPagerAdapter pagerAdapter = null; //----------------------------------------------------------------------------- @Override public void onCreate (Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView (R.layout.main_activity); ... do other initialization, such as create an ActionBar ... pagerAdapter = new MainPagerAdapter(); pager = (ViewPager) findViewById (R.id.view_pager); pager.setAdapter (pagerAdapter); // Create an initial view to display; must be a subclass of FrameLayout. FrameLayout v0 = (FrameLayout) inflater.inflate (R.layout.one_of_my_page_layouts, null); pagerAdapter.addView (v0, 0); } //----------------------------------------------------------------------------- // Here's what the app should do to add a view to the ViewPager. public void addView (View newPage) { int pageIndex = pagerAdapter.addView (newPage); // You might want to make "newPage" the currently displayed page: pager.setCurrentItem (pageIndex, true); } //----------------------------------------------------------------------------- // Here's what the app should do to remove a view from the ViewPager. public void removeView (View defunctPage) { int pageIndex = pagerAdapter.removeView (pager, defunctPage); // You might want to choose what page to display, if the current page was "defunctPage". if (pageIndex == pagerAdapter.getCount()) pageIndex--; pager.setCurrentItem (pageIndex); } //----------------------------------------------------------------------------- // Here's what the app should do to get the currently displayed page. public View getCurrentPage () { return pagerAdapter.getView (pager.getCurrentItem()); } //----------------------------------------------------------------------------- // Here's what the app should do to set the currently displayed page. "pageToShow" must // currently be in the adapter, or this will crash. public void setCurrentPage (View pageToShow) { pager.setCurrentItem (pagerAdapter.getItemPosition (pageToShow), true); } }
最后,这边在addview、removeview会报出error,加上PagerAdapter 的NotifyDataChanged就行,应该是状态检查的Exception。
另外,如果用Pager的地方多,也可以写个代理:PagerHelper(ViewPager, XXPagerAdapter),在这里面来实现各种方便的操作~
相关文章推荐
- TextView with SingleLine as true and Gravity as Center not passing the events to the ViewPager if it has a Click Event
- Add Zoom and Scale Capabilities to CScrollView
- 多附件上传组件代码(Add and Remove HTML elements dynamically with Javascript)
- How to Add and Remove Apps
- Gluster FS - How to add and remove a node
- add line order to each line and remove the last match of each line
- Android LayoutInflater - Dynamically Add and Remove Views using Java code
- add cool toolbar 256corler is easy,only two file and onCreate func is needed also the MainFrame.toolbar must resize to what real need and also the load width is same to it.
- Using Imagecache and CCK Imagefield to Add an Image in Drupal
- createDataSource, and add new rows on the datagrid to be filled out.
- ASP.NET 动态创建文本框 TextBox (add TextBox to page dynamically)
- MSCRM 4 - Remove 'Add Existing xxxxx to this record' button
- How to implement a many-to-many relationship using Linq to Sql ? Part II : add/remove support (转)
- How to add, modify, or delete registry subkeys and values by using a registration entries (.reg) file
- How to modify non-customizable entity in MS Dynamics CRM 4.0 (Add Product price column to Price List Item view)
- How to copy all view private files and Derived objects between views
- 如何在工具栏中,加上图标和文字?(How To Add Icon And Text)
- Gridview之RowCommand,ButtonField[add buttons and use the RowCommand event to add custom functionality to the control]
- ViewState 简述一(With Example And Apply to Asp.net)
- ViewState 简述一(With Example And Apply to Asp.net)