安卓自定义TextView实现自动滚动

Posted 别人眼中的过客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了安卓自定义TextView实现自动滚动相关的知识,希望对你有一定的参考价值。

xml文件代码

      <com.mobile.APITest.ScrollEditText
            android:id="@+id/statusEditText"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dip"
            android:layout_marginRight="5dip"
            android:layout_weight="1"
            android:gravity="top"
            android:scrollbarFadeDuration="1000"
            android:scrollbarStyle="insideOverlay"
            android:scrollbars="vertical"
            android:singleLine="false"
            android:textColor="@color/black" />

自定义TextView

package com.mobile.APITest;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.method.ScrollingMovementMethod;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.mobile.pos.lib.Global.POSCharUtils;

/**
 * Created by Administrator on 2016/11/23.
 */
@SuppressLint("AppCompatCustomView")
public class ScrollEditText extends TextView {
    private static final String TAG = "ScrollEditText";
    private boolean pressFlag = false;//判断手指是否按着屏幕,如果是就不需要滚动了。

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


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

    public ScrollEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            postDelayed(runnable, 2000);
        }
        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            pressFlag = true;
        }
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            pressFlag = true;
            removeCallbacks(runnable);
        }
        POSCharUtils.showLogD(TAG, event.getAction() + "");
        return super.dispatchTouchEvent(event);
    }

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            pressFlag = false;
        }
    };

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        setOnLongClickListener(new OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                showDialog();
                return false;
            }
        });
        setMovementMethod(ScrollingMovementMethod.getInstance());
    }


    public void appendStr(final CharSequence text) {
        post(new Runnable() {
            @Override
            public void run() {
                ScrollEditText.super.append(text);
                POSCharUtils.showLogD(TAG, text.toString());
                if (pressFlag) return;
                int scrollAmount = getLayout().getLineTop(getLineCount())
                        - getHeight();
                if (scrollAmount > 0)
                    scrollTo(0, scrollAmount);
                else
                    scrollTo(0, 0);
            }
        });
    }

    public void setTextStr(final CharSequence text) {
        post(new Runnable() {
            @Override
            public void run() {
                ScrollEditText.super.setText(text);
                if (pressFlag) return;
                int scrollAmount = getLayout().getLineTop(getLineCount())
                        - getHeight();

                if (scrollAmount > 0)
                    scrollTo(0, scrollAmount);
                else
                    scrollTo(0, 0);
            }
        });
    }

    private void scrollTop() {
        scrollTo(0, 0);
    }

    private void scrollBottom() {
        int scrollAmount = getLayout().getLineTop(getLineCount())
                - getHeight();
        if (scrollAmount > 0)
            scrollTo(0, scrollAmount);
        else
            scrollTo(0, 0);
    }
    //信息内容是简单地列表项

    public void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this.getContext());
        //定义复选框
        builder.setItems(new String[]{"顶部", "底部"}, listener);
        builder.create().show();
    }

    private DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
                case 0:
                    scrollTop();
                    break;
                case 1:
                    scrollBottom();
                    break;
            }
        }
    };
}

长按ScrollEditText 可以跳转至view的底部或者顶部

以上是关于安卓自定义TextView实现自动滚动的主要内容,如果未能解决你的问题,请参考以下文章

Android 文字自动滚动(跑马灯)效果的两种实现方法[特别好使]

Android安卓进阶技巧之全局自定义字体的实现

安卓自定义View文章数据滚动显示数值

ios中具有自动滚动功能的无限Textview

Android 自定义TextView实现文本内容自动调整字体大小以适应TextView的大小

自定义View实现自动数字增长的TextView