我已经将 SwipeRefreshLayout 与 recyclerview 一起使用,它在所有 android 版本中工作正常,但在 Kitkat 中,当我下拉时,SwipeRefreshLayout 加载程序不会上升,也不会刷新 recyclerview 数据,但在棒棒糖中工作
我正在使用 'com.android.support:support-v4:22.2.1'
Edit 1
SwipeRefreshLayout swipe_refresh;
swipe_refresh = (SwipeRefreshLayout)rootView.findViewById(R.id.swipe_refresh);
swipe_refresh.post(new Runnable() {
@Override
public void run() {
swipe_refresh.setRefreshing(true);
}
});
swipe_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
// Loading more data..
.....
....
getData();
}
});
swipe_refresh.setColorSchemeResources(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
public void getData(){
// Load data from web service...
...
...
...
// After loading
swipe_refresh.setRefreshing(false);
// Set data to adapter
}
布局代码
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_refresh"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@color/grey_100">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/gv_feeds"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"
android:scrollbars="vertical"/>
<ProgressBar
android:id="@+id/prog_load_more"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:visibility="gone"/>
</LinearLayout>
</FrameLayout>
</android.support.v4.widget.SwipeRefreshLayout>
我以前也遇到过同样的问题。当您没有将 RecyclerView 作为 SwipeRefreshLayout 的直接子级时,似乎会发生这种情况。
我们通过使用自定义 SwipeRefreshLayout 解决了这个问题,如下所示:
public class FixedSwipeRefreshLayout extends SwipeRefreshLayout {
private RecyclerView recyclerView;
public FixedSwipeRefreshLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FixedSwipeRefreshLayout(Context context) {
super(context);
}
public void setRecyclerView(RecyclerView recyclerView) {
this.recyclerView = recyclerView;
}
@Override
public boolean canChildScrollUp() {
if (recyclerView!=null) {
return recyclerView.canScrollVertically(-1);
}
return false;
}
}
然后,不要使用android.support.v4.widget.SwipeRefreshLayout
使用您自己的“com.example.app.FixedSwipeRefreshLayout”,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<com.example.app.FixedSwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_refresh"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
</com.example.app.FixedSwipeRefreshLayout>
最后,当您获得 SwipeRefreshLayout 的句柄时,使用您的自定义句柄并设置您的 RecyclerView:
FixedSwipeRefreshLayout swipe_refresh;
swipe_refresh = (FixedSwipeRefreshLayout) rootView.findViewById(R.id.swipe_refresh);
RecyclerView mRecyclerView = (RecyclerView) rootView.findViewById(R.id.gv_feeds);
swipe_refresh.setRecyclerView(mRecyclerView);
希望这可以帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)