正常使用Viewpager的代码然后几处添加代码
主要是在这里还有布局中的属性
mViewpager.setPageMargin(30);//设置间距
mViewpager.setOffscreenPageLimit(mImagesList.size());
android:clipChildren="false"
适配器
public class Frag_Home_PagerAdapter extends PagerAdapter {
private Context context;
private List<String> mImageList;
public Frag_Home_PagerAdapter(Context context, List<String> mImageList) {
this.context = context;
this.mImageList = mImageList;
}
@Override
public int getCount() {
return mImageList.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
return view == o;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
Glide.with(context).load(mImageList.get(position)).into(imageView);
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:clipChildren="false"
android:id="@+id/frag_home_linner"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/frag_home_viewpager"
android:clipChildren="false"
android:layout_width="match_parent"
android:layout_marginLeft="@dimen/dp_30"
android:layout_marginRight="@dimen/dp_30"
android:layout_height="@dimen/dp_200"></android.support.v4.view.ViewPager>
</LinearLayout>
Activity层
mViewpager.setPageMargin(30);//设置间距
mViewpager.setOffscreenPageLimit(mImagesList.size());
Frag_Home_PagerAdapter homePagerAdapter = new Frag_Home_PagerAdapter(getActivity(),mImagesList);
mViewpager.setAdapter(homePagerAdapter);