给RecyclerView 添加HeadView

本文介绍如何在RecyclerView中添加HeadView。由于RecyclerView本身不提供类似ListView的addHeadView方法,我们需要通过自定义实现。核心思路是利用getItemViewType方法来判断加载不同布局。首先准备AdaperParam Bean,然后在适配器中根据是否有头部布局进行判断。最后通过代码实现,此处使用了InitRecyclerView工具类。
摘要由CSDN通过智能技术生成

ListView为我们提供了addHeadView方法添加头部,RecyclerView没有该方法,下面我们来做这个工作。主要的思路还是根据getItemViewType的返回值加载不同的布局文件。

第一步:准备bean——AdaperParam,适配器需要的几个参数,如下

<span style="font-size:14px;">package com.dfsebook.myrecycleradapter.bean;

import java.util.List;


public class AdapterParam <T,V>{

    private V headData;//头部绑定数据

    private List<T> mDatas;//items绑定数据

    private int headLayoutId;//头部对应的布局文件

    private int itemLayouyId;//item对应的布局文件

    private int headBrId;//头部对应的资源

    private int itemBrId;//item对应的资源

    public V getHeadData() {
        return headData;
    }

    public void setHeadData(V headData) {
        this.headData = headData;
    }

    public List<T> getmDatas() {
        return mDatas;
    }

    public void setmDatas(List<T> mDatas) {
        this.mDatas = mDatas;
    }

    public int getHeadLayoutId() {
        return headLayoutId;
    }

    public void setHeadLayoutId(int headLayoutId) {
        this.headLayoutId = headLayoutId;
    }

    public int getItemLayouyId() {
        return itemLayouyId;
    }

    public void setItemLayouyId(int itemLayouyId) {
        this.itemLayouyId = itemLayouyId;
    }

    public int getHeadBrId() {
        return headBrId;
    }

    public void setHeadBrId(int headBrId) {
        this.headBrId = headBrId;
    }

    public int getItemBrId() {
        return itemBrId;
    }

    public void setItemBrId(int itemBrId) {
        this.itemBrId = itemBrId;
    }
}</span>
第二步:改写上篇Adapter

<span style="font-size:14px;">package com.dfsebook.myrecycleradapter.adapter;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.vie
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要给 RecyclerView 的头部和尾部添加圆角背景,可以采用以下步骤: 1. 定义一个自定义的 Drawable,可以在 drawable 目录下新建一个 XML 文件,命名为 round_bg.xml,代码如下: ``` <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:topLeftRadius="10dp" android:topRightRadius="10dp" android:bottomLeftRadius="10dp" android:bottomRightRadius="10dp"/> <solid android:color="@color/white"/> </shape> ``` 该 Drawable 定义了一个矩形的圆角背景,四个角的圆角半径均为 10dp,填充颜色为白色。 2. 在 RecyclerView 的 Adapter 中,重写 getItemViewType 方法,在其中判断当前位置是否为头部或尾部,返回不同的 ViewType。例如: ``` @Override public int getItemViewType(int position) { if (position == 0) { return VIEW_TYPE_HEADER; } else if (position == getItemCount() - 1) { return VIEW_TYPE_FOOTER; } else { return VIEW_TYPE_ITEM; } } ``` 3. 在 onCreateViewHolder 方法中创建对应的 ViewHolder,并根据 ViewType 设置不同的布局和背景。例如: ``` @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if (viewType == VIEW_TYPE_HEADER) { View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.header_layout, parent, false); view.setBackground(ContextCompat.getDrawable(parent.getContext(), R.drawable.round_bg)); return new HeaderViewHolder(view); } else if (viewType == VIEW_TYPE_FOOTER) { View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.footer_layout, parent, false); view.setBackground(ContextCompat.getDrawable(parent.getContext(), R.drawable.round_bg)); return new FooterViewHolder(view); } else { View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.item_layout, parent, false); return new ItemViewHolder(view); } } ``` 4. 最后,在对应的布局文件中,可以根据需求自定义头部和尾部的样式。 以上就是在 RecyclerView 中给头部和尾部添加圆角背景的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值