码迷,mamicode.com
首页 > 其他好文 > 详细

RecyclerView添加Header和Footer

时间:2015-08-26 18:02:43      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:recycler   header   footer   android   

使用过RecyclerView的同学就知道它并没有添加header和footer的方法,而ListView和GirdView都有,但是开发过程中难免有需求需要添加一个自定义的header或者footer,或者不同布局的Item。

好了,我们知道如果在ListView中要添加不同的布局的item,都是通过重写getItemViewType()getViewTypeCount()这两个方法来控制的,OK,对于RecyclerView,还是用这个方法来做,分别对不同的Item用不同的flag标记,然后在创建和绑定数据时候分别对不同的flag对应不同的处理,直接上代码(这里我添加了header和footer):

先来看看效果:
技术分享


代码:

RecyclerView.Adapter

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);
            }
        }
    }
}

RecyclerView.ViewHolder

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

RecyclerView添加Header和Footer

标签:recycler   header   footer   android   

原文地址:http://blog.csdn.net/u010687392/article/details/48005545

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!