我希望“刷新内容的滑动”功能在回收器视图(类似于SwipeRefreshLayout)。
目前,我有一个按钮,刷新视图时,点击,我想做同样的使用滑动向上。唯一的问题是,从API 22开始SwipeRefreshLayout是可用的。
当使用API 21时,是否可以这样做?
发布于 2017-09-30 08:36:55
使用android.support.v4.widget.SwipeRefreshLayout。添加build.gradle compile 'com.android.support:support-v4:x.x.x',其中x.x.x是支持库的最后一个版本。
发布于 2017-09-30 08:40:28
您可以使用类android.support.v4.widget.SwipeRefreshLayout,它位于支持库v4中。
在您的build.gradle中添加依赖项:
compile 'com.android.support:support-core-ui:26.1.0'在这里,您可以在官方文档中找到所有细节。
发布于 2017-09-30 08:42:06
您可以使用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>https://stackoverflow.com/questions/46501054
复制相似问题