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

后端 未结 9 1182
鱼传尺愫
鱼传尺愫 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:11

    Just another sugestion, since this question helped me a lot :).

    You can put a listener when the view has finished its layout phase, and right after do the fullScroll althought you'll need to extend the class for that.

    I only did this because i wanted to scroll to a section right after onCreate() to avoid that flickering from starting point to scroll point.

    Something like:

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

提交回复
热议问题