为什么不能以编程方式滚动 ScrollView?

Why cannot scroll ScrollView Programmatically?

我想以编程方式滚动 ScrollView 但我不能。

None 的 ScrollView 方法“scrollToarrowScrollpageScrollfullScroll” 不起作用!

final ScrollView scrollView = (ScrollView) view.findViewById(R.id.scrollbar_textchat);
Button sendButton = (Button) view.findViewById(R.id.btn_send);
        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                scrollView.fullScroll(View.FOCUS_DOWN);
            }
        });

xml布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <!--main-->
        <ScrollView
            android:id="@+id/scrollbar_textchat"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:verticalScrollbarPosition="left"
            android:fillViewport="true">
                <!--messages list-->
                <ListView
                    android:id="@+id/list_textchat"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:listSelector="@android:color/transparent"
                    android:cacheColorHint="@android:color/transparent" />
        </ScrollView>
        <!--/main-->

        <!--line-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_marginBottom="10dp"
            android:layout_height="2dp"
            android:background="#FFEEEEEE"/>

        <!--already Typing message...-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:orientation="horizontal">

            <com.salamatyar.components.TypingAnimationView
                android:id="@+id/typing_textchat"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dp"/>

            <TextView
                android:id="@+id/text_typing_message"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="typing" />

        </LinearLayout>
        <!--/already Typing message...-->

        <!--line-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_marginBottom="10dp"
            android:layout_height="2dp"
            android:background="#FFEEEEEE"/>

        <!--type-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <EditText
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>

            <Button
                android:id="@+id/btn_send"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="send"/>

        </LinearLayout>
        <!--/type-->
    </LinearLayout>

</LinearLayout>

你应该 运行 scrollView.post 中的代码如下:

scrollView.post(new Runnable() {            
    @Override
    public void run() {
           scrollView.fullScroll(View.FOCUS_DOWN);              
    }
});

试试这个代码:

 final ScrollView scrollView = (ScrollView) view.findViewById(R.id.scrollbar_textchat);
scrollView.post(new Runnable() { 
        public void run() { 
             scrollView.scrollTo(0, scrollView.getBottom());
        } 
});

使用 ListView: 滚动到底部位置

  listView.post(new Runnable(){
  public void run() {
    listView.setSelection(listView.getCount() - 1);
  }});

xml布局:

移除

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

并使用 android:layout_weight="1"

       <ListView
            android:id="@+id/list_textchat"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:listSelector="@android:color/transparent"
            android:cacheColorHint="@android:color/transparent" />