开篇废话
产品让我将“我的”页面改版,上面加了一个广告条,非常类似淘宝“我的”页面的广告条,然后就自己写了一个,虽然网上类似的教程有很多了,但是我用的这个实现方法我没有见过,如果大家有更好的办法请留言给我,谢谢。这里只提供一种思路,有兴趣的小伙伴可以自行封装一下,记得告知我。滚动图文条之GitHub地址,帮我点个Star。
滚动图文条
大概效果就是下图这样。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bA9PYlyY-1637131665774)(https://upload-images.jianshu.io/upload_images/7203723-7194925ba7e54edc.gif?imageMogr2/auto-orient/strip)]
思路
- 写一个不可用手滑动的RecyclerView
- 使用Handler定时RecyclerView自动滑动到下一个Item
- 使用smoothScrollToPosition使其平滑地滑动
开始工作
做一些基本工作
写一个AdModel类。
public class AdModel {
public String title;
public String content;
public AdModel(String title, String content) {
this.title = title;
this.content = content;
}
}
写一些item_ad布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:minHeight="50dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:background="@null"
tools:background="@color/black">
<LinearLayout
android:id="@+id/ll_ad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/tv_title"
tools:text="会员身份0元抢"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:lines="1"
android:textColor="@color/white"
android:textSize="12sp" />
<TextView
android:id="@+id/tv_content"
tools:text="送你体验会员"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:lines="1"
android:textColor="@color/white"
android:textSize