Android:Listview反弹到scrollview

Jay*_*ayp 13 android android-widget android-animation android-scrollview

有什么方法可以将ListView的弹跳效果添加到常规scrollview中?通过弹跳我的意思是当你触到列表的底部时橡皮筋就像效果一样.

小智 18

添加效果反弹到android中的listview

第1步:在com.base.view包中创建新文件BounceListView

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

}
Run Code Online (Sandbox Code Playgroud)

第2步:在您的布局中,请更改

<ListView 
   android:id="@+id/list"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
/>
Run Code Online (Sandbox Code Playgroud)

<com.base.view.BounceListView 
   android:id="@+id/list"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
/>
Run Code Online (Sandbox Code Playgroud)


DRi*_*FTy 11

根据ScrollViewAPI中的内容,onOverScrolled()如果您创建扩展ScrollView该类的自定义视图,则应该能够覆盖该方法.在快速谷歌搜索后,我遇到了这个链接,看起来好像这是你想要做的......我相信这个方法是在Android 2.3.1中添加的,所以你将被限制在运行它的设备上.


mac*_*ing 5

我找到了BounceListView的最佳实现(在LGPL许可下).这是:https://github.com/Larphoid/android-Overscroll-ListView