android xrecyclerview 怎么加头布局
1个回答
展开全部
[java] view plain copy
// 拓展的一个列表,添加头部
private ArrayList<View> mHeaderViews = new ArrayList<>();
对外声明一个方法,来添加头部布局
[java] view plain copy
public void addHeaderView(View view) {
mHeaderViews.add(view);
}
修改一下WrapAdapter的构造方法
[java] view plain copy
public WrapAdapter(ArrayList<View> headerViews, ArrayList<View> 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中。
// 拓展的一个列表,添加头部
private ArrayList<View> mHeaderViews = new ArrayList<>();
对外声明一个方法,来添加头部布局
[java] view plain copy
public void addHeaderView(View view) {
mHeaderViews.add(view);
}
修改一下WrapAdapter的构造方法
[java] view plain copy
public WrapAdapter(ArrayList<View> headerViews, ArrayList<View> 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中。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询