相似微信首頁彈性滾動和慣性滾動效果的實現——OverScroll

OverScroll

利用CoordinatorLayout+Behavior實現列表彈性滾動和慣性滾動效果(相似微信首頁),支持水平和垂直方向的滾動,效果以下:php

vertical over-scroll
horizontal over-scroll
nested over-scroll

Usage 用法

Gradle

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, OverScrollVerticalRecyclerViewOverScrollScrollViewandroid

如今你的佈局就實現了彈性滾動和慣性滾動效果啦!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

項目地址OverScroll

多謝支持個人github項目>>>OverScroll微信

相關文章
相關標籤/搜索