代碼實現(xiàn)
先貼一下TextView
跑馬燈的實現(xiàn)代碼:
<TextView
android:id="@+id/fragment_game_detail_header_new_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/common_text"
android:textSize="18.0sp"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"/>
遇到問題
在界面上碱工,有一個用viewPager
實現(xiàn)的廣告輪播功能庵寞,發(fā)現(xiàn)每次切換廣告的時候茅姜,跑馬燈會跳動闪朱,并且從頭顯示,以為是viewPager
與跑馬燈沖突钻洒,后來在網(wǎng)上搜了一下奋姿,android 6.0有時候會出現(xiàn)這個問題。
解決的方法
在跑馬燈控件外層素标,再嵌套一個布局控件:
<LinearLayout
android:id="@+id/fragment_game_detail_header_new_name_layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="3dp"
android:layout_marginRight="10dp">
<TextView
android:id="@+id/fragment_game_detail_header_new_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/common_text"
android:textSize="18.0sp"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"/>
</LinearLayout>
總結(jié)
1称诗、正常情況下,跑馬燈用TextView
實現(xiàn)即可头遭,如果遇到抖動問題寓免,在TextView
外層嵌套一個局部就可以解決問題。
2计维、如果跑馬燈用在列表的item里面袜香,會發(fā)現(xiàn)跑馬燈并沒有跑起來,怎么辦呢鲫惶?只要在ViewHolder
調(diào)用TextView.setSelected(true)
就行了蜈首。
最后貼上封裝的跑馬燈TextView
public class MarqueeTextView extends AppCompatTextView {
public MarqueeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setSelected(true);
setSingleLine();
setMarqueeRepeatLimit(Integer.MAX_VALUE);
setEllipsize(TextUtils.TruncateAt.MARQUEE);
}
@Override
public void setSelected(boolean selected) {
super.setSelected(selected);
}
}