API 21上android中的Recyclerview刷起刷新

API 21上android中的Recyclerview刷起刷新,android,android-recyclerview,refresh,android-support-library,swipe,Android,Android Recyclerview,Refresh,Android Support Library,Swipe,我想在“回收器”视图中使用“刷起以刷新内容”功能(类似于SwipeRefreshLayout) 目前,我有一个按钮,当点击时刷新视图,我想用向上滑动来做同样的事情。唯一的问题是SwipeRefreshLayout从API 22开始就可用 使用API 21时是否可以执行此操作?使用android.support.v4.widget.swiperFreshLayout。 添加build.gradle编译'com.android.support:support-v4:x.x.x' 其中x.x.x是最新

我想在“回收器”视图中使用“刷起以刷新内容”功能(类似于
SwipeRefreshLayout

目前,我有一个按钮,当点击时刷新视图,我想用向上滑动来做同样的事情。唯一的问题是
SwipeRefreshLayout
从API 22开始就可用


使用API 21时是否可以执行此操作?

使用android.support.v4.widget.swiperFreshLayout。 添加build.gradle
编译'com.android.support:support-v4:x.x.x'

其中x.x.x是最新版本的支持库。

使用android.support.v4.widget.SwipeRefreshLayout。 添加build.gradle
编译'com.android.support:support-v4:x.x.x'

其中x.x.x是支持库的最新版本。

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

添加您的
build.gradle
依赖项:

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

在这里您可以在官方文档中找到。

您可以使用支持库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);
    }
}
<?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>
现在像这样实现自定义
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);
    }
}
<?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>

您可以使用
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);
    }
}
<?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>
现在像这样实现自定义
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);
    }
}
<?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>