1. 程式人生 > >Android RecyclerView新增頭部與尾部

Android RecyclerView新增頭部與尾部

RecyclerView一個藝術般的控制元件,完全取代了之前的ListView與GridView,並且有著比ListView與GridView更加強大的功能,可是RecyclerView缺有個非誠明顯的瑕疵,竟然沒有封裝addHeaderView和addFooterView方法,沒辦法只好自己想辦法實現了:

核心程式碼:

private class RecyclerAdapter extends RecyclerView.Adapter{

        private final static int HEAD_COUNT = 1;
        private final static
int FOOT_COUNT = 1; private final static int TYPE_HEAD = 0; private final static int TYPE_CONTENT = 1; private final static int TYPE_FOOTER = 2; public int getContentSize(){ return list.size(); } public boolean isHead(int position){ return
HEAD_COUNT != 0 && position == 0; } public boolean isFoot(int position){ return FOOT_COUNT != 0 && position == getContentSize() + HEAD_COUNT; } @Override public int getItemViewType(int position) { int contentSize = getContentSize(); if
(HEAD_COUNT != 0 && position == 0){ // 頭部 return TYPE_HEAD; }else if(FOOT_COUNT != 0 && position == HEAD_COUNT + contentSize){ // 尾部 return TYPE_FOOTER; }else{ return TYPE_CONTENT; } } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if (viewType == TYPE_HEAD){ View itemView = LayoutInflater.from(GridActivity.this).inflate(R.layout.head_for_recyclerview,parent,false); return new RecyclerAdapter.HeadHolder(itemView); }else if(viewType == TYPE_CONTENT){ View itemView = LayoutInflater.from(GridActivity.this).inflate(R.layout.item_for_recycler_view,parent,false); return new RecyclerAdapter.ContentHolder(itemView); }else{ View itemView = LayoutInflater.from(GridActivity.this).inflate(R.layout.foot_for_recyclerview,parent,false); return new RecyclerAdapter.FootHolder(itemView); } } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { if (holder instanceof RecyclerAdapter.HeadHolder){ // 頭部 }else if(holder instanceof RecyclerAdapter.ContentHolder){ // 內容 RecyclerAdapter.ContentHolder myHolder = (RecyclerAdapter.ContentHolder) holder; myHolder.itemText.setText(list.get(position - 1)); }else{ // 尾部 } } @Override public int getItemCount() { return list.size() + HEAD_COUNT + FOOT_COUNT; } // 頭部 private class HeadHolder extends RecyclerView.ViewHolder{ public HeadHolder(View itemView) { super(itemView); } } // 內容 private class ContentHolder extends RecyclerView.ViewHolder{ TextView itemText; public ContentHolder(View itemView) { super(itemView); itemText = (TextView)itemView.findViewById(R.id.item_text); } } // 尾部 private class FootHolder extends RecyclerView.ViewHolder{ public FootHolder(View itemView) { super(itemView); } } }

此時只需要將此介面卡設為RecyclerView的列表模式的介面卡就OK了.

image

表格模式需要增加這麼一步:
final GridLayoutManager manager = new GridLayoutManager(this,3);
recyclerView.setLayoutManager(manager);
adapter = new RecyclerAdapter();
recyclerView.setAdapter(adapter);
manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
    @Override
    public int getSpanSize(int position) {
        return adapter.isFoot(position) || adapter.isHead(position) ? manager.getSpanCount() : 1;
    }
});

image