解决问题:
1.ViewPager的懒加载模式为了让切换更加流畅,但是会多预加载的Fragment需要的数据进行网络请求,当多个Fragment都存在大量数据的时候,容易因为队列中请求较多导致卡顿,也浪费流量。
2.频繁切换ViewPager造成过多的网络请求。
3.在UI还没有渲染完成的时候请求数据,导致空指针异常。
解决方案:抽象基类的创建
public abstract class BasePageFragment extends Fragment { protected boolean isViewInitiated; protected boolean isVisibleToUser; protected boolean isDataInitiated; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); isViewInitiated = true; prepareFetchData(); } @Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); this.isVisibleToUser = isVisibleToUser; prepareFetchData(); } public abstract void fetchData(); public boolean prepareFetchData() { return prepareFetchData(false); } public boolean prepareFetchData(boolean forceUpdate) { if (isVisibleToUser && isViewInitiated && (!isDataInitiated || forceUpdate)) { fetchData(); isDataInitiated = true; return true; } return false; } }
看代码这里只有一个setUserVisibleHint需要说下,此方法用于判断Fragment上面的UI是否已经可视了,所以在prepareFetchData方法里我们做如下判断:就是当前UI可见,并且fragment已经初始化完毕,如果网络数据未加载,那么请求数据。由于我们选用的是
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerVertical="true" android:scrollbars="none"> </android.support.v7.widget.RecyclerView>
此插件有下拉刷新的回调方法,所以当你主观需要刷新的时候可以直接下拉刷新数据,就不要每一次切换页面都发起请求了。
接下里就是ViewPager中的Fragment只需要继承改抽象类,实现
fetchData() 方法即可:
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB) public class Item1Fragment extends BasePageFragment { private XRecyclerView xRecyclerView; private ItemAdapter adapter = null; private List<TaskDetail> lists; public Item1Fragment() { } @Override public void fetchData() { ApiServiceUnified.getOurInstance().getTaskList("23442704051802179';", "0", null, null, new retrofit2.Callback<ResponseUnified60001>() { @Override public void onResponse(Call<ResponseUnified60001> call, Response<ResponseUnified60001> response) { } @Override public void onFailure(Call<ResponseUnified60001> call, Throwable throwable) { } }); } @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_item1,null); xRecyclerView = view.findViewById(R.id.recyclerview); xRecyclerView.setLayoutManager(new LinearLayoutManager(getContext())); initData(); adapter = new ItemAdapter(getContext(),lists); xRecyclerView.setAdapter(adapter); adapter.setOnClickItemListener(new ItemAdapter.OnClickItemListener() { @Override public void onClick(TaskDetail taskDetail) { Intent intent = new Intent(getActivity(), TaskDetailsActivity.class); switch (taskDetail.getType()){ case "1"://待派件 break; case "2"://待回收 break; case "3"://代送回执包 break; } startActivity(intent); } }); return view; } }
可以实现的效果,就是只有当Fragment渲染完成才发起请求,切在预加载的页面中,请求过的页面不会再第二次发起请求,如果需要就下拉刷新。
文章来源: https://blog.csdn.net/qq_20369621/article/details/91457606