RecyclerView广告轮播有两个难点,首先是无限循环,网上很多修改Adapter的有缺陷,自定义一个LayoutManager无疑是一个好选择;难点二:每次只滚动一页,这个就需要自定义PagerSnapHelper,并且LayoutManager需要implements RecyclerView.SmoothScroller.ScrollVectorProvider
如果你是新手可以看看这篇文章,讲解很详细
https://blog.csdn.net/ww897532167/article/details/86585214
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<android.support.v7.widget.RecyclerView
android:id="@+id/advertisingCarousel"
android:layout_width="match_parent"
android:layout_height="200dp"/>
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="150dp"
android:orientation="horizontal">
</RadioGroup>
</RelativeLayout>
item布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="200dp" />
</LinearLayout>
初始化
List<SmartDrinkBean> list = new ArrayList<>();
list.add(new SmartDrinkBean(R.drawable.image_sz));
list.add(new SmartDrinkBean(R.drawable.image_sz));
list.add(new SmartDrinkBean(R.drawable.image_sz));
list.add(new SmartDrinkBean(R.drawable.image_sz));
RadioButtons = new ArrayList<>();
for (int i=0;i< list.size();i++) {
RadioButton radioButton = new RadioButton(mContext);
radioGroup.addView(radioButton);
RadioButtons.add(r