Java 为什么不能以编程方式滚动ScrollView?
我想以编程方式滚动Java 为什么不能以编程方式滚动ScrollView?,java,android,android-scrollview,Java,Android,Android Scrollview,我想以编程方式滚动ScrollView,但我不能 ScrollView方法“scrollTo、arrowScroll、pageScroll和fullScroll”都不起作用 final ScrollView scrollView = (ScrollView) view.findViewById(R.id.scrollbar_textchat); Button sendButton = (Button) view.findViewById(R.id.btn_send); sendB
ScrollView
,但我不能
ScrollView
方法“scrollTo
、arrowScroll
、pageScroll
和fullScroll
”都不起作用
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);
}
});
您应该在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);
}});
请尝试以下代码:
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布局:
<?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
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”
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
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”
谢谢你的回答,但我之前测试过,没有成功!真奇怪!这对我来说总是有效的,不管怎样,你有没有尝试过scrollView.postDelayed
而不是scrollView.post
谢谢你的回答,但我之前测试过,没有成功!真奇怪!这对我来说总是有效的,不管怎样,您是否尝试过scrollView.postDelayed
而不是scrollView.post
您只是将scrollView
添加到Listview
中。。因此,请尝试滚动列表视图。检查编辑还有另一个问题:您的手动滚动是否对列表视图项目有效?非常感谢,我滚动了listview
,并且确实有效。您仅将scrollview
添加到listview
。。因此,请尝试滚动listview。检查编辑还有另一个问题:您的手动滚动对listview项目有效吗?非常感谢,我滚动了listview,确实有效。