实现思路
- 在Adapter中定义不同的布局和item状态
- 判断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&#