RecyclerView item 中 textView 跑马灯效果无效的

这篇博客回顾了在RecyclerView中实现TextView跑马灯效果的解决方案。博主指出,尽管在ListView中曾经解决过此类问题,但在长时间未使用后忘记了具体方法。关键在于XML配置,必须设置android:singleLine="true"(或者使用已更新的android:lines="1"),否则跑马灯效果无法正常工作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

跑马灯效果几年前好流行,一起在ListView 中解决过。但是很久没用了,一时竟然忘了。

其实在RecyclerView中解决方法也一样。

下面就权当做个记录。两个必要条件

1、XML配置

 Textview 一定要用 android:singleLine="true"。因为这个方法过时了,就用  android:lines="1".怎么搞都没用。

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:marqueeRepeatLimit="marquee_forever"
    android:scrollHorizontally="true"
    android:lines="1"
    android:textSize="12dp" />
正确配置如下:
<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:marqueeRepeatLimit="marquee_forever"
    android:singleLine="true"
    android:scrollHorizontally="true"
    android:textSize="12dp" />
2、在adapter 里面 TextView 设置    holder.titleTv.setSelected(true);

@Override
public void onBindViewHolder(MainListHolder holder, int position) {
    Uri uri = Uri.parse(Constant.IMG_BASEURL + mDatas.get(position).getMainPic());
    Log.i("MainListAdapter", uri.toString());
    holder.draweeView.setImageURI(uri);
    holder.titleTv.setText(mDatas.get(position).getTitle());
    holder.titleTv.setSelected(true);
    holder.cityDsstrictTv.setText(mDatas.get(position).getCityTitle() + " [" +  mDatas.get(position).getDistrictTitle()+"]");
    holder.priceTv.setText(mDatas.get(position).getPrice() + "");
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值