recycleView中实现textView跑马灯效果

本文介绍了一种自定义TextView的方法,使其能在Android应用中实现跑马灯效果。通过重写TextView的isFocused()和isSelected()方法,确保TextView能够响应触摸事件并保持焦点,从而实现水平滚动的文字显示效果。

自定义一个TextView
public class MarqueTextView extends AppCompatTextView {

public MarqueTextView(Context context) {
    super(context);
}

public MarqueTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public MarqueTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
public boolean isFocused() {
    return true;
    //        自定义设置让focusable为true
    //        这个方法相当于在layout中
    //        android:focusable="true"
    //        android:focusableInTouchMode="true"
}
/**
 * 详情见TextView中setSelected方法
 * TextView中setSelected方法调该方法,返回false才有跑马灯效果
 * @return 返回false
 */
@Override
public boolean isSelected() {
    return false;
}

}

布局文件中
<.MarqueeTextView
android:id=”@+id/tv_today_new_word”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_centerHorizontal=”true”
android:ellipsize=”marquee”
android:focusable=”true”
android:focusableInTouchMode=”true”
android:gravity=”center”
android:marqueeRepeatLimit=”marquee_forever”
android:scrollHorizontally=”true”
android:singleLine=”true”
android:textColor=”@color/hm_new_word_color”
android:textSize=”@dimen/hm_word_font_size”/>

在recycleView中设置,必须设置该项。

((ViewHolder) holder).mTvNewWord.setSelected(true);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值