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

时间:2017-02-13 10:08:07

标签: java android android-scrollview

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

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

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

3 个答案:

答案 0 :(得分:1)

试试这段代码:

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

使用ListView:滚动到底部的poistion

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

答案 1 :(得分:1)

xml layout:

除去

 <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 =&#34; 1&#34;

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

答案 2 :(得分:0)

你应该在scrollView.post中运行代码,如下所示:

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