Android RecyclerView添加HeaderView和FooterView学习

一、装饰设计模式

装饰模式指的是在不改变原类文件和不使用继承的情况下,动态地扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象。

二、扩展Adapter

将原RecyclerView的Adapter传递进来,进行HeaderView和FootView扩展,HeaderView和FootView的逻辑在WrapRecyclerAdapter中进行处理,原有Item交给原Adapter进行处理,这就是装饰设计模式。


Android RecyclerView添加HeaderView和FooterView学习_第1张图片
效果图
WrapRecyclerAdapter
public class WrapRecyclerAdapter extends RecyclerView.Adapter {

    private List headerViews;
    private List footerViews;
    private RecyclerView.Adapter adapter;

    public WrapRecyclerAdapter(RecyclerView.Adapter adapter) {
        headerViews = new ArrayList<>();
        footerViews = new ArrayList<>();
        this.adapter = adapter;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int position) {
        if (position < headerViews.size()) {
            return onCreateHeaderFooterViewHolder(headerViews.get(0));
        }
        if (position < headerViews.size() + adapter.getItemCount()) {
            return adapter.onCreateViewHolder(parent, position - headerViews.size());
        }
        return onCreateHeaderFooterViewHolder(footerViews.get(position - adapter.getItemCount() - headerViews.size()));
    }

    private RecyclerView.ViewHolder onCreateHeaderFooterViewHolder(View view) {
        return new RecyclerView.ViewHolder(view) {
        };
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (position >= headerViews.size() && position < (adapter.getItemCount() + headerViews.size())) {
            adapter.onBindViewHolder(holder, position - headerViews.size());
        }
    }

    @Override
    public int getItemCount() {
        return adapter.getItemCount() + headerViews.size() + footerViews.size();
    }

    public void addHeaderView(View view) {
        if (!headerViews.contains(view)) {
            headerViews.add(view);
            notifyDataSetChanged();
        }
    }

    public void addFooterView(View view) {
        if (!footerViews.contains(view)) {
            footerViews.add(view);
            notifyDataSetChanged();
        }
    }
}
WrapRecyclerView
public class WrapRecyclerView extends RecyclerView {

    private WrapRecyclerAdapter wrapRecyclerAdapter;

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

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

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

    @Override
    public void setAdapter(Adapter adapter) {
        wrapRecyclerAdapter = new WrapRecyclerAdapter(adapter);
        super.setAdapter(wrapRecyclerAdapter);
    }

    public void addHeaderView(View view) {
        if (wrapRecyclerAdapter != null) {
            wrapRecyclerAdapter.addHeaderView(view);
        }
    }

    public void addFooterView(View view) {
        if (wrapRecyclerAdapter != null) {
            wrapRecyclerAdapter.addFooterView(view);
        }
    }
}

三、用法

    wrapRecyclerView.setAdapter(new BaseAdapter(strings));
    View header = LayoutInflater.from(MainActivity.this).inflate(R.layout.header, wrapRecyclerView, false);
    View footer = LayoutInflater.from(MainActivity.this).inflate(R.layout.footer, wrapRecyclerView, false);
    wrapRecyclerView.addHeaderView(header);
    wrapRecyclerView.addFooterView(footer);

四、致谢

谢谢他的视频教程,谢谢他的文章
红橙Darren

你可能感兴趣的:(Android RecyclerView添加HeaderView和FooterView学习)