如何在AppBarLayout中添加pull to refresh

mia*_*shu 7 android android-coordinatorlayout android-appbarlayout

我发现大部分材料都是讨论在AppBarLayout下面的区域添加"pull to refresh",比如SwipeRefreshLayout,我想知道如何在AppBarLayout中执行此操作,这意味着:

在此输入图像描述

下拉时,拉动刷新指示符出现在AppBarLayout中.

怎么实现这个?谢谢

================= UPDATE ===================

我终于以自己的方式解决了这个问题.是录制简单UI的视频链接.

关键点是我从github 重写了appbarlayout-spring-behavior.这真的很有帮助.简单地说,我重写了AppBarLayoutSpringBehavior,在draging时添加了Pull-To-Refresh-Callback,并添加了一些逻辑来显示pull-to-refresh动画,以防止简单地动画回到原始状态.过了一段时间,然后我告诉行为动画回来.

虽然重写代码看起来很难看,但似乎可行.我将重构我的代码,使其干净和模块化

Fra*_*and 0

您可以尝试这样的视图层次结构

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="?attr/actionBarSize">

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/appBar"/>
    </android.support.v4.widget.SwipeRefreshLayout>

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"/>
    </android.support.design.widget.AppBarLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

在您的 Java 类中,您可以像这样使用 SwipeRefreshLayout

swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
swipeRefreshLayout.setProgressViewOffset(false, 0, (int) (swipeRefreshLayout.getPaddingTop() * 1.5));
swipeRefreshLayout.setOnRefreshListener(() -> {
    swipeRefreshLayout.setRefreshing(true);
    // long process here
    new Handler().postDelayed(() -> runOnUiThread(() -> swipeRefreshLayout.setRefreshing(false)), 2000);
});
Run Code Online (Sandbox Code Playgroud)