下拉android时SwipeRefreshLayout加载器未启动 [英] SwipeRefreshLayout loader not going up when pull down android

查看:88
本文介绍了下拉android时SwipeRefreshLayout加载器未启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将SwipeRefreshLayout与recyclerview一起使用,它在所有android版本中都可以正常工作,但是在Kitkat中,当我下拉时,SwipeRefreshLayout加载器不会上升,也不会刷新recyclerview数据,但可以在棒棒糖中工作

I have used SwipeRefreshLayout with recyclerview, Its working fine in all android version but in Kitkat when i pull down then SwipeRefreshLayout loader not going up and also doesn't refresh recyclerview data but working in lollipop

我正在使用'com.android.support:support-v4:22.2.1'

I am using 'com.android.support:support-v4:22.2.1'

编辑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的直接子级时,似乎会发生这种情况.

I have had the same issue before. It seems to happen when you don't have your RecyclerView as a direct child of your SwipeRefreshLayout.

我们通过使用这样的自定义SwipeRefreshLayout解决了此问题:

We fixed this issue by using a custom SwipeRefreshLayout like this one:

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',如下所示:

Then, instead of using android.support.v4.widget.SwipeRefreshLayout use your own 'com.example.app.FixedSwipeRefreshLayout' like this:

<?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:

And finally, when you get a handle of your SwipeRefreshLayout, use your custom one and set your 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);

希望这会有所帮助.

这篇关于下拉android时SwipeRefreshLayout加载器未启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆