android - 带有 RecyclerView 的 SwipeRefreshLayout 在 Fragment 中不起作用

标签 android android-recyclerview android-asynctask swiperefreshlayout

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

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

这是我在 fragment 类中所做的。

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
        }
    }

}

最佳答案

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

关于android - 带有 RecyclerView 的 SwipeRefreshLayout 在 Fragment 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56216273/

相关文章:

java - ksoap2 发送复杂对象时出错

android - 如何获取图像资源 ID 并将其发送到 Android 中的其他 Activity ?

android - 如何定义android中Horizo​​ntal recyclerview中的项目数量?

java - 如何向 x 次 TCP 客户端请求 TCP 服务器数据?

Android 进度对话框未显示在 AsyncTask 中

android - publishProgress 不调用 onProgressUpdate

android - 如何使用自定义主题设置 DialogPreference 的样式?

android - flutter : Overlaying the graphics on the barcode which is detected by firebase vision

java - 我正在尝试使用 getter 和 setter 从我的 firebase 数据库中检索数据,但收到错误消息说无法转换对象

android - 如何在不同的进程中使用 AsyncTask?