gpt4 book ai didi

Android: Listview 弹跳到 scrollview

转载 作者:可可西里 更新时间:2023-11-01 19:09:57 25 4
gpt4 key购买 nike

有什么方法可以将 ListView 的弹跳效果添加到常规 ScrollView 中?我所说的弹跳是指当您击中列表底部时产生的类似橡皮筋的效果。

最佳答案

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

}

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

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

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

关于Android: Listview 弹跳到 scrollview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7201907/

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