[英]How do I dynamically add fragments to start of ViewPager as user reaches the end of the ViewPager?
[英]how do I add margin at the end of a ViewPager?
我正在处理使用viewpager在片段中显示图像的水平列表(而不是listview或recyclerview)的旧代码。 viewpager使用一个自定义的PagerAdapter,它会覆盖适当的方法(instantiateItem,getItemPositiion等)。 我现在的问题是,我想在viewpager的最后一个元素的末尾添加一些空间,我试图通过在最后一个元素中添加一个边距来实现。
public Object instantiateItem(ViewGroup collection, int position) {
FrameLayout itemView = ViewManager.createView(position, height, type);
int viewWidth = itemView.getLayoutParams().width;
RelativeLayout itemViewWrap = new RelativeLayout(mContext);
if(postion == items.size()-1){
RelativeLayout.LayoutParams itemViewparams = RelaiveLayout.LayoutParams)itemView.getLayoutParams();
itemViewparams.setMargins(0, 0, viewWidth, 0);
}
itemViewWrap.addView(itemView);
collection.addView(itemViewWrap);
return itemView;
}
不幸的是,这不起作用,并且最后一个元素也没有显示。 我如何正确地做到这一点? 这是该viewpager所在片段的xml。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.playground.test.views.ItemViewPager
android:id="@+id/viewpager"
android:layout_gravity="center_vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
</RelativeLayout>
您可以使用ViewPagerAdapter#getPageWidth覆盖最后一页的宽度。 要比页面的其余部分具有所需的页边空白。
https://developer.android.com/reference/android/support/v4/view/PagerAdapter.html#getPageWidth(int)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.