有什么方法可以将 ListView 的反弹效果添加到常规滚动视图中?弹跳是指当您到达列表底部时的橡皮筋效果。
6 回答
在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"
/>
从 API 的外观来看,如果您创建扩展类的自定义视图,ScrollView
您应该能够覆盖该方法。在做了一个快速的谷歌搜索后,我发现了这个链接,看起来这就是你想要做的……我相信这个方法是在 Android 2.3.1 中添加的,所以你将被限制在运行它的设备上。onOverScrolled()
ScrollView
我找到了 BounceListView 的最佳实现(在 LGPL 许可下)。这是:https ://github.com/Larphoid/android-Overscroll-ListView
您可能拥有定制的三星设备。您应该知道反弹效果不是 Android 操作系统的默认行为,它是三星引入的东西(而且它的实现也很差,他们应该使ScrollView
行为相同)。在 Android 2.3 中引入了过度滚动支持,默认行为不是弹跳,而是一种强度与滚动速度/“力”成正比的光辉。它适用于任何地方(列表视图、滚动视图、网络视图等)。
总之,你不应该担心这一点。没有简单的参数可以传递给ScrollView
它以使其像那样过度滚动。IMO ,经历扩展ScrollView
课程的所有麻烦是不值得的。只依赖默认行为。
如果三星想惹恼他们的用户并给他们一个不一致的用户界面,那就这样吧。
对于那些想要在ListView
.
如何
添加此效果的一种方法是使用addHeaderView
and addFooterView
in ListView
,并且它们的填充(topPadding
用于页眉视图和bottomPadding
页脚视图)是0
第一次设置为,然后我们覆盖onTouchEvent
, 并根据移动距离更改填充。
执行
笔记
这个想法是从android-pulltorefresh借来的,因为弹跳效果比 pull-to-refresh 更简单,所以代码也更短。^_^
希望这会对某人有所帮助..
对于在 NestedScrollView 上寻找弹跳效果的任何人,我制作了一个库: https ://github.com/Valkriaine/Bouncy
用法:
在您的应用模块 build.gradle 中:
dependencies {
implementation 'com.factor:bouncy:1.8'
// if you want BouncyRecyclerView too, add implementation for recyclerview
implementation 'androidx.recyclerview:recyclerview:1.1.0'
}
并BouncyNestedScrollView
用作普通的 NestedScrollView:
<com.factor.bouncy.BouncyNestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:fling_animation_size=".7"
app:overscroll_animation_size=".7">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
...
...
...
</LinearLayout>
</com.factor.bouncy.BouncyNestedScrollView>
fling_animation_size
指定投掷的过度滚动效果的大小,如果没有给出值,则默认为 0.5。
overscroll_animation_size
指定拖动的过度滚动效果的大小,如果没有给出值,默认为 0.5。
强烈建议将两个值都保持在 5 以下。
BouncyNestedScrollView
基于 NestedScrollView 的源代码进行了修改,因此从技术上讲,它应该可以与 NestedScrollView 的所有现有自定义选项一起使用。