我有一个Fragment
,SwipeRefreshLayout
,然后RecyclerView
为孩子。
提交片段后,它会开始与服务器通信,以便检索一些数据并填充一个CustomAdapter,它将为RecyclerView提供服务。
用户可以通过向下滑动或按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?立即强行空适配器?
答案 0 :(得分:15)
我遇到问题,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;
}
}
答案 1 :(得分:0)
您需要在致电setRefreshing(true)
之前致电测量方法
看看:SwipeRefreshLayout no animation on fragment creation。也许它会有所帮助。
答案 2 :(得分:0)
我只是将RecyclerView和RelativeLayout包装在SwipeRefreshLayout中,问题就解决了。
<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>