Android偷梁换柱打造可添加头部和底部的RecyclerView

最近在看一本名为《Android源码设计模式解析与实战》的书,表示看完如果不及时敲一偏还真容易忘记,刚好看到装饰者模式,这里就敲一个有关这个思想的代码。

装饰模式定义:动态的给一个对象添加额外的职责,使得这个对象的功能越来越丰富。

我们先把最原始的使用RecyclerView的步骤写一篇然后再升级为最终版。

布局很简单,就不在多说,个人习惯喜欢从Adapter入手,我们创建一个类,实现最简单的Adapter,下面是BaseRaleAdapter:

public class BaseRaleAdapter extends RecyclerView.Adapter{
    private List mList;
    private Context mContext;
    public BaseRaleAdapter(Context context,List list){
        this.mList=list;
        mContext=context;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.expand_item_layout,parent,false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.mTest.setText(mList.get(position));
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        private final TextView mTest;

        public ViewHolder(View itemView) {
            super(itemView);
            mTest = itemView.findViewById(R.id.test);
        }
    }
}

然后再Activity里面使用


public class Main20Activity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private BaseRaleAdapter mBaseRaleAdapter;
    private List mList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main20);
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        LinearLayoutManager linearLayout=new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayout);
        mBaseRaleAdapter = new BaseRaleAdapter(Main20Activity.this,mList);
        mRecyclerView.setAdapter(mBaseRaleAdapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        for(int x=0;x<20;x++){
            mList.add(x+"");
        }
        mBaseRaleAdapter.notifyDataSetChanged();
    }
}

这就是最基础的使用了,那么我们将BaseRaleAdapter进行扩展,就运用到装饰者模式了,原本你没有添加头部和底部的功能,哎~?我将你包裹一下,就能拓展出这两个功能了,来,继续走下去。

创建一个ExpandAdapter,顾名思义他就是拓展adapter,他也是和BaseRaleAdapter一样,也是集成于RecyclerView.Adapter<>,但是尖括号里面的内容就不是BaseRaleAdapter.ViewHolder了,而是RecyclerView.ViewHolder,因为等下在onCreateViewHolder中我们不仅要返回正常item的ViewHolder也要返回头部与底部的ViewHolder,所以这样做可以兼容。

public class ExpandAdapter extends RecyclerView.Adapter{
    private RecyclerView.Adapter mRealAdpter;
    private List mHanders=new ArrayList<>();
    private List mFoots=new ArrayList<>();
    public ExpandAdapter(RecyclerView.Adapter realAdpter){
        this.mRealAdpter=realAdpter;
    }

    /**
     * 由于下面需要获取ViewHolder,那肯定是需要当前位置,但是onCreateViewHolder唯一与position有关的就是viewType
     * 所以我们需要复写getItemViewType,让其返回position而不是type
     * @param parent
     * @param position
     * @return
     */
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int position) {

        //如果是头部就返回头部的ViewHolder
        int handersNumber = mHanders.size();
        if(position

然后更新下Activity的使用代码,将原来的BaseRaleAdapter包裹在里面,

public class Main20Activity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private BaseRaleAdapter mBaseRaleAdapter;
    private List mList=new ArrayList<>();
    private ExpandAdapter mExpandAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main20);
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        LinearLayoutManager linearLayout=new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayout);
        mBaseRaleAdapter = new BaseRaleAdapter(Main20Activity.this,mList);
        mExpandAdapter = new ExpandAdapter(mBaseRaleAdapter);
        mRecyclerView.setAdapter(mExpandAdapter);
        View view = LayoutInflater.from(Main20Activity.this).inflate(R.layout.layout_hander, mRecyclerView, false);
        mExpandAdapter.addHanderView(view);
    }

    @Override
    protected void onResume() {
        super.onResume();
        for(int x=0;x<20;x++){
            mList.add(x+"");
        }
        mBaseRaleAdapter.notifyDataSetChanged();
    }
}

头部的布局


    

Android偷梁换柱打造可添加头部和底部的RecyclerView_第1张图片
微信图片_20180719174812.jpg

这样简单的封装就出来了,我们就完成了装饰。,,

你会发现一个问题,在activity中,使用这个封装好的Adapter有点步骤太多,别人使用的时候要注意的也多,那怎么简化呢?我们可以参考ListView,so?我们能不能将Adapter的偷梁换柱放到RecyclerView中呢?好!说干就干,创建一个ExpandRecyclerView

public class ExpandRecyclerView extends RecyclerView{
    private ExpandAdapter mExpandAdapter;

    public ExpandRecyclerView(Context context) {
        super(context);
    }

    public ExpandRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ExpandRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    /**
     * 偷梁换柱
     * @param adapter
     */
    @Override
    public void setAdapter(Adapter adapter) {
        mExpandAdapter=new ExpandAdapter(adapter);
        super.setAdapter(mExpandAdapter);
    }

    /**
     * 添加头部
     * @param view
     */
    public void addHanderView(View view){
        if(mExpandAdapter!=null){
            mExpandAdapter.addHanderView(view);
        }
    }

    /**
     * 添加底部
     * @param view
     */
    public void addFootView(View view){
        if(mExpandAdapter!=null){
            mExpandAdapter.addFootView(view);
        }
    }

    /**
     * 移除头部
     * @param view
     */
    public void removeHanderView(View view){
        if(mExpandAdapter!=null){
            mExpandAdapter.removeHanderView(view);
        }
    }

    /**
     * 移除底部
     * @param view
     */
    public void removeFootView(View view){
        if(mExpandAdapter!=null){
            mExpandAdapter.removeFootView(view);
        }
    }
}

这样在Activity的使用中,就会变得简单

public class Main20Activity extends AppCompatActivity {

    private ExpandRecyclerView mRecyclerView;
    private BaseRaleAdapter mBaseRaleAdapter;
    private List mList=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main20);
        mRecyclerView = (ExpandRecyclerView) findViewById(R.id.recycler_view);
        LinearLayoutManager linearLayout=new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayout);
        mBaseRaleAdapter = new BaseRaleAdapter(Main20Activity.this,mList);
        mRecyclerView.setAdapter(mBaseRaleAdapter);
        View view = LayoutInflater.from(Main20Activity.this).inflate(R.layout.layout_hander, mRecyclerView, false);
        mRecyclerView.addHanderView(view);
    }

    @Override
    protected void onResume() {
        super.onResume();
        for(int x=0;x<20;x++){
            mList.add(x+"");
        }
        mBaseRaleAdapter.notifyDataSetChanged();
    }
}

当然,Activity的布局中就要应用我们自定义的ExpandRecyclerView 而不是RecyclerView了。

以前我就是用上面这些代码使用的,但是有一次被同事指出一个bug,就是删除一行条目时,没有及时刷新,这样,我们稍微修改下代码来验证下

public class BaseRaleAdapter extends RecyclerView.Adapter{
    private List mList;
    private Context mContext;
    public BaseRaleAdapter(Context context,List list){
        this.mList=list;
        mContext=context;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.expand_item_layout,parent,false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.mTest.setText(mList.get(position));
        //移除测试
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mList.remove(position);
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        private final TextView mTest;

        public ViewHolder(View itemView) {
            super(itemView);
            mTest = itemView.findViewById(R.id.test);
        }
    }
}

BaseRaleAdapter中加上了点击删除功能,发现根本没用,因为BaseRaleAdapter调用了notifyDataSetChanged但是我们ExpandAdapter并没有相应,所以界面也是没有变化的,所以在ExpandAdapter中要监听BaseRaleAdapter调用了notifyDataSetChanged,然后也使用ExpandAdapter调用一次notifyDataSetChanged就行,好!我们就修改一下ExpandAdapter

public class ExpandAdapter extends RecyclerView.Adapter{
    private RecyclerView.Adapter mRealAdpter;
    private List mHanders=new ArrayList<>();
    private List mFoots=new ArrayList<>();
    public ExpandAdapter(RecyclerView.Adapter realAdpter){
        this.mRealAdpter=realAdpter;
        //监听BaseRaleAdapter调用了notifyDataSetChanged,然后也使用ExpandAdapter调用一次notifyDataSetChanged
        realAdpter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
            @Override
            public void onChanged() {
                super.onChanged();
                notifyDataSetChanged();
            }
        });
    }

    /**
     * 由于下面需要获取ViewHolder,那肯定是需要当前位置,但是onCreateViewHolder唯一与position有关的就是viewType
     * 所以我们需要复写getItemViewType,让其返回position而不是type
     * @param parent
     * @param position
     * @return
     */
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int position) {

        //如果是头部就返回头部的ViewHolder
        int handersNumber = mHanders.size();
        if(position

你可能感兴趣的:(Android偷梁换柱打造可添加头部和底部的RecyclerView)