首页 > 编程技术 > android

android跑马灯出现重复跳动以及不滚动问题的解决方法

发布时间:2021-9-16 00:00

android跑马灯出现重复跳动、不滚动问题,本文给出解决方案,供大家参考。

原因:页面有View被重新绘制了、焦点被抢占

例如:

1、TextView 的width被设置为wrap_content,setText()时内容改变会导致View重新绘制;
2、页面中动态生成View同样会影响跑马灯效果;

解决办法:

1.尽可能的将页面的View的宽和高设置为固定值,尽量不要动态去修改

2.自定义TextView 重写isFocused()函数,让他放回true也就是一直获取了,焦点效果自然也就出来了,如果这都不能解决那肯就不是焦点问题了。

public class MarqueTextView extends TextView {

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

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

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

    @Override

    public boolean isFocused() {
        return true;
    }
    @Override
    protected void onFocusChanged(boolean focused, int direction,
                                  Rect previouslyFocusedRect) {
        if(focused){
            super.onFocusChanged(focused,direction,previouslyFocusedRect);
        }
    }

    @Override
    public void onWindowFocusChanged(boolean focused)
    {
        if (focused)
        {
            super.onWindowFocusChanged(focused);
        }
    }
}

小编之前还看到一个关于android跑马灯重复抖动的解决方法,也分享给大家,谢谢原作者的分享

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

<TextView
       android:id="@+id/tv_blueToothDatas"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:textColor="@color/yellow_f38131"
       android:singleLine="true"
       android:ellipsize="marquee"
       android:focusable="true"
       android:focusableInTouchMode="true"
       android:marqueeRepeatLimit="marquee_forever"
/>

出现的问题,在界面上,有一个用viewPager实现的广告轮播功能,发现每次切换广告的时候,跑马灯会跳动,并且从头显示,以为是viewPager与跑马灯冲突,后来在网上搜了一下,android 6.0有时候会出现这个问题,解决的方法,在跑马灯控件外层,再嵌套一个布局控件

<LinearLayout android:layout_width="0dp"
         android:layout_height="wrap_content"
         android:layout_weight="1"
         android:orientation="horizontal"
         android:layout_marginLeft="5dp"
         android:layout_marginRight="5dp">
       <TextView
         android:id="@+id/tv_blueToothDatas"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         tools:text="11111111111111111111111111111111111111111111111"
         android:textColor="@color/yellow_f38131"
         android:singleLine="true"
         android:ellipsize="marquee"
         android:focusable="true"
         android:focusableInTouchMode="true"
         android:marqueeRepeatLimit="marquee_forever"
     />
</LinearLayout>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。

标签:[!--infotagslink--]

您可能感兴趣的文章: