Android 水平滚动视图:添加新视图时自动滚动到结束?
我有一个包含线性布局的水平滚动视图。在屏幕上,我有一个按钮,可以在运行时将新视图添加到LinearLayout,我希望在添加新视图时,滚动视图可以滚动到列表的末尾 我几乎让它工作-除了它总是滚动一个视图短的最后一个视图。它似乎在滚动,而没有首先计算新视图的包含 在我的应用程序中,我使用了一个自定义视图对象,但我制作了一个使用ImageView的小型测试应用程序,并且具有相同的症状。我在Layout和ScrollView上尝试了各种方法,比如requestLayout(),我尝试了scrollTo(Integer.MAX_VALUE),然后它滚动到netherverse:)我是否违反了UI线程问题或其他问题Android 水平滚动视图:添加新视图时自动滚动到结束?,android,user-interface,horizontal-scrolling,Android,User Interface,Horizontal Scrolling,我有一个包含线性布局的水平滚动视图。在屏幕上,我有一个按钮,可以在运行时将新视图添加到LinearLayout,我希望在添加新视图时,滚动视图可以滚动到列表的末尾 我几乎让它工作-除了它总是滚动一个视图短的最后一个视图。它似乎在滚动,而没有首先计算新视图的包含 在我的应用程序中,我使用了一个自定义视图对象,但我制作了一个使用ImageView的小型测试应用程序,并且具有相同的症状。我在Layout和ScrollView上尝试了各种方法,比如requestLayout(),我尝试了scrollTo
- 瑞克
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);
与:
短暂的延迟应该给系统足够的时间来解决
另外,只需将滚动延迟到UI循环的当前迭代之后就足够了。我还没有测试过这个理论,但如果它是正确的,那么做以下几点就足够了:
s.post(new Runnable() {
public void run() {
s.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
});
我更喜欢这一点,因为引入任意延迟对我来说似乎很麻烦(尽管这是我的建议)。这只是另一个建议,因为这个问题对我帮助很大:) 您可以在视图完成布局阶段后放置一个侦听器,然后立即执行完整滚动,尽管您需要为此扩展类 我之所以这样做,是因为我想滚动到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();
}
}
使用查看方法SmoothScroll
postDelayed(new Runnable() {
public void run() {
scrollView.smoothScrollTo(newView.getLeft(), newView.getTop());
}
}, 100L);
你需要输入一个runnable,这样你就可以在布局过程中留出时间来定位新视图了。我认为最好的方法是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.post(new Runnable() {
@Override
public void run() {
((HorizontalScrollView) Iview
.findViewById(R.id.hr_scroll))
.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
});
这就是我所做的
//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);
}
});
}
我同意@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);
}
});
子
视图
不能立即添加到视图组
。因此需要发布一个Runnable
,它将在其他挂起的任务完成后运行
因此,在添加视图后,请使用:
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)
泰德,非常感谢,这已经困扰了我好几天了!:)Hi-Ted…当我尝试使用上述代码时,它显示错误无法解析方法“postdayed(java.lang.runnable,long)”。。。请帮帮我…@VinitVikash-这是视图
类中的一个方法。您可能需要执行s.postDelayed(…)
。或者,创建一个处理程序
并使用它的postDelayed()
方法。new Handler().postDelayed()
如果您不知道如何使用postDelayed()
@PratikButani-这是可行的,但将处理程序作为一个字段分配一次并使用会更有效,而不是每次都在飞行中创建一个。然而,由于我们已经有了视图引用s
,最好只使用s.postDelayed(…)
,这很好地避免了我们自己的处理程序的需要。这也是我提出的解决方案。更少的代码,完成了工作。这是到目前为止问题的正确答案。任何随机时间的等待都是胡说八道。等待布局引擎完成它的工作是正确的。我不认为时间长短是相关的。问题是滚动不应该发生在UI线程循环的当前迭代中。当前迭代完成后,视图层次结构应该是最新的,并且布局合理。不适合我。View#getTop()返回相对于父级的位置,因此这在更多嵌套布局中不起作用
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)