58

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

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

在我的应用程序中,我使用的是自定义 View 对象,但我制作了一个使用 ImageView 并具有相同症状的小型测试应用程序。我在 Layout 和 ScrollView 上尝试了各种类似 requestLayout() 的方法,我尝试了 scrollTo(Integer.MAX_VALUE) 并且它滚动到了下界:) 我是在违反 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>
4

9 回答 9

142

我认为有一个时间问题。添加视图时未完成布局。它被请求并在短时间内完成。添加视图后立即调用 fullScroll 时,线性布局的宽度还没有机会扩大。

尝试更换:

s.fullScroll(HorizontalScrollView.FOCUS_RIGHT);

和:

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

短暂的延迟应该给系统足够的时间来稳定。

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

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

我更喜欢这个,因为引入任意延迟对我来说似乎很老套(尽管这是我的建议)。

于 2011-01-18T04:44:04.823 回答
18

我同意@monxalo 和@granko87 的观点,更好的方法是使用监听器,而不是假设如果我们让任意时间过去,布局就会完成。

如果像我一样,您不需要使用自定义 Horizo​​ntalScrollView 子类,您只需添加一个 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);
    }
});
于 2015-03-03T19:36:23.837 回答
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();
    }
}
于 2011-03-16T15:54:21.977 回答
4

使用 View 方法 smoothScrollTo

postDelayed(new Runnable() {
    public void run() {
       scrollView.smoothScrollTo(newView.getLeft(), newView.getTop());
 }
 }, 100L);

您需要放入一个可运行文件,以便为布局过程留出时间来定位新视图

于 2013-02-04T11:22:06.053 回答
3

This is what I did.

//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);
            }
     });
   }
于 2015-02-08T15:01:55.003 回答
3

使用下面的代码进行水平滚动视图 {向右滚动}

view.post(new Runnable() {
                @Override
                public void run() {
                    ((HorizontalScrollView) Iview
                            .findViewById(R.id.hr_scroll))
                            .fullScroll(HorizontalScrollView.FOCUS_RIGHT);
                }
            });
于 2014-08-24T00:03:29.643 回答
1

我认为最好的方法是 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);
}
于 2013-05-15T12:17:58.157 回答
0

孩子View可能不会立即添加到ViewGroup. 因此,需要发布一个Runnable将在其他待处理任务完成后运行的。

所以在添加之后View,使用:

horizontalScrollView.post(new Runnable() {
    @Override
    public void run() {
        horizontalScrollView.fullScroll(View.FOCUS_RIGHT);
    }
});
于 2018-03-06T07:25:34.373 回答
0

这是我在 kotlin 中使用 3 个 ImageViews 的方式:

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)
于 2019-05-13T20:01:36.280 回答