RecyclerView广告轮播

本文介绍了如何使用RecyclerView创建广告轮播效果,重点在于无限循环的实现,通过自定义LayoutManager和PagerSnapHelper来解决这一难题。同时,文章提供了一个详细的步骤指南,包括item布局、初始化、自定义组件及适配器的设置,最后讲解了如何实现循环滚动。
摘要由CSDN通过智能技术生成

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值