SwipeRefresh不会处理片段 - API 19

时间:2016-06-09 11:58:46

标签: android android-fragments android-recyclerview swiperefreshlayout

我目前正在实施swipeRefreshLayout问题,但它仍然没有显示出来。我尝试在此linklink以及此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)的实际设备上对此进行测试。

1 个答案:

答案 0 :(得分:0)

您在RecyclerView而不是整个视图中都返回了onCreateView(),因此您的片段仅显示您的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;