BaseRecyclerAdapter处理了header和foo

作者: JC_Hou | 来源:发表于2016-11-24 15:10 被阅读391次

封装了的RecyclerViewAdapter,内部处理了添加header和footer方法,点击事件。
具体可以看我这篇为RecyclerView添加header和footer

public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    public static final int TYPE_HEADER = 0;  //说明是带有Header的
    public static final int TYPE_FOOTER = 1;  //说明是带有Footer的
    public static final int TYPE_NORMAL = 2;  //说明是不带有header和footer的

    private ArrayList<T> mDatas=new ArrayList<>();

    private View mHeaderView, mFooterView;

    public interface onItemClickListener<T>{
        void onItemClick(int position,T data);
        void onItemLongClick(int position,T data);
    }

    private onItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(onItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public View getHeader() {
        return mHeaderView;
    }

    public void setHeader(View mHeader) {
        this.mHeaderView = mHeader;
        notifyItemInserted(0);
    }

    public View getFooter() {
        return mFooterView;
    }

    public void setFooter(View mFooter) {
        this.mFooterView = mFooter;
        if (mHeaderView ==null){
            notifyItemInserted(mDatas.size());
        }else {
            notifyItemInserted(mDatas.size()+1);
        }
    }

    public void addDatas(ArrayList<T> datas) {
        mDatas.addAll(datas);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (mHeaderView==null){
            if (mFooterView==null){
                return TYPE_NORMAL;
            }else{
                if (position==mDatas.size()){
                    return TYPE_FOOTER;
                }else{
                    return TYPE_NORMAL;
                }
            }
        }else {
            if (position==0){
                return TYPE_HEADER;
            }else{
                if (mFooterView==null){
                    return TYPE_NORMAL;
                }else{
                    if (position!=mDatas.size()+1){
                        return TYPE_NORMAL;
                    }else{
                        return TYPE_FOOTER;
                    }
                }
            }
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mHeaderView!=null&&viewType==TYPE_HEADER){
            return new Holder(mHeaderView);
        }else if (mFooterView!=null&&viewType==TYPE_FOOTER){
            return new Holder(mFooterView);
        }
        return onCreate(parent,viewType);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position)==TYPE_HEADER||getItemViewType(position)==TYPE_FOOTER) return;
        final int realPosition = getRealPosition(holder);
        final T data = mDatas.get(realPosition);
        onBind(holder,realPosition,data);

        if (mOnItemClickListener!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickListener.onItemClick(realPosition,data);
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    mOnItemClickListener.onItemLongClick(realPosition,data);
                    return false;
                }
            });
        }
    }

    private int getRealPosition(RecyclerView.ViewHolder viewHolder){
        int layoutPosition = viewHolder.getLayoutPosition();
        return mHeaderView==null?layoutPosition:layoutPosition-1;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager){
            final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
            gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    return getItemViewType(position)==TYPE_HEADER||getItemViewType(position)==TYPE_FOOTER?gridLayoutManager.getSpanCount():1;
                }
            });
        }
    }


    @Override
    public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams();
        if (layoutParams!=null&&layoutParams instanceof StaggeredGridLayoutManager.LayoutParams){
            StaggeredGridLayoutManager.LayoutParams params = (StaggeredGridLayoutManager.LayoutParams) layoutParams;
            params.setFullSpan(getItemViewType(holder.getLayoutPosition())==TYPE_HEADER||getItemViewType(holder.getLayoutPosition())==TYPE_FOOTER);
        }
    }

    @Override
    public int getItemCount() {
        if (mHeaderView==null){
            if (mFooterView==null){
                return mDatas.size();
            }else {
                return mDatas.size()+1;
            }
        }else{
            if (mFooterView==null){
                return mDatas.size()+1;
            }else {
                return mDatas.size()+2;
            }
        }
    }

    public abstract RecyclerView.ViewHolder onCreate(ViewGroup parent, final int viewType);
    public abstract void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, T data);

    class Holder extends RecyclerView.ViewHolder{

        public Holder(View itemView) {
            super(itemView);
        }
    }
}```

####简化使用adapter

public class MyAdapter extends BaseRecyclerAdapter<DataBean> {

@Override
public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
    return new MyHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_item,parent,false));
}

@Override
public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, DataBean data) {
    if (viewHolder instanceof MyHolder){
        MyHolder holder = (MyHolder) viewHolder;
        holder.imageView.setImageResource(data.getImage());
        holder.textView.setText(data.getText());
    }
    
}

class MyHolder extends MyAdapter.Holder {
    ImageView imageView;
    TextView textView;

    public MyHolder(View itemView) {
        super(itemView);
        imageView= (ImageView) itemView.findViewById(R.id.pubu_img);
        textView= (TextView) itemView.findViewById(R.id.pubu_txt);
    }
}

}```

在Activity中的使用

        mRecycleView= (RecyclerView) findViewById(R.id.recycle_view);
        MyAdapter myAdapter=new MyAdapter();
        myAdapter.addDatas(mList);
        myAdapter.setOnItemClickListener(new BaseRecyclerAdapter.onItemClickListener() {
            @Override
            public void onItemClick(int position, Object data) {
                Toast.makeText(MainActivity.this,(position+1)+"click", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onItemLongClick(int position, Object data) {
                Toast.makeText(MainActivity.this,(position+1)+"long click", Toast.LENGTH_SHORT).show();
            }
        });
        mRecycleView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        myAdapter.setHeader(LayoutInflater.from(this).inflate(R.layout.header,mRecycleView,false));
        View footer = LayoutInflater.from(this).inflate(R.layout.header, mRecycleView, false);
        myAdapter.setFooter(footer);
        ((TextView)footer.findViewById(R.id.tv)).setText("footer");
        mRecycleView.setAdapter(myAdapter);
        SpacesItemDecoration decoration = new SpacesItemDecoration(16);
        mRecycleView.addItemDecoration(decoration);```

相关文章

网友评论

  • 有酒和远方:为什么会有一个红色的边框啊?
    JC_Hou:@画江湖之不良人 是不是recyclerview的原因,但按理说,应该是兼容的,毕竟是兼容包
    有酒和远方:发现在高版本手机上面 没有,低版本会有一个边框的效果
  • 有酒和远方:没得说 正好要用到
    JC_Hou: @画江湖之不良人 一起进步

本文标题:BaseRecyclerAdapter处理了header和foo

本文链接:https://www.haomeiwen.com/subject/obmbpttx.html