// 拓展列表添加部
private ArrayList mHeaderViews = new ArrayList>()
外声明添加部布局
[java] view plain copy
public void addHeaderView(View view) {
mHeaderViews.add(view)
}
修改WrapAdapter构造
[java] view plain copy
public WrapAdapter(ArrayList headerViews, ArrayList footViews, RecyclerView.Adapter adapter) {
this.adapter = adapter
this.mHeaderViews = headerViews
this.mFootViews = footViews
}
加部位置判断
[java] view plain copy
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView)
RecyclerView.LayoutManager manager = recyclerView.getLayoutManager()
if (manager instanceof GridLayoutManager) {
final GridLayoutManager gridManager = ((GridLayoutManager) manager)
gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return (isHeader(position) || isFooter(position))
? gridManager.getSpanCount() : 1
}
})
}
}
@Override
public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
super.onViewAttachedToWindow(holder)
ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams()
if (lp != null
&&lp instanceof StaggeredGridLayoutManager.LayoutParams
&&(isHeader(holder.getLayoutPosition()) || isFooter(holder.getLayoutPosition()))) {
StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp
p.setFullSpan(true)
}
}
public boolean isHeader(int position) {
return position >= 0 &&position <mHeaderViews.size()
}
onCreateViewHolder()通同类型返同布局onBindViewHolder()判断布局候做任何 *** 作即需要部布局位置绑定adapter
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)