我有一个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?立即强制一个空适配器?