RecyclerView添加header Footer

在我们Android开发过程中RecyclerView的使用中有的还需要给它添加header/footer来丰富我们的项目界面显示,

本篇文章就是介绍如何简单的给RecyclerView添加header 和 footer,来满足项目中的需求,

之前有浏览过大佬的添加方式,无奈找不到了,在此有时间做个整理笔记,方便之后用的时候直接用.

RecyclerView的简单使用上篇(RecyclerView的简单使用总结)有介绍过,感兴趣的可以查看;

闲话就这么多,直接上代码.


先上效果图(顶部header和底部footer):

RecyclerView添加header Footer_第1张图片

实现原理:

大家都知道在RecyclerView的adapter中onCreateViewHolder方法中有一个viewType参数;

而实际上我们可以通过viewType和getItemViewType()配合就可以自己定义类型了,定义Header Content Footer.(此处举一反三也可以定义多类型的item)

首先我们就需要创建三个ViewHolder

    //中间内容显示
    class ContentViewHolder extends RecyclerView.ViewHolder {
        TextView textView;

        public ContentViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.text);
        }
    }

    //头部header
    class HeaderViewHolder extends RecyclerView.ViewHolder {
        TextView header;

        public HeaderViewHolder(@NonNull View itemView) {
            super(itemView);
            header = itemView.findViewById(R.id.header);
        }
    }

    //尾部footer
    class FooterViewHolder extends RecyclerView.ViewHolder {
        TextView footer;

        public FooterViewHolder(@NonNull View itemView) {
            super(itemView);
            footer = itemView.findViewById(R.id.footer);
        }
    }

之后我们可以在相应adapter中设置一些三个类型相关的参数来配置:

  • 设置是否存在header/footer;
  • 移除header/footer;
  • 为header/footer单独设置数据(我这里方便直接设置的是String类型的文字显示)
  • ...

具体代码如下:

(这里提醒一句当我们存在header的时候我们content内容的position记得mDatas.get(position - mHeaderCount),防止数组下标越界错误.)

package com.example.testdemo2;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;
import java.util.Random;

/**
 * create by JOY
 * create on 4/9/21
 * description
 */
public class TestAdapter extends RecyclerView.Adapter {

    /**
     * item 类型
     */
    private static final int CONTENT_ITEM = 0;
    /**
     * footer 类型
     */
    private static final int FOOTER_ITEM = 1;
    /**
     * header 类型
     */
    private static final int HEADER_ITEM = 2;

    /**
     * footer
     */
    private int mFooterCount = 0;
    /**
     * header
     */
    private int mHeaderCount = 0;

    private String headerText = "Header";
    private String footerText = "Footer";


    private List mDatas;

    public TestAdapter(List mDatas) {
        this.mDatas = mDatas;
    }

    public void setmDatas(List mDatas) {
        this.mDatas = mDatas;
        notifyDataSetChanged();
    }

    /**
     * 根据特定条件移除footer
     */
    public void removeFooter() {
        mFooterCount = 0;
    }

    /**
     * 根据特定条件移除header
     */
    public void removeHeader() {
        mHeaderCount = 0;
    }

    /**
     * 根据特定条件添加header
     */
    public void setHeader() {
        mHeaderCount = 1;
    }

    /**
     * 根据特定条件添加footer
     */
    public void setFooter() {
        mFooterCount = 1;
    }

    public void setHeaderText(String headerText) {
        this.headerText = headerText;
    }

    public void setFooterText(String footerText) {
        this.footerText = footerText;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == HEADER_ITEM) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_header, parent, false);
            return new HeaderViewHolder(view);
        } else if (viewType == FOOTER_ITEM) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_footer, parent, false);
            return new FooterViewHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_test, parent, false);
            return new ContentViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof HeaderViewHolder) {
            HeaderViewHolder headerViewHolder = (HeaderViewHolder) holder;
            headerViewHolder.header.setText(headerText);

        } else if (holder instanceof FooterViewHolder) {
            FooterViewHolder footerViewHolder = (FooterViewHolder) holder;
            footerViewHolder.footer.setText(footerText);

        } else if (holder instanceof ContentViewHolder) {
            //随机颜色
            Random random = new Random();
            int color = Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256));
            ContentViewHolder contentViewHolder = (ContentViewHolder) holder;
            contentViewHolder.textView.setBackgroundColor(color);
            contentViewHolder.textView.setText(mDatas.get(position - mHeaderCount));
        }

    }

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

    @Override
    public int getItemViewType(int position) {
        int count = mDatas.size();
        if (mHeaderCount != 0 && position < mHeaderCount) {
            return HEADER_ITEM;
        } else if (mFooterCount != 0 && position >= (mHeaderCount + count)) {
            return FOOTER_ITEM;
        } else {
            return CONTENT_ITEM;
        }
    }

    //中间内容显示
    class ContentViewHolder extends RecyclerView.ViewHolder {
        TextView textView;

        public ContentViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.text);
        }
    }

    //头部header
    class HeaderViewHolder extends RecyclerView.ViewHolder {
        TextView header;

        public HeaderViewHolder(@NonNull View itemView) {
            super(itemView);
            header = itemView.findViewById(R.id.header);
        }
    }

    //尾部footer
    class FooterViewHolder extends RecyclerView.ViewHolder {
        TextView footer;

        public FooterViewHolder(@NonNull View itemView) {
            super(itemView);
            footer = itemView.findViewById(R.id.footer);
        }
    }

}

 

你可能感兴趣的:(Android,RecyclerView,java,android,studio,android)