SwipeRefresh 不适用于片段 - API 19

SwipeRefresh Doesn't Work On Fragments - API 19

我目前正在实施 swipeRefreshLayout 问题是,它仍然没有出现。我试图在这个 ,and this link ,and this link 中实现一些答案。

我的 gradle 模块:compile 'com.android.support:appcompat-v7:23.1.1'

这是我的示例 xml:recycler_view

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/recycler_relative_root"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/feeds_swipe_refresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:scrollbars="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.v4.widget.SwipeRefreshLayout>

Java代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    //INFLATE
        mRelativeLayoutRoot = (RelativeLayout) inflater.inflate(R.layout.recycler_view,container,false)
                                                       .findViewById(R.id.recycler_relative_root);
    //MAP SWIPE REFRESH
        mSwipeRefreshLayout = (SwipeRefreshLayout) mRelativeLayoutRoot.findViewById(R.id.feeds_swipe_refresh);

    //MAP RECYCLER VIEW
        recyclerView = (RecyclerView) mRelativeLayoutRoot.findViewById(R.id.my_recycler_view);

    //RECYCLER VIEW LAYOUT
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setHasFixedSize(true);

    //ORG ADAPTER
        orgListAdapter = new OrgListAdapter(organizationDataList);
        recyclerView.setAdapter(orgListAdapter);

    //GET DATA REQUEST
        parseJSONData();

    return recyclerView;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            parseJSONData();
        }
    });

    mSwipeRefreshLayout.post(new Runnable() {
        @Override
        public void run() {
            mSwipeRefreshLayout.setRefreshing(true);
        }
    });

    mSwipeRefreshLayout.setColorSchemeColors(R.color.appIntroColorFifth,
            R.color.appIntroColorFourth,
            R.color.appIntroColorOne,
            R.color.appIntroColorTwo);
}

每当我向下滑动时,没有任何反应,也不会显示滑动刷新。 我在模拟器中的实际设备 运行 API 19 (Kitkat)API 23 (Marshmallow) 上进行了测试。

您将在 onCreateView() 中返回您的 RecyclerView,而不是整个视图,因此您的片段仅显示您的 RecyclerView。您根本不需要获取根布局,只需创建视图并使用它来查找子视图即可。并确保您 return 根视图:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.recycler_view, container, false);

    mRecyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view);
    mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.feeds_swipe_refresh);

    // Your other create view code here...

    return view;
}

如果你真的需要根 RelativeLayout 你可以调用:

mRelativeLayout = (RelativeLayout) view;