【问题标题】:SwipeRefreshLayout with RecyclerView not working in Fragment带有 RecyclerView 的 SwipeRefreshLayout 在片段中不起作用
【发布时间】:2019-10-06 13:10:00
【问题描述】:

我在片段中的 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
        }
    }

}

【问题讨论】:

  • 添加SwipeRefreshLayout作为根布局。
  • @MehulSolanki 我不想,因为我有一些视图要添加到 SwipeRefreshLayout 上方
  • 你不能在滑动上方有东西来刷新布局,它需要是根。您可以在回收站视图上方放置“东西”,但不能在 swiperefresh 上方放置
  • @JoachimHaglund 这可能不是真的。我已经测试过了,你可以在 SwipeRefreshLayout 上方看到视图。
  • 您可以尝试在您的LinearLayout 中添加android:clickable="true"。这可能会有所帮助

标签: android android-recyclerview android-asynctask swiperefreshlayout


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    可能由于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);
       }
    

    【讨论】:

      【解决方案3】:

      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>
      

      【讨论】:

      • 我已经尝试过第一个解决方案,没有运气。其次,由于我已经迁移到 androidx,我无法使用支持库。
      【解决方案4】:

      尝试使用下面的 sn-p:

      始终使用支持库小部件。而不是这些:

      <androidx.recyclerview.widget.RecyclerView
      

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

      【讨论】:

      • 支持库被贬值了,androidX 是新的做事方式和功能,没有什么不同。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-18
      相关资源
      最近更新 更多