带有RecyclerView的SwipeRefreshLayout在片段中不起作用

时间:2019-05-20 07:38:22

标签: android android-recyclerview android-asynctask swiperefreshlayout

我在SwipeRefreshLayout的片段中有一个recyclerview。我已经实现了AsyncTask来为recyclerview获取数据。问题是SwipeRefreshLayout根本不显示,因此数据不会在recyclerview中填充。我为Activity做过同样的事情,它工作得很好,但不是零碎的。有人可以指导我我做错了什么吗? 这是片段的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".FeatureFragment">

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        </androidx.recyclerview.widget.RecyclerView>

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</LinearLayout>

这是我在片段类中所做的。

public class FeaturedFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
    private RecyclerView mRecyclerView;
    private SwipeRefreshLayout mSwipeLayout;
    public FeaturedFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_featured_captions, container, false);
        mRecyclerView = view.findViewById(R.id.recyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mSwipeLayout = view.findViewById(R.id.refresh_layout);
        mSwipeLayout.setOnRefreshListener(this);
        return view;
    }

    @Override
    public void onRefresh() {
        new FetchFeedTask().execute((Void) null);
    }


    private class FetchFeedTask extends AsyncTask<Void, Void, Boolean> {
        @Override
        protected void onPreExecute() {
            mSwipeLayout.setRefreshing(true);
        }

        @Override
        protected Boolean doInBackground(Void... voids) {
            //things to do in background
        }

        @Override
        protected void onPostExecute(Boolean success) {
            mSwipeLayout.setRefreshing(false);
            mRecyclerView.setAdapter(/*adapter*/);
            //things to do at the end
        }
    }

}

4 个答案:

答案 0 :(得分:1)

在这里回答为时已晚,但可能会帮助其他人。 我遇到了同样的问题,这是因为 recyclerView 是 swipeRefreshLayout 的直接子级,所以只需将 recyclerView 放在布局中(例如 ConstraintLayout),然后将此布局放在您的 swipeRefreshLayout 中,一切都会按您的预期工作。

答案 1 :(得分:0)

可能由于ReacyclerView滚动Listener而导致代码无法正常工作 请尝试以下内容。

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener(){
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        int topRowVerticalPosition =
                (recyclerView == null || recyclerView.getChildCount() == 0) ? 0 : recyclerView.getChildAt(0).getTop();
        mSwipeLayout.setEnabled(topRowVerticalPosition >= 0);

    }

    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
    }
});

另外,将您的初始化内容添加到onViewCreated()内,如下所示

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_featured_captions, container, false);
        return view;
    }



    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mRecyclerView = view.findViewById(R.id.recyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mSwipeLayout = view.findViewById(R.id.refresh_layout);
        mSwipeLayout.setOnRefreshListener(this);
   }

答案 2 :(得分:0)

尝试使用以下代码段:

//Always use support library widgets. instead of these
 <androidx.recyclerview.widget.RecyclerView and
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
    </android.support.v4.widget.SwipeRefreshLayout>

答案 3 :(得分:0)

  

swipelayout的高度更改为wrap_content

解决方案1 ​​:)

 <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

解决方案2 :)通过设计库使用滑动布局和回收站视图

<android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
         <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

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