最近项目中遇到一个问题,app首页的Activity中由若干个Fragment页面组成,其中一个fragment页面是由一个GridView和ListView组成的列表,如果列表中数据量过大的时候,在请求数据的时候点击页面上的其他按钮会无响应,直到该fragment上的数据刷新完页面才会有反应,经过测试是由于fragment请求完数据刷新UI界面与Activity中的底部按钮都是共用的Activity UI主线程,所以只能同时响应一个,解决方案就是对列表数据进行分页处理,并用RecyclerView实现列表。
分页处理就是请求完接口得到数据,作为总数据,刷新、加载时通过page再获取每页需要获取的数据,刷新页面,进行一些相关的操作即可,在此就不做赘述了。
使用RecyclerView实现的好处是可以利用其局部刷新的特点,减少因为notifyDataSetChanged()带来的不必要的开销,此处用一个RecyclerView实现了GridView和ListView,用viewType属性实现两种布局的效果。
设置GridLayoutManager的列数为两列,然后通过itemViewType值判断item占用的列数。
final GridLayoutManager manager = new GridLayoutManager(getActivity(), 2);
//设置占用的列数
manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
int type = rvXiangmu.getAdapter().getItemViewType(position);
Log.i("pp", position + ":" + type);
//若是TYPE_ONE,占用两列,否则占用一列,分母为grid设置的列数
if (type == TaskTeamEntity2.TYPE_ONE) {
return manager.getSpanCount();
} else {
return 1;
}
}
});
rvXiangmu.setLayoutManager(manager);
给item绘制间距
rvXiangmu.addItemDecoration(new RecyclerView.ItemDecoration() {
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
//给布局里的子view添加边距
GridLayoutManager.LayoutParams layoutParams = (GridLayoutManager.LayoutParams) view.getLayoutParams();
int position = parent.getChildPosition(view);
//spanSize表示item占用几列
int spanSize = layoutParams.getSpanSize();
//如果是GridView的话绘制
//表示item占用的列数不是manager设置的总列数,表示一行多列的情况
if (spanSize!=manager.getSpanCount()) {
int spanIndex = layoutParams.getSpanIndex();
outRect.bottom = ScreenUtil.dip2px(getActivity(), 4);
if (spanIndex == 0) {
outRect.left = ScreenUtil.dip2px(getActivity(), 8);
outRect.right = ScreenUtil.dip2px(getActivity(), 2);
} else {
outRect.right = ScreenUtil.dip2px(getActivity(), 8);
outRect.left = ScreenUtil.dip2px(getActivity(), 2);
}
if (position == 0 || position == 1) {
outRect.top = ScreenUtil.dip2px(getActivity(), 4);
}
}
}
});
adapter中部分代码
//创建新View,被LayoutManager所调用
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
//根据不同的viewType,创建并返回相应的ViewHolder
switch (viewType) {
case TaskTeamEntity2.TYPE_TWO:
return new TypeTwoHolder(mLayoutInflater.inflate(R.layout.item_item1, viewGroup, false));
case TaskTeamEntity2.TYPE_ONE:
return new TypeOneHolder(mLayoutInflater.inflate(R.layout.item_item2, viewGroup, false));
}
return null;
}
public class TypeOneHolder extends TypeAbstarctViewHolder {
private TextView itemNameTv;
private TextView itemTimeTv;
public TypeOneHolder(View view) {
super(view);
itemNameTv = (TextView) view.findViewById(R.id.tv_item_name);
itemTimeTv = (TextView) view.findViewById(R.id.tv_item_time);
}
@Override
public void bindHolder(TaskTeamEntity2 item, int position) {
itemNameTv.setText(item.t_teamName);
itemTimeTv.setText(item.t_enddate);
}
}
//自定义的ViewHolder,持有每个Item的的所有界面元素
public class TypeTwoHolder extends TypeAbstarctViewHolder {
private TextView itemNameTv;
private TextView itemTimeTv;
public TypeTwoHolder(View view) {
super(view);
itemNameTv = (TextView) view.findViewById(R.id.tv_item_name);
itemTimeTv = (TextView) view.findViewById(R.id.tv_item_time);
}
@Override
public void bindHolder(TaskTeamEntity2 item, int position) {
itemNameTv.setText(item.t_teamName);
itemTimeTv.setText(item.t_enddate);
}
}
public abstract class TypeAbstarctViewHolder extends RecyclerView.ViewHolder {
public TypeAbstarctViewHolder(View itemView) {
super(itemView);
}
public abstract void bindHolder(TaskTeamEntity2 item, int position);
}