RecycleView添加头部和底部

本文详细介绍了如何在Android的RecycleView中添加头部和底部,包括线性布局、表格布局和瀑布流的实现。通过在Adapter中定义不同布局和item状态,根据状态加载相应布局文件,并提供了具体的Java代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现思路

  1. 在Adapter中定义不同的布局和item状态
  2. 判断item的状态,加载对应的布局文件

实例代码

在gradle中添加下面的依赖

compile 'com.android.support:recyclerview-v7:23.1.0'
compile 'com.android.support:cardview-v7:23.1.0'
布局文件

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.gjg.learn.recycleview_head_foot.MainActivity">

   <android.support.v7.widget.RecyclerView
       android:id="@+id/rv_list"
       android:layout_width="match_parent"
       android:layout_height="match_parent">

   </android.support.v7.widget.RecyclerView>
</RelativeLayout>

rv_header.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cv_header"
    android:foreground="?android:attr/selectableItemBackground"
    app:cardCornerRadius="6dp"
    app:cardElevation="4dp"
    app:cardBackgroundColor="#4CAF50"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:gravity="center">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="header"
            android:id="@+id/tv_header"
            android:textSize="30sp"
            android:textColor="#ffffff"
            android:padding="4dp"
            android:layout_margin="8dp"/>
    </LinearLayout>

</android.support.v7.widget.CardView>

rv_footer.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cv_footer"
    android:foreground="?android:attr/selectableItemBackground"
    app:cardCornerRadius="6dp"
    app:cardElevation="4dp&#
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值