RecyclerView添加头部

在开发中,很多情况下不只是单纯的去使用 RecyclerView ,有时需要添加头部,这样配合使用可以实现复杂的布局。也可以使用 RecyclerView 的下拉刷新和上拉加载功能,比如下图上面有附近的人,下面是瀑布流:

 

GitHub 下载地址:https://github.com/wuqingsen/RecyclerViewHeader

CSDN 下载地址:https://download.csdn.net/download/wuqingsen1/11079298

 

下面就看一下如何实现的;

首先设置 adapter ,和平常一样:

        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        mainAdapter = new MainAdapter(MainActivity.this, mDatas);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(mainAdapter);

 

然后在 adapter 中添加 onViewAttachedToWindow 方法:

    @Override
    public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
        if (lp != null && lp instanceof StaggeredGridLayoutManager.LayoutParams) {
            StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp;
            p.setFullSpan(holder.getLayoutPosition() == 0);
        }
    }

然后根据 viewType 判断即可,下面是 adapter 中的全部代码:

public class MainAdapter extends RecyclerView.Adapter {

    private LayoutInflater inflater;
    private List mDatas;
    private Context mContext;
    private final int type_zero = 0;
    private final int type_one = 1;

    public MainAdapter(Context mContext, List mDatas) {
        this.mDatas = mDatas;
        this.mContext = mContext;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
        if (lp != null && lp instanceof StaggeredGridLayoutManager.LayoutParams) {
            StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp;
            p.setFullSpan(holder.getLayoutPosition() == 0);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_main_header, parent, false);
            return new ZeroViewHolder(view);
        }
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_main, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        switch (holder.getItemViewType()) {
            case type_zero:
                break;
            default:
                MyViewHolder myViewHolder = (MyViewHolder) holder;
                ViewGroup.LayoutParams params1 = myViewHolder.iv_icon.getLayoutParams();
                if (position % 2 == 0) {
                    params1.height = 600;
                    params1.width = Utils.getScreenWidth(mContext) / 2;
                    myViewHolder.iv_icon.setLayoutParams(params1);
                    myViewHolder.iv_icon.setBackgroundResource(R.drawable.icon_bg1);
                }else {
                    params1.height = 700;
                    params1.width = Utils.getScreenWidth(mContext) / 2;
                    myViewHolder.iv_icon.setLayoutParams(params1);
                    myViewHolder.iv_icon.setBackgroundResource(R.drawable.icon_bg2);
                }
                break;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return type_zero;
        } else {
            return type_one;
        }
    }

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

    class ZeroViewHolder extends RecyclerView.ViewHolder {

        public ZeroViewHolder(View itemView) {
            super(itemView);
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_icon;

        public MyViewHolder(View itemView) {
            super(itemView);
            iv_icon = itemView.findViewById(R.id.iv_icon);
        }
    }
}

 

在这里要注意,使用瀑布流会出现滑动闪烁、空白和位置移动这样的问题;如果你要使用瀑布流,请看下面的链接关于瀑布流的优化:https://blog.csdn.net/wuqingsen1/article/details/88942159

 

设置完毕,来看下完成后简单的效果,虽然丑,但功能实现了:

 

你可能感兴趣的:(RecyclerView添加头部)