gpt4 book ai didi

java - 如何在android中创建可弹跳的 ScrollView ?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:18:09 25 4
gpt4 key购买 nike

如何在 Android 中像 iPhone 一样创建弹跳 ScrollView

最佳答案

在android中给scrollview添加弹跳效果

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

public class BounceScrollView extends ScrollView
{
private static final int MAX_Y_OVERSCROLL_DISTANCE = 200;

private Context mContext;
private int mMaxYOverscrollDistance;

public BounceScrollView(Context context)
{
super(context);
mContext = context;
initBounceScrollView();
}

public BounceScrollView(Context context, AttributeSet attrs)
{
super(context, attrs);
mContext = context;
initBounceScrollView();
}

public BounceScrollView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
mContext = context;
initBounceScrollView();
}

private void initBounceScrollView()
{
//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);
}

}

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

<ScrollView 
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

<com.base.view.BounceScrollView 
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

关于java - 如何在android中创建可弹跳的 ScrollView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7470267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com