Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

RecyclerView 添加 Header 與 Footer,並適配水平布局的左右結構

編輯:關於 Android 程式設計

折騰了整整一個下午,踩過不少坑:最麻煩的是當 LayoutManager 設為水平方向時,頭尾佈局必須改成「左右結構」,否則寬高約束完全錯位;另外還遇到某個 item 需要佔多個跨度(類似單元格合併)的情況。

好在最終都調通了。下面把成果按六種 LayoutManager 組合逐一記錄。

效果預覽

LinearLayoutManager.VERTICAL

LinearLayoutManager.HORIZONTAL

GridLayoutManager.VERTICAL

GridLayoutManager.HORIZONTAL

StaggeredGridLayoutManager.VERTICAL

StaggeredGridLayoutManager.HORIZONTAL

其中一個 Footer 的細節:

Demo 的目錄結構:

核心思路

RecyclerView 本身不像 ListView 那樣提供 addHeaderView(),實現頭尾的思路是:在 Adapter 內部用 itemViewType 標記頭、尾與普通項,並為它們準備不同的佈局。

而本文的關鍵補充在於:先讀出當前的 LayoutManager 及其方向,如果是水平方向,就換用另一套「左右結構」的頭尾佈局——垂直時頭尾是 match_parent × wrap_content,水平時則要反過來變成 wrap_content × match_parent。

MainActivity

public class MainActivity extends AppCompatActivity {

    private int listHeaderLayoutId = R.layout.list_header_layout;
    private int listFooterLayoutId = R.layout.list_footer_layout;

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

        RecyclerView rv = (RecyclerView) findViewById(R.id.id_rv);
        rv.setLayoutManager(new LinearLayoutManager(this));
        /* 其他可替換的 LayoutManager:
        rv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        rv.setLayoutManager(new GridLayoutManager(this, 3));
        rv.setLayoutManager(new GridLayoutManager(this, 3, GridLayoutManager.HORIZONTAL, false));
        rv.setLayoutManager(new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL));
        rv.setLayoutManager(new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.HORIZONTAL));
        */

        MyRecyclerViewAdapter adapter = new MyRecyclerViewAdapter(getData());

        // 針對 HORIZONTAL 方向單獨設置左右結構的頭尾佈局
        LayoutManager layoutManager = rv.getLayoutManager();
        if (layoutManager instanceof StaggeredGridLayoutManager) {
            StaggeredGridLayoutManager slm = (StaggeredGridLayoutManager) layoutManager;
            boolean horizontal = slm.getOrientation() == StaggeredGridLayoutManager.HORIZONTAL;
            listHeaderLayoutId = horizontal ? R.layout.list_header_layout_4_horizontal : listHeaderLayoutId;
            listFooterLayoutId = horizontal ? R.layout.list_footer_layout_4_horizontal : listFooterLayoutId;
        } else if (layoutManager instanceof GridLayoutManager) {
            GridLayoutManager glm = (GridLayoutManager) layoutManager;
            boolean horizontal = glm.getOrientation() == GridLayoutManager.HORIZONTAL;
            listHeaderLayoutId = horizontal ? R.layout.list_header_layout_4_horizontal : listHeaderLayoutId;
            listFooterLayoutId = horizontal ? R.layout.list_footer_layout_4_horizontal : listFooterLayoutId;
        } else if (layoutManager instanceof LinearLayoutManager) {
            LinearLayoutManager llm = (LinearLayoutManager) layoutManager;
            boolean horizontal = llm.getOrientation() == LinearLayoutManager.HORIZONTAL;
            listHeaderLayoutId = horizontal ? R.layout.list_header_layout_4_horizontal : listHeaderLayoutId;
            listFooterLayoutId = horizontal ? R.layout.list_footer_layout_4_horizontal : listFooterLayoutId;
        }

        // 注意:inflate 時 root 傳 RecyclerView,attachToRoot 傳 false
        View headView = LayoutInflater.from(this).inflate(listHeaderLayoutId, rv, false);
        View footView = LayoutInflater.from(this).inflate(listFooterLayoutId, rv, false);
        adapter.setHeaderView(headView);
        adapter.setFooterView(footView);

        rv.setHasFixedSize(true);
        rv.setItemAnimator(new DefaultItemAnimator());
        rv.setAdapter(adapter);
    }

    public List<String> getData() {
        List<String> dataList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            dataList.add("str" + i);
        }
        return dataList;
    }
}

activity_main.xml 很簡單,一個 RelativeLayout 裡放一個 RecyclerView 即可。

垂直方向的佈局

list_header_layout.xml

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:background="@color/colorAccent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="10dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="head str" />
</LinearLayout>

list_footer_layout.xml

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:background="@color/colorPrimary"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="10dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="foot view" />
</LinearLayout>

list_item.xml

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:background="@android:color/darker_gray"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="10dp">

    <TextView
        android:id="@+id/id_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="normal str" />
</LinearLayout>

水平方向的佈局

水平時最需要改的是寬高:寬度改為 wrap_content,高度改為 match_parent,、gravity 也要從 center_horizontal 換成 center_vertical。

list_header_layout_4_horizontal.xml

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_margin="5dp"
    android:background="@color/colorAccent"
    android:gravity="center_vertical"
    android:orientation="vertical"
    android:padding="10dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="left str" />
</LinearLayout>

list_footer_layout_4_horizontal.xml

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_margin="5dp"
    android:background="@color/colorPrimary"
    android:gravity="center_vertical"
    android:orientation="vertical"
    android:padding="10dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="right view" />
</LinearLayout>

list_item_4_horizontal.xml

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:background="@android:color/darker_gray"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="10dp">

    <TextView
        android:id="@+id/id_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="normal str" />
</LinearLayout>

小結

整個方案的核心只有兩句話:頭尾本質上就是兩個特殊類型的 item,放進 Adapter 裡管理;水平方向時,別忘了把頭尾佈局的寬高約束整體旋轉九十度。剩下的都是細節堆疊而已。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved