我有一个包含LinearLayout的HorizontalScrollView。在屏幕上我有一个Button,它将在运行时向LinearLayout添加新的视图,我希望滚动视图在添加新视图时滚动到列表的末尾。
我几乎让它工作 - 除了它总是滚动一个视图而不是最后一个视图。在没有首先计算包含新视图的情况下,它似乎在滚动。
在我的应用程序中,我使用的是自定义View对象,但我制作了一个使用ImageView并具有相同症状的小型测试应用程序。我在Layout和ScrollView上尝试了各种各样的东西,比如requestLayout(),我尝试了scrollTo(Integer.MAX_VALUE),它滚动到了netherverse :)我是否违反了UI线程问题?
======
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.addButton);
b.setOnClickListener(new AddListener());
add();
}
private void add() {
LinearLayout l = (LinearLayout) findViewById(R.id.list);
HorizontalScrollView s =
(HorizontalScrollView) findViewById(R.id.scroller);
ImageView i = new ImageView(getApplicationContext());
i.setImageResource(android.R.drawable.btn_star_big_on);
l.addView(i);
s.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
private class AddListener implements View.OnClickListener {
@Override
public void onClick(View v) {
add();
}
}
}
布局XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<HorizontalScrollView
android:id="@+id/scroller"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbarSize="50px">
<LinearLayout
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4px"/>
</HorizontalScrollView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center">
<Button
android:id="@+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="80px"
android:paddingRight="80px"
android:paddingTop="40px"
android:paddingBottom="40px"
android:text="Add"/>
</LinearLayout>
</LinearLayout>
我认为这是一个时间问题。添加视图时不进行布局。请求并在短时间内完成。在添加视图后立即调用fullScroll时,linearlayout的宽度没有机会展开。
尝试更换:
s.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
有:
s.postDelayed(new Runnable() {
public void run() {
s.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
}, 100L);
短暂的延迟应该给系统足够的时间来解决。
附:简单地延迟滚动直到UI循环的当前迭代之后可能就足够了。我没有测试过这个理论,但是如果它是正确的,那么做下面的事就足够了:
s.post(new Runnable() {
public void run() {
s.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
});
我更喜欢这个,因为引入任意延迟对我来说似乎很苛刻(即使这是我的建议)。
我同意@monxalo和@granko87的观点,更好的方法是使用监听器而不是假设如果我们让任意时间量通过,布局将完成。
如果像我一样,你不需要使用自定义的HorizontalScrollView子类,你可以添加一个OnLayoutChangeListener来保持简单:
mTagsScroller.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View view, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
mTagsScroller.removeOnLayoutChangeListener(this);
mTagsScroller.fullScroll(View.FOCUS_RIGHT);
}
});
只是另一个消化,因为这个问题帮了我很多:)。
您可以在视图完成其布局阶段时放置一个侦听器,并且在执行fullScroll之后,您需要为此扩展该类。
我只是这样做是因为我想在onCreate()之后滚动到一个部分,以避免从起点到滚动点的闪烁。
就像是:
public class PagerView extends HorizontalScrollView {
private OnLayoutListener mListener;
///...
private interface OnLayoutListener {
void onLayout();
}
public void fullScrollOnLayout(final int direction) {
mListener = new OnLayoutListener() {
@Override
public void onLayout() {
fullScroll(direction)
mListener = null;
}
};
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if(mListener != null)
mListener.onLayout();
}
}
使用View方法smoothScrollTo
postDelayed(new Runnable() {
public void run() {
scrollView.smoothScrollTo(newView.getLeft(), newView.getTop());
}
}, 100L);
您需要输入一个runnable,以便为布局过程留出时间来定位新视图
这就是我做的。
//Observe for a layout change
ViewTreeObserver viewTreeObserver = yourLayout.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
yourHorizontalScrollView.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
});
}
使用下面的代码进行水平滚动视图{向右滚动}
view.post(new Runnable() {
@Override
public void run() {
((HorizontalScrollView) Iview
.findViewById(R.id.hr_scroll))
.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
});
我认为最好的方法是monxalo发布的方法,因为在布局完成之前你不知道需要多长时间。我尝试过它,效果很好。唯一的区别是,我在自定义水平滚动视图中只添加了一行:
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
this.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
孩子View
可能不会立即加入ViewGroup
。所以需要发布一个Runnable
,它将在完成其他待处理任务后运行。
所以在添加View
后,使用:
horizontalScrollView.post(new Runnable() {
@Override
public void run() {
horizontalScrollView.fullScroll(View.FOCUS_RIGHT);
}
});
这是我在kotlin中使用3个ImageView的方式:
var p1 = true; var p2 = false; var p3 = false
val x = -55
val handler = Handler()
handler.postDelayed(object : Runnable {
override fun run() {
if (p1){
mainView.picsScrollViewID.smoothScrollTo(mainView.bigPic1ID.x.toInt() + x, 0)
p1 = false
p2 = true
p3 = false
}
else if(p2){
mainView.picsScrollViewID.smoothScrollTo(mainView.bigPic2ID.x.toInt() + x, 0)
p1 = false
p2 = false
p3 = true
}
else if(p3){
mainView.picsScrollViewID.smoothScrollTo(mainView.bigPic3ID.x.toInt() + x, 0)
p1 = true
p2 = false
p3 = false
}
handler.postDelayed(this, 2000)
}
}, 1400)