Ric*_*use 49 user-interface android horizontal-scrolling
我有一个包含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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
布局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>
Run Code Online (Sandbox Code Playgroud)
Ted*_*opp 129
我认为这是一个时间问题.添加视图时不进行布局.请求并在短时间内完成.在添加视图后立即调用fullScroll时,linearlayout的宽度没有机会展开.
尝试更换:
s.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
Run Code Online (Sandbox Code Playgroud)
有:
s.postDelayed(new Runnable() {
public void run() {
s.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
}, 100L);
Run Code Online (Sandbox Code Playgroud)
短暂的延迟应该给系统足够的时间来解决.
PS简单地延迟滚动直到UI循环的当前迭代之后可能就足够了.我没有测试过这个理论,但是如果它是正确的,那么做下面的事就足够了:
s.post(new Runnable() {
public void run() {
s.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
});
Run Code Online (Sandbox Code Playgroud)
我更喜欢这个,因为引入任意延迟对我来说似乎很苛刻(即使这是我的建议).
New*_*wtz 15
我同意@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);
}
});
Run Code Online (Sandbox Code Playgroud)
mon*_*alo 12
只是另一个消化,因为这个问题帮了我很多:).
您可以在视图完成其布局阶段时放置一个侦听器,并且在执行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();
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
57374 次 |
最近记录: |