I would like to add or delete pages from my view pager dynamically. Is that possible?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
Based on other answers and other resources I've ended with this code.
CustomPagerAdapter:
MainActivity:
To remove pages:
MainFragment:
SecondaryFragment has the same code.
The layouts are simple ViewPager/Fragment layouts with
android.support.v4.view.ViewPager
with idcontainer
and FragmentTextView
with idsection_label
.Resources:
you have to set the adapter to viewpager again, then it will refresh the content.
removeView(int pos) in my PagerAdaper
wherever I am removing the file I have to do like this
EDIT:
First:override the pagerAdapter method "getItemPosition"
Second:remove the data bind with the adapter.and call adapter.notifydatachanged
Yes. You can add or delete views dynamically to the PagerAdapter that is supplying them to the ViewPager and call
notifyDataSetChanged()
from the PagerAdapter to alert the affected ViewPager about the changes. However, when you do so, you must override thegetItemPosition(Object)
of the PagerAdapter, that tells them whether the items they are currently showing have changed positions. By default, this function is set toPOSITION_UNCHANGED
, so the ViewPager will not refresh immediately if you do not override this method. For example,Although, if you simply want to add or remove the view temporarily from display, but not from the dataset of the PagerAdapter, try using
setPrimaryItem(ViewGroup, int, Object)
for going to a particular view in the PagerAdapter's data anddestroyItem(ViewGroup, int, Object)
for removing a view from display.