梯形網格視圖開源庫分析:AndroidStaggeredGrid

jopen 10年前發布 | 16K 次閱讀 Android開發 移動開發 AndroidStaggeredGrid

AndroidStaggeredGrid 是一個支持實現多行多列且可變尺寸的階梯形網格視圖的開源庫。

StaggeredGridView 的開發是因為該開源作者在實現其Etsy app時發現Google的Android庫中沒有提供相關功能以實現其需求,即需要在landscape & portrait兩種模式下顯示不同的行、列,且能在模、豎屏都保持單元格顯示同步, 運行穩定,還要支持自定義頁頭 & 頁腳。

該庫使用簡單,可以通過作者提供的/sample app的源碼了解使用細節,如圖:

梯形網格視圖開源庫分析:AndroidStaggeredGrid 梯形網格視圖開源庫分析:AndroidStaggeredGrid

梯形網格視圖開源庫分析:AndroidStaggeredGrid

總的來說使用該庫只需3步:

1. 將 StaggeredGridView 加入到需要顯示的布局文件中,如:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<com.etsy.android.grid.StaggeredGridView
        android:id="@+id/grid_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:item_margin="8dp"
        app:column_count_portrait="2"
        app:column_count_landscape="3" />
 </FrameLayout>

2. 配置屬性:

item_margin - The margin around each grid item (default 0dp).
column_count_portrait - The number of columns displayed when the grid is in portrait (default 2).
column_count_landscape - The number of columns displayed when the grid is in landscape (default 3).
grid_paddingLeft - Padding to the left of the grid. Does not apply to headers and footers (default 0).
grid_paddingRight - Padding to the right of the grid. Does not apply to headers and footers (default 0).
grid_paddingTop - Padding to the top of the grid. Does not apply to headers and footers (default 0).
grid_paddingBottom - Padding to the bottom of the grid. Does not apply to headers and footers (default 0).

3. 配置適配器,這與使用GridView/ListView時基本一樣:

ListAdapter adapter = ...;
StaggeredGridView gridView = (StaggeredGridView) findViewById(R.id.grid_view);
gridView.setAdapter(adapter);

說明: 由于列寬在屏幕方向橫豎變化時亦會變化, 網格視圖需要每個單元格維護自身的寬高比例,該庫提供了 DynamicHeightImageView 類作為示例實現單元格視圖高度按其寬度變化的功能。

項目主頁:http://www.baiduhome.net/lib/view/home/1388930104171

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!