使用RecyclerView添加Header和Footer的方法

2019-12-10 18:50:08于丽

2:主布局文件 activity_main.xml 很简单里面一个RecyclerView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://www.easck.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
xmlns:android="http://www.easck.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rv_list"
/>
</LinearLayout>

3:列表项布局 rv_item.xml 外面一个CardView的卡片式容器里面一个TextView

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:card_view="http://www.easck.com/apk/res-auto"
xmlns:android="http://www.easck.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:id="@+id/cv_item"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_item_text"
android:text="test"
android:layout_margin="8dp"
/>
</LinearLayout>
</android.support.v7.widget.CardView>

4:列表头部布局 rv_header.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:card_view="http://www.easck.com/apk/res-auto"
xmlns:android="http://www.easck.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:id="@+id/cv_item"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp"
card_view:cardBackgroundColor="#4CAF50"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Header"
android:textSize="30sp"
android:textColor="#ffffff"
android:gravity="center"
/>
</LinearLayout>
</android.support.v7.widget.CardView>