HorizontalScrollView: auto-scroll to end when new Views are added?

后端 未结 9 1183
鱼传尺愫
鱼传尺愫 2020-11-28 06:37

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

9条回答
  •  感情败类
    2020-11-28 07:07

    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);
                }
         });
       }
    

提交回复
热议问题