10

我有一个Fragment和一个SwipeRefreshLayout然后RecyclerView作为孩子。当 Fragment 提交时,它开始与服务器通信以检索一些数据并填充将为 RecyclerView 提供服务的 CustomAdapter。

用户可以通过向下滑动或按下 ActionBar 上的按钮来刷新内容。在后一种情况下,我手动调用swipeRefreshLayout.setRefreshing(true). 所以直到现在都没有麻烦。

当我在第一次加载 RecyclerView ( onStart()) 期间手动调用刷新状态时,就会出现问题。进度指示器没有显示,我假设是因为 RecyclerView 仍然是空的,因为它需要几秒钟来检索所有数据......

我给你留一些代码。

片段代码:

public class MyFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {


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

        View mView = inflater.inflate(R.layout.fragment_stop, container, false);
        mRecyclerView = (RecyclerView) mView.findViewById(R.id.recyclerView1);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(),
             DividerItemDecoration.VERTICAL_LIST));
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());

       //SwipeRefreshLayout retrieved and  configured
       swipeLayout = (SwipeRefreshLayout) mView.findViewById(R.id.swipe_container);
       swipeLayout.setOnRefreshListener(this);
       swipeLayout.setColorSchemeResources(
            R.color.primaryColor,
            R.color.cyan_500,
            R.color.light_green_500,
            R.color.amber_500);

        return mView;


     }

    ...
    @Override
        public void onStart() {
            super.onStart();
            executeSchedule(); //First execution and data loading.
        }
        ...

    @Override
    public void onRefresh() {
        executeSchedule();
    }
    ...
    public void executeSchedule() {
        new Schedule().execute();
    }



    private class Schedule extends AsyncTask<Void, Void, Void> {

        ...
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            if(!swipeLayout.isRefreshing()) {
                swipeLayout.setRefreshing(true); //set refresh state
            }
        }

        @Override
        protected Void doInBackground(Void... params) {
        ...
        }

        @Override
        protected void onPostExecute(Void result) {
            adap = new CustomAdapter(getActivity(), ...);
            mRecyclerView.setAdapter(adap);
            swipeLayout.setRefreshing(false);
        }

}

XML 代码:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/section_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <android.support.v4.widget.SwipeRefreshLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/swipe_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingLeft="4dp"
                android:paddingRight="4dp"
                android:paddingBottom="2dp"
                android:layout_marginTop="5dp"
                android:divider="#e0e0e0"
                tools:context=".MyFragment" />

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

解决这种情况的最佳做法是什么?添加第二个虚拟 SwipeRefreshLayout?立即强制一个空适配器?

4

3 回答 3

17

我遇到了SwipeRefreshLayout不使用空回收器的问题,所以我创建了一个非常简单的空适配器。

public class EmptyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public int getItemCount() {
        return 0;
    }
}
于 2016-01-14T09:01:03.827 回答
7

我只是在 SwipeRefreshLayout 中将 RecyclerView 与 RelativeLayout 包装起来,问题就解决了。

<android.support.v4.widget.SwipeRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/swipeContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

</android.support.v7.widget.RecyclerView>
</RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>
于 2018-11-02T16:16:50.497 回答
0

您需要在 call 之前调用 measure 方法setRefreshing(true)
看看:SwipeRefreshLayout no animation on fragment creation。也许它会有所帮助。

于 2014-11-02T18:27:54.557 回答