Android仿制淘宝滚动图文条

开篇废话

产品让我将“我的”页面改版,上面加了一个广告条,非常类似淘宝“我的”页面的广告条,然后就自己写了一个,虽然网上类似的教程有很多了,但是我用的这个实现方法我没有见过,如果大家有更好的办法请留言给我,谢谢。这里只提供一种思路,有兴趣的小伙伴可以自行封装一下,记得告知我。滚动图文条之GitHub地址,帮我点个Star。

滚动图文条

大概效果就是下图这样。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bA9PYlyY-1637131665774)(https://upload-images.jianshu.io/upload_images/7203723-7194925ba7e54edc.gif?imageMogr2/auto-orient/strip)]

思路

  1. 写一个不可用手滑动的RecyclerView
  2. 使用Handler定时RecyclerView自动滑动到下一个Item
  3. 使用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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是步骤: 1. 打开Android Studio,创建一个新的项目。 2. 在app目录下的res文件夹中,创建一个新的layout文件夹。在这个文件夹下创建一个新的xml文件,命名为activity_login.xml。 3. 在xml文件中添加以下代码: ``` <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/logo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" android:src="@drawable/qq_logo" /> <EditText android:id="@+id/username" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/logo" android:layout_marginTop="50dp" android:layout_marginLeft="30dp" android:layout_marginRight="30dp" android:padding="10dp" android:hint="请输入QQ号" /> <EditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/username" android:layout_marginTop="20dp" android:layout_marginLeft="30dp" android:layout_marginRight="30dp" android:padding="10dp" android:inputType="textPassword" android:hint="请输入密码" /> <Button android:id="@+id/login_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/password" android:layout_marginTop="50dp" android:layout_marginLeft="30dp" android:layout_marginRight="30dp" android:text="登录" /> </RelativeLayout> ``` 4. 在res文件夹下的drawable文件夹中,添加一个新的图片,命名为qq_logo.png,作为QQ的logo图标。 5. 在MainActivity.java文件中,添加以下代码: ``` public class MainActivity extends AppCompatActivity { private EditText usernameEditText; private EditText passwordEditText; private Button loginButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); usernameEditText = findViewById(R.id.username); passwordEditText = findViewById(R.id.password); loginButton = findViewById(R.id.login_button); loginButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String username = usernameEditText.getText().toString(); String password = passwordEditText.getText().toString(); // 在这里添加登录的逻辑 } }); } } ``` 6. 运行程序,你就可以看到一个仿制的QQ登录界面了。 注意:这个登录界面只是一个简单的示例,没有实现真正的登录功能。如果你想要实现登录功能,你需要自己编写相关的代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值