利用CoordinatorLayout+Behavior實現列表彈性滾動和慣性滾動效果(相似微信首頁),支持水平和垂直方向的滾動,效果以下:php
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.1993hzw:OverScroll:1.1.1'
}
複製代碼
在xml佈局文件中添加相似代碼:java
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">
...
<cn.forward.overscroll.view.OverScrollVerticalRecyclerView android:background="#0ff" android:id="@+id/overscroll_view" android:layout_width="match_parent" android:layout_height="match_parent" />
...
</android.support.design.widget.CoordinatorLayout>
複製代碼
固然你能夠在CoordinatorLayout
中使用OverScrollHorizontalRecyclerView
, OverScrollVerticalRecyclerView
或 OverScrollScrollView
.android
如今你的佈局就實現了彈性滾動和慣性滾動效果啦!git
你還能夠拓展該控件以實現更復雜的交互效果,設置 IOverScrollCallback 或者添加 IOffsetChangeListener.github
IOverScrollView overScrollView = findViewById(R.id.overscroll_view);
overScrollView.setOverScrollCallback(new IOverScrollCallback() {
@Override
public boolean canScroll(IOverScroll overScroll, View child, int scrollDirection) {
...
}
@Override
public int getMaxFlingOffset(IOverScroll overScroll, View child, int scrollDirection) {
...
}
@Override
public float getDampingFactor(IOverScroll overScroll, View child, int scrollDirection) {
...
}
@Override
public int getMinFlingVelocity(IOverScroll overScroll, View child, int scrollDirection) {
...
}
@Override
public void onOffsetChanged(IOverScroll overScroll, View child, int offset) {
...
}
@Override
public boolean onSpringBack(IOverScroll overScroll, View child) {
...
}
@Override
public void onStopSpringingBack(IOverScroll overScroll, View child) {
...
}
});
overScrollView.addOffsetChangeListener(new IOffsetChangeListener() {
@Override
public void onOffsetChanged(View child, int offset) {
...
}
});
複製代碼
(默認IOverScrollCallback
接口的實現爲SimpleOverScrollCallback)bash
多謝支持個人github項目>>>OverScroll!微信