Android之RecycleView添加header和footer的实例详解


    上一篇讲了RecycleView的简单使用,如果还不清楚RecycleView的简单使用的,可以看https://blog.csdn.net/adaixiaoxiao/article/details/96976275


     RecyclerView虽然作为ListView的替代者有着较好的性能提升,但是ListView常用的addHeaderView,addFooterView,在RecyclerView中没有提供这个方法,我们应该如何为列表添加头部和底部呢?通过看ListView的源码可以知道ListView的添加Header和Footer是靠Adapter里面动态添加的,所以我们按照这个思路也给RecyclerView添加HeaderView和FooterView,先看一下效果

Android之RecycleView添加header和footer的实例详解_第1张图片

    实现核心:RecyclerView实现添加HeaderView和FooterView的核心就是在Adapter里面的onCreateViewHolder根据viewType来判断是列表项还是HeaderView来分别加载不同的布局文件,下面来看下具体的实现代码和步骤。

  1. 第一步还是引入recyceview的依赖 

implementation ‘com.android.support:recyclerview-v7:28.0.0

   2.  RecyclerView的Adapter,在getItemViewType方法里面判断了当前Item的类型,然后在onCreateViewHolder跟据item的类型分别加载不同的布局以实现HeaderView和FooterView,参考以下代码

public class RecyAttendanceAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private List mDatas;

    //item类型
    public static final int ITEM_TYPE_HEADER = 0;
    public static final int ITEM_TYPE_CONTENT = 1;
    public static final int ITEM_TYPE_BOTTOM = 2;

    //header与footer的个数
    private int mHeaderCount = 1;
    private int mFooterCount = 1;

    public RecyAttendanceAdapter(Context mContext, List mDatas) {
        this.mContext = mContext;
        this.mDatas = mDatas;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //根据不同的viewType加载不同的布局
        if (viewType == ITEM_TYPE_HEADER) {
            return new HeaderViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_header_recycle, parent, false));
        } else if (viewType == ITEM_TYPE_BOTTOM) {
            return new FooterViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_footer_recycle, parent, false));
        } else {
            return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recyview_layout, parent, false));
        }

    }

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

        } else if (holder instanceof MyViewHolder) {
            //取出数据,进行显示填充
            String s = mDatas.get(position - mHeaderCount);
            ((MyViewHolder) holder).tv.setText(s);
        } else if (holder instanceof FooterViewHolder) {

        }
    }


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

    public int getContentItemCount() {
        return mDatas.size();
    }

    @Override
    public int getItemViewType(int position) {
        int dataItemCount = getContentItemCount();
        //头部view
        if (mHeaderCount != 0 && position < mHeaderCount) {
            return ITEM_TYPE_HEADER;
        } else if (mFooterCount != 0 && position >= (mHeaderCount + dataItemCount)) {
            return ITEM_TYPE_BOTTOM;
        } else {
            return ITEM_TYPE_CONTENT;
        }
    }

    //header
    class HeaderViewHolder extends RecyclerView.ViewHolder {
        public HeaderViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }

    //footer
    class FooterViewHolder extends RecyclerView.ViewHolder {
        public FooterViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }

    //内容
    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tv;

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

3.在MainActivity中的对recycleview的进行配置,与基本使用方法里面一样

public class MainActivity extends AppCompatActivity {

    private RecyclerView recy_attendance;
    private RecyAttendanceAdapter mRecyAttendanceAdapter;

    //测试数据
    private List testDatas = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initDatas();
        initView();
    }

    //测试数据
    private void initDatas() {
        for (int i = 0; i < 20; i++) {
            testDatas.add("data"+i);
        }
    }

    private void initView() {
        recy_attendance = findViewById(R.id.recy_attendance);
        //初始化适配器
        mRecyAttendanceAdapter = new RecyAttendanceAdapter(this,testDatas);
        //配置layoutManager
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recy_attendance.setLayoutManager(layoutManager);
        //配置adapter
        recy_attendance.setAdapter(mRecyAttendanceAdapter);
    }
}

4.  R.layout.item_header_recycle 的XML文件



    
    

5. R.layout.item_footer_recycle的XML文件



    

    

6. R.layout.item_recyview_layout的XML代码



    
    

到此recycleview添加header和footer整个就实现了。

你可能感兴趣的:(android)