How to trigger an event when scrollView reach the bottom with Android?

前端 未结 5 1007
情话喂你
情话喂你 2020-12-10 03:48

Im looking for an event that I can use on the scrollView to be fired when the user has scrolled to the bottom.

E.g my list of items should be extended with more item

5条回答
  •  旧时难觅i
    2020-12-10 04:14

    Please read this article: Android: Understanding When Scrollview Has Reached The Bottom

    Source code:

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) 
    {
        // Grab the last child placed in the ScrollView, we need it to determinate the bottom position.
        View view = (View) getChildAt(getChildCount()-1);
    
        // Calculate the scrolldiff
        int diff = (view.getBottom()-(getHeight()+getScrollY()));
    
        // if diff is zero, then the bottom has been reached
        if( diff == 0 )
        {
            // notify that we have reached the bottom
            Log.d(ScrollTest.LOG_TAG, "MyScrollView: Bottom has been reached" );
        }
    
        super.onScrollChanged(l, t, oldl, oldt);
    }
    

    If you want you can add make a custom widget.

    Example:

    package se.marteinn.ui;
    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.View;
    import android.widget.ScrollView;
    
    
    /**
     * Triggers a event when scrolling reaches bottom.
     *
     * Created by martinsandstrom on 2010-05-12.
     * Updated by martinsandstrom on 2014-07-22.
     *
     * Usage:
     *
     *  scrollView.setOnBottomReachedListener(
     *      new InteractiveScrollView.OnBottomReachedListener() {
     *          @Override
     *          public void onBottomReached() {
     *              // do something
     *          }
     *      }
     *  );
     * 
     *
     * Include in layout:
     *  
     *  
     *  
     */
    public class InteractiveScrollView extends ScrollView {
        OnBottomReachedListener mListener;
    
        public InteractiveScrollView(Context context, AttributeSet attrs,
                int defStyle) {
            super(context, attrs, defStyle);
        }
    
        public InteractiveScrollView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public InteractiveScrollView(Context context) {
            super(context);
        }
    
        @Override
        protected void onScrollChanged(int l, int t, int oldl, int oldt) {
            View view = (View) getChildAt(getChildCount()-1);
            int diff = (view.getBottom()-(getHeight()+getScrollY()));
    
            if (diff == 0 && mListener != null) {
                mListener.onBottomReached();
            }
    
            super.onScrollChanged(l, t, oldl, oldt);
        }
    
    
        // Getters & Setters
    
        public OnBottomReachedListener getOnBottomReachedListener() {
            return mListener;
        }
    
        public void setOnBottomReachedListener(
                OnBottomReachedListener onBottomReachedListener) {
            mListener = onBottomReachedListener;
        }
    
    
        /**
         * Event listener.
         */
        public interface OnBottomReachedListener{
            public void onBottomReached();
        }
    
    }
    

提交回复
热议问题