Horizo​​ntalScrollView:添加新视图时自动滚动到结束? [英] HorizontalScrollView: auto-scroll to end when new Views are added?

查看:28
本文介绍了Horizo​​ntalScrollView:添加新视图时自动滚动到结束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 LinearLayout 的 Horizo​​ntalScrollView.在屏幕上,我有一个按钮,它会在运行时向 LinearLayout 添加新的视图,我希望在添加新视图时滚动视图滚动到列表的末尾.

I have a HorizontalScrollView containing a LinearLayout. On screen I have a Button that will add new Views to the LinearLayout at runtime, and I'd like the scroll view to scroll to the end of the list when a new View is added.

我几乎让它工作了 - 除了它总是比最后一个视图滚动一个视图.它似乎在没有首先计算新视图的包含情况下滚动.

I almost have it working - except that it always scrolls one view short of the last view. It seems like it's scrolling without first calculating the inclusion of the new view.

在我的应用程序中,我使用了自定义 View 对象,但我制作了一个使用 ImageView 并具有相同症状的小型测试应用程序.我在 Layout 和 ScrollView 上尝试了诸如 requestLayout() 之类的各种方法,我尝试了 scrollTo(Integer.MAX_VALUE) 并且它滚动到了下界:) 我是否违反了 UI 线程问题或其他什么?

In my app I am using a custom View object, but I made a small test application that uses ImageView and has the same symptom. I tried various things like requestLayout() on both the Layout and ScrollView, I tried scrollTo(Integer.MAX_VALUE) and it scrolled into the netherverse :) Am I violating a UI thread issue or something?

  • 瑞克

======

    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 的宽度还没有机会扩大.

I think there's a timing issue. Layout isn't done when a view is added. It is requested and done a short time later. When you call fullScroll immediately after adding the view, the width of the linearlayout hasn't had a chance to expand.

尝试更换:

s.fullScroll(HorizontalScrollView.FOCUS_RIGHT);

与:

s.postDelayed(new Runnable() {
    public void run() {
        s.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
    }
}, 100L);

短暂的延迟应该给系统足够的时间来解决.

The short delay should give the system enough time to settle.

附言简单地将滚动延迟到 UI 循环的当前迭代之后可能就足够了.我没有测试过这个理论,但如果它是正确的,那么做以下事情就足够了:

P.S. It might be sufficient to simply delay the scrolling until after the current iteration of the UI loop. I have not tested this theory, but if it's right, it would be sufficient to do the following:

s.post(new Runnable() {
    public void run() {
        s.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
    }
});

我更喜欢这个,因为引入任意延迟对我来说似乎很棘手(即使这是我的建议).

I like this better because introducing an arbitrary delay seems hacky to me (even though it was my suggestion).

这篇关于Horizo​​ntalScrollView:添加新视图时自动滚动到结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆