Android 跑马灯重复抖动的解决方法

代码实现

先贴一下TextView跑马灯的实现代码:

<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实现的广告轮播功能,发现每次切换广告的时候,跑马灯会跳动,并且从头显示,以为是viewPager与跑马灯冲突,后来在网上搜了一下,android 6.0有时候会出现这个问题。

解决的方法

在跑马灯控件外层,再嵌套一个布局控件:

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

总结

1、正常情况下,跑马灯用TextView实现即可,如果遇到抖动问题,在TextView外层嵌套一个局部就可以解决问题。
2、如果跑马灯用在列表的item里面,会发现跑马灯并没有跑起来,怎么办呢?只要在ViewHolder调用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);
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容