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

Android 學習教程(6):可動態合併單元的 GridView 實作

編輯:關於 Android 程式設計

本文分享一個可變尺寸的 GridView(StaggeredGridView)實作範例,重點在於每個單元格可以透過 LayoutParams.span 動態指定佔用的欄數,從而拼出錯落有致的版面。

MainActivity.java

作為入口頁,只負責跳轉到展示頁:

 package siso.haha; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { private Button btnStaggeredGridView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnStaggeredGridView=(Button)findViewById(R.id.btnStaggeredGridView); btnStaggeredGridView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(MainActivity.this,staggeredgridviewActivity.class); //直接啟動一個 Activity startActivity(intent); } }); } } 

staggeredgridviewActivity.java

 package siso.haha; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import java.util.Random; import viewHelper.StaggeredGridView; import viewHelper.StaggeredGridView.LayoutParams; public class staggeredgridviewActivity extends Activity { private final static String TAG = staggeredgridviewActivity.class.getSimpleName(); private StaggeredGridView mSGV; private SGVAdapter mAdapter; private StaggeredGridView.OnScrollListener mScrollListener = new StaggeredGridView.OnScrollListener() { @Override public void onScrollStateChanged(ViewGroup view, int scrollState) { Log.d(TAG, "[onScrollStateChanged] scrollState:" + scrollState); switch (scrollState) { case SCROLL_STATE_IDLE: setTitle("SCROLL_STATE_IDLE"); break; case SCROLL_STATE_FLING: setTitle("SCROLL_STATE_FLING"); break; case SCROLL_STATE_TOUCH_SCROLL: setTitle("SCROLL_STATE_TOUCH_SCROLL"); break; default: break; } } @Override public void onScroll(ViewGroup view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { Log.d(TAG, "[onScroll] firstVisibleItem:" + firstVisibleItem + " visibleItemCount:"+visibleItemCount + " totalItemCount:" + totalItemCount); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_staggeredgridview); mAdapter = new SGVAdapter(this); mSGV = (StaggeredGridView) findViewById(R.id.grid); mSGV.setColumnCount(4); mSGV.setAdapter(mAdapter); mSGV.setOnScrollListener(mScrollListener); } @Override public boolean onCreateOptionsMenu(Menu menu) { return true; } private final class SGVAdapter extends BaseAdapter { LayoutInflater mInflater; public SGVAdapter(Context ctx) { mInflater = LayoutInflater.from(ctx); } @Override public int getCount() { return 30; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } Random r = new Random(); @Override public View getView(int position, View convertView, ViewGroup parent) { //LayoutParams 相當於一個 Layout 的資訊包,封裝了位置、高、寬等資訊。 //可以這樣理解:在棋盤上每個棋子都佔據一個位置,這個位置資訊就是它的 LayoutParams。 //寬與高可以設為三種值:確定的數值、FILL_PARENT(填滿父容器)、WRAP_CONTENT(包裹內容)。 final LayoutParams lp; final View v; switch (position) { case 0: case 29: v = mInflater.inflate(R.layout.element_header, parent, false); lp = new LayoutParams(v.getLayoutParams()); lp.span = mSGV.getColumnCount(); break; case 8: case 9: case 18: case 19: v = mInflater.inflate(R.layout.element_item_small, parent, false); lp = new LayoutParams(v.getLayoutParams()); lp.span = 1; break; case 10: case 20: v = mInflater.inflate(R.layout.element_item_large, parent, false); lp = new LayoutParams(v.getLayoutParams()); lp.span = 4; break; default: v = mInflater.inflate(R.layout.element_item, parent, false); lp = new LayoutParams(v.getLayoutParams()); lp.span = 2; break; } v.setLayoutParams(lp); return v; } } }

這裡的關鍵在 getView 的 switch 分支:表頭佔滿四欄、大圖卡佔四欄、小圖卡佔一欄、普通項目佔兩欄,透過 lp.span 就能讓同一個網格裡出現不同寬度的單元。

版面檔

activity_main.xml:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="siso.haha.MainActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="動態GridView" android:id="@+id/btnStaggeredGridView" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button2" android:layout_below="@+id/btnStaggeredGridView" android:layout_centerHorizontal="true" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button3" android:layout_below="@+id/button2" android:layout_centerHorizontal="true" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button4" android:layout_below="@+id/button3" android:layout_centerHorizontal="true" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button5" android:layout_below="@+id/button4" android:layout_centerHorizontal="true" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button6" android:layout_below="@+id/button5" android:layout_centerHorizontal="true" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button7" android:layout_below="@+id/button6" android:layout_centerHorizontal="true" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button8" android:layout_below="@+id/button7" android:layout_centerHorizontal="true" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button9" android:layout_below="@+id/button8" android:layout_centerHorizontal="true" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button10" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" /> </RelativeLayout> 

activity_staggeredgridview.xml:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".staggeredgridviewActivity" > <viewHelper.StaggeredGridView android:id="@+id/grid" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout> 

element_header.xml:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="56dp" android:background="@drawable/bg_white_box" android:gravity="center_vertical" android:orientation="horizontal" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="做一點事..." /> <ProgressBar android:id="@+id/progressBar1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout> 

element_item.xml:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/bg_white_box" android:orientation="vertical" android:padding="2dp" > <ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="100dp" android:src="@android/color/holo_green_light" /> <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="56dp" android:layout_margin="8dp" android:drawableRight="@android:drawable/ic_menu_info_details" android:gravity="center_vertical" android:lines="2" android:text="列表項文本在這裡,圖像上面" android:textAppearance="?android:attr/textAppearance" /> </LinearLayout> 

element_item_large.xml:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/bg_white_box" android:orientation="vertical" android:padding="2dp" > <ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="160dp" android:src="@android:color/holo_orange_light" /> <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp" android:drawableRight="@android:drawable/ic_menu_info_details" android:gravity="center_vertical" android:lines="2" android:text="列表項文本在這裡,圖像上面" android:textAppearance="?android:attr/textAppearance" /> </LinearLayout> 

element_item_small.xml:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/bg_white_box" android:orientation="vertical" android:padding="2dp" > <ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="100dp" android:src="@android:color/holo_red_light" /> <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="56dp" android:layout_margin="8dp" android:drawableRight="@android:drawable/ic_menu_info_details" android:gravity="center_vertical" android:lines="2" android:text="列表項文本在這裡,圖像上面" android:textAppearance="?android:attr/textAppearance" /> </LinearLayout> 

bg_white_box.xml:

 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="https://schemas.android.com/apk/res/android" android:shape="rectangle" > <solid android:color="@android:drawable/screen_background_light" /> <stroke android:width="1dp" android:color="@android:color/holo_blue_dark" /> </shape> 

執行結果

整體而言,這種「span 可變」的網格適合做出資訊流中大小交錯的卡片效果。若專案已經採用 RecyclerView,也可以透過 GridLayoutManager 的 SpanSizeLookup 達到同等效果,程式碼會更精簡。

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