Android scrollview autoscrolling as text is added

倾然丶 夕夏残阳落幕 提交于 2019-12-04 08:54:24

问题


I have a scrollview containing a textview in an Android app. This textview will have text appended to it continuously at set intervals. Scrolling works and the text adds just fine, but what I'd like to do is have the scrollview autoscroll down as text is added. As new text is appended at the bottom, it automatically scrolls down to match and old text is pushed out of sight at the top. What would be even better is to add text to the bottom of the scrollview and have it push older text upward, but one thing at a time.


回答1:


<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    >
    <TextView
        android:id="@+id/statusText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom" />
</ScrollView>



回答2:


I've tried the solution with gravity but result was a lot of empty space under actual text when scroll to bottom.

So that is another way. You can place your TextView inside of ScrollView:

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:fillViewport="true" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</ScrollView>

And define addTextChangedListener

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

            //some code here

        ScrollView scrollView1 = (ScrollView) findViewById(R.id.scrollView1);
        TextView textView1 = (TextView) findViewById(R.id.textView1);
        textView1.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable arg0) {
            scrollView1.fullScroll(ScrollView.FOCUS_DOWN);
            // you can add a toast or whatever you want here
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
                int arg2, int arg3) {
            //override stub
        }

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            //override stub
        }

    }) }

Actually i noticed right now that the application will scroll every time the text is changed, not just added. However it works just fine for me.




回答3:


You can simply scroll the whole ScrollView to the bottom when text is added.

textView.append(text);
scroll.fullScroll(View.FOCUS_DOWN)



回答4:


textView.append(text);
scroll.fullScroll(View.FOCUS_DOWN)

this will give problem, it won't scroll upto extreme top, due to android:layout_gravity="bottom"




回答5:


If you're looking for a more generic way of auto-scrolling to the bottom of a ScrollView, you could try the snippet below. It has the advantage that you don't need to post a Runnable to make sure you're in the UI thread. It has the disadvantage that I'm not sure what things this could break.

public class AutoScroller extends ScrollView {
    public boolean autoscroll = true;

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


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if(autoscroll) {
           scrollTo(getScrollX(), getChildAt(getChildCount()-1).getBottom() - getHeight());
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

Another candidat to override might be onLayout.



来源:https://stackoverflow.com/questions/5224761/android-scrollview-autoscrolling-as-text-is-added

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!