Android: Listview's bounce to scrollview

后端 未结 6 864
生来不讨喜
生来不讨喜 2021-02-02 00:33

Any way of adding ListView\'s bounce effect to regular scrollview? By bounce I mean the rubber band like effect when you hit the bottom of the list.

6条回答
  •  日久生厌
    2021-02-02 00:49

    Add effect bounce to listview in android

    Step 1: Create new file BounceListView in package com.base.view

    public class BounceListView extends ListView
    {
        private static final int MAX_Y_OVERSCROLL_DISTANCE = 200;
    
        private Context mContext;
        private int mMaxYOverscrollDistance;
    
        public BounceListView(Context context) 
        {
            super(context);
            mContext = context;
            initBounceListView();
        }
    
        public BounceListView(Context context, AttributeSet attrs) 
        {
            super(context, attrs);
            mContext = context;
            initBounceListView();
        }
    
        public BounceListView(Context context, AttributeSet attrs, int defStyle) 
        {
            super(context, attrs, defStyle);
            mContext = context;
            initBounceListView();
        }
    
        private void initBounceListView()
        {
            //get the density of the screen and do some maths with it on the max overscroll distance
            //variable so that you get similar behaviors no matter what the screen size
    
            final DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
                final float density = metrics.density;
    
            mMaxYOverscrollDistance = (int) (density * MAX_Y_OVERSCROLL_DISTANCE);
        }
    
        @Override
        protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) 
        { 
            //This is where the magic happens, we have replaced the incoming maxOverScrollY with our own custom variable mMaxYOverscrollDistance; 
            return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, mMaxYOverscrollDistance, isTouchEvent);  
        }
    
    }
    

    Step 2: At your layout, please change

    
    

    to

    
    

提交回复
热议问题