标签:recycler header footer android
使用过RecyclerView的同学就知道它并没有添加header和footer的方法,而ListView和GirdView都有,但是开发过程中难免有需求需要添加一个自定义的header或者footer,或者不同布局的Item。
好了,我们知道如果在ListView中要添加不同的布局的item,都是通过重写getItemViewType()
和getViewTypeCount()
这两个方法来控制的,OK,对于RecyclerView,还是用这个方法来做,分别对不同的Item用不同的flag标记,然后在创建和绑定数据时候分别对不同的flag对应不同的处理,直接上代码(这里我添加了header和footer):
先来看看效果:
代码:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewHolder> {
private List<String> datas;
private static final int IS_HEADER = 2;
private static final int IS_FOOTER = 3;
private static final int IS_NORMAL = 1;
public RecyclerViewAdapter(List<String> datas) {
this.datas = datas;
}
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
RecyclerViewHolder holder;
//对不同的flag创建不同的Holder
if (viewType == IS_HEADER) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_header, viewGroup, false);
holder = new RecyclerViewHolder(view,IS_HEADER);
return holder;
} else if (viewType == IS_FOOTER) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_footer, viewGroup, false);
holder = new RecyclerViewHolder(view,IS_FOOTER);
return holder;
}else if(viewType==IS_NORMAL){
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_item, viewGroup, false);
holder = new RecyclerViewHolder(view,IS_NORMAL);
return holder;
}
return null;
}
@Override
public void onBindViewHolder(final RecyclerViewHolder recyclerViewHolder, int position) {
//对不同的Item相应不同的操作
if(position!=0&&position!=datas.size()+1&&recyclerViewHolder.viewType==IS_NORMAL){
recyclerViewHolder.mTextView.setText(datas.get(position - 1));
}
if(position==0&&recyclerViewHolder.viewType==IS_HEADER){
//header
recyclerViewHolder.mButton.setOnClickListener(new View.OnClickListener() {
int i=0;
@Override
public void onClick(View v) {
recyclerViewHolder.mButton.setText(++i+"");
}
});
}
if(position==datas.size()+1&&recyclerViewHolder.viewType==IS_FOOTER){
//footer
}
}
@Override
public int getItemCount() {
return datas.size() + 2;
}
@Override
public int getItemViewType(int position) {
if (position == 0) {
return IS_HEADER;
} else if(position==datas.size()+1){
return IS_FOOTER;
}else {
return IS_NORMAL;
}
}
class RecyclerViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public Button mButton;
public int viewType;
public RecyclerViewHolder(View itemView,int viewType) {
super(itemView);
this.viewType = viewType;
if(viewType==IS_HEADER){
mButton = (Button) itemView.findViewById(R.id.button);
}
if(viewType==IS_FOOTER){
//do some sthing
}
if(viewType==IS_NORMAL){
mTextView = (TextView) itemView.findViewById(R.id.tv_content);
}
}
}
}
class RecyclerViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public Button mButton;
public int viewType;
public RecyclerViewHolder(View itemView,int viewType) {
super(itemView);
this.viewType = viewType;
if(viewType==IS_HEADER){
mButton = (Button) itemView.findViewById(R.id.button);
}
if(viewType==IS_FOOTER){
//do some sthing
}
if(viewType==IS_NORMAL){
mTextView = (TextView) itemView.findViewById(R.id.tv_content);
}
}
}
这里我把ViewHolder单独拿出来看就是为了看不同之处。
版权声明:本文为博主原创文章,未经博主允许不得转载。转载注明出处:http://blog.csdn.net/u010687392
标签:recycler header footer android
原文地址:http://blog.csdn.net/u010687392/article/details/48005545