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);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章