1.由于recyclerView没有提供像Listview一样的addheadView的方法,就需要我们自己设置head。
布局很简单上下两个头和脚布局。内容写了一些假数据。贴一下adapter中的代码:
public class HeadFootAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
/**
* ITEM的类型
*/
private static final int HEAD_TYPE = 0;
private static final int FOOT_TYPE = 1;
private static final int CONTENT_TYPE = 2;
private Context context;
private List<String> list;
/**
* 头布局的数量
*/
private int HEAD_COUNT = 1;
/**
* 脚布局的数量
*/
private int FOOT_COUNT = 1;
public HeadFootAdapter(Context context, List<String> list) {
this.context = context;
this.list = list;
}
//判断当前item是否是HeadView
public boolean isHeaderView(int position) {
return HEAD_COUNT != 0 && position < HEAD_COUNT;
}
//判断当前item是否是FooterView
public boolean isBottomView(int position) {
return FOOT_COUNT != 0 && position >= (HEAD_COUNT + list.size());
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == HEAD_TYPE) {
return new HeadViewHolder(View.inflate(context, R.layout.item_head, null));
} else if (viewType == FOOT_TYPE) {
return new FootViewHolder(View.inflate(context, R.layout.item_foot, null));
} else {
return new ContentViewHolder(View.inflate(context, R.layout.item_head_foot, null));
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof HeadViewHolder) {//头布局
} else if (holder instanceof FootViewHolder) {// 脚布局
} else {
((ContentViewHolder) holder).txtContent.setText(list.get(position-1));
}
}
@Override
public int getItemCount() {
return HEAD_COUNT + list.size() + FOOT_COUNT;
}
@Override
public int getItemViewType(int position) {
int dataCount = list.size();
if (HEAD_COUNT != 0 && position < HEAD_COUNT) {
return HEAD_TYPE;
} else if (FOOT_COUNT != 0 && position >= (HEAD_COUNT + dataCount)) {
return FOOT_TYPE;
} else {
return CONTENT_TYPE;
}
}
/**
* 内容的VIEWHOLDER
*/
public static class ContentViewHolder extends RecyclerView.ViewHolder {
public TextView txtContent;
public ContentViewHolder(View itemView) {
super(itemView);
txtContent = get(itemView, R.id.txt_content);
}
private static <T extends View> T get(View view, int id) {
SparseArray<View> viewholder = (SparseArray<View>) view.getTag();
if (viewholder == null) {
viewholder = new SparseArray<View>();
view.setTag(viewholder);
}
View chicldView = viewholder.get(id);
if (chicldView == null) {
chicldView = view.findViewById(id);
viewholder.put(id, chicldView);
}
return (T) chicldView;
}
}
/**
* 头布局的VIEWHOLDER
*/
public static class HeadViewHolder extends RecyclerView.ViewHolder {
public HeadViewHolder(View itemView) {
super(itemView);
}
private static <T extends View> T get(View view, int id) {
SparseArray<View> viewholder = (SparseArray<View>) view.getTag();
if (viewholder == null) {
viewholder = new SparseArray<View>();
view.setTag(viewholder);
}
View chicldView = viewholder.get(id);
if (chicldView == null) {
chicldView = view.findViewById(id);
viewholder.put(id, chicldView);
}
return (T) chicldView;
}
}
/**
* 脚布局的VIEWHOLDER
*/
public static class FootViewHolder extends RecyclerView.ViewHolder {
public FootViewHolder(View itemView) {
super(itemView);
}
private static <T extends View> T get(View view, int id) {
SparseArray<View> viewholder = (SparseArray<View>) view.getTag();
if (viewholder == null) {
viewholder = new SparseArray<View>();
view.setTag(viewholder);
}
View chicldView = viewholder.get(id);
if (chicldView == null) {
chicldView = view.findViewById(id);
viewholder.put(id, chicldView);
}
return (T) chicldView;
}
}
}
首先通过getViewType设置item的类型:
@Override
public int getItemViewType(int position) {
int dataCount = list.size();
if (HEAD_COUNT != 0 && position < HEAD_COUNT) {
return HEAD_TYPE;
} else if (FOOT_COUNT != 0 && position >= (HEAD_COUNT + dataCount)) {
return FOOT_TYPE;
} else {
return CONTENT_TYPE;
}
}
在onCreateViewHolder中通过类型判断设置item的布局:
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == HEAD_TYPE) {
return new HeadViewHolder(View.inflate(context, R.layout.item_head, null));
} else if (viewType == FOOT_TYPE) {
return new FootViewHolder(View.inflate(context, R.layout.item_foot, null));
} else {
return new ContentViewHolder(View.inflate(context, R.layout.item_head_foot, null));
}
}
最后在Activity中设置Adapter:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.head_foot_activity);
headFootRecyclerView = (RecyclerView) findViewById(R.id.headFootRecyclerView);
initData();
final GridLayoutManager grid = new GridLayoutManager(this, 4);
headFootRecyclerView.setLayoutManager(grid);
final HeadFootAdapter adapter = new HeadFootAdapter(this, list);
headFootRecyclerView.setAdapter(adapter);
grid.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return (adapter.isHeaderView(position)||adapter.isBottomView(position))?grid.getSpanCount():1;
}
});
}
在设置头布局的时候为头布局设置spansize设置了占据4格的单元格:
grid.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return (adapter.isHeaderView(position)||adapter.isBottomView(position))?grid.getSpanCount():1;
}
});
通过判断当前位置是否是头布局或脚布局,如果是的话就让头布局占据4格。
(adapter.isHeaderView(position)||adapter.isBottomView(position))?grid.getSpanCount():1;
布局比较简单就不贴了,源码链接:git源码