recycleView中实现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);

猜你喜欢

转载自blog.csdn.net/lyjSmile/article/details/79359006