繁体   English   中英

Recyclerview 向上滑动以在 API 21 上的 android 中刷新

[英]Recyclerview swipe up to refresh in android on API 21

我想在回收站视图中“向上滑动以刷新内容”功能(类似于SwipeRefreshLayout )。

目前我有一个按钮可以在点击时刷新视图,我想使用向上滑动来做同样的事情。 唯一的问题是SwipeRefreshLayout自 API 22 起可用。

使用 API 21 时是否可以完成?

使用 android.support.v4.widget.SwipeRefreshLayout。 在您的 build.gradle 中添加compile 'com.android.support:support-v4:xxx'其中 xxx 是支持库的最新版本。

您可以使用支持库 v4 中的类android.support.v4.widget.SwipeRefreshLayout

build.gradle添加依赖项:

compile 'com.android.support:support-core-ui:26.1.0'

您可以在此处找到官方文档中的所有详细信息

您可以使用android.support.v4.widget.SwipeRefreshLayout 虽然我发现支持版本有问题,然后我不得不像这样修改SwipeRefreshLayout

import android.app.Activity;
import android.content.Context;
import android.support.design.widget.AppBarLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;

public class CustomSwipeRefreshLayout extends SwipeRefreshLayout implements AppBarLayout.OnOffsetChangedListener {
    private AppBarLayout appBarLayout;

    public CustomSwipeRefreshLayout(Context context) {
        super(context);
    }

    public CustomSwipeRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (getContext() instanceof Activity) {
            appBarLayout = (AppBarLayout) ((Activity) getContext()).findViewById(R.id.appbar);
            if (appBarLayout != null)
                appBarLayout.addOnOffsetChangedListener(this);
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        if (appBarLayout != null) {
            appBarLayout.removeOnOffsetChangedListener(this);
            appBarLayout = null;
        }
        super.onDetachedFromWindow();
    }

    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int i) {
        this.setEnabled(i == 0);
    }
}

现在像这样实现自定义的SwipeRefreshLayout

<?xml version="1.0" encoding="utf-8"?>
<your.package.name.CustomView.CustomSwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_refresh_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white">


    <!-- Your RecyclerView -->

</your.package.name.CustomView.CustomSwipeRefreshLayout>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM