StackView也是AdapterViewAnimator的子類,它也用於顯示Adapter提供的系列View。SackView將會以“堆疊(Stack)”方式來顯示多個列表項。
為了控制StackView顯示的View組件,StackView提供了如下兩種控制方式。
- 拖走StackView中處於頂端的View,下一個View將會顯示出來。將上一個View拖進StackView,將使之顯示出來。
- 通過調用StackView的showNext()、showPrevious()控制顯示上一個、下一個組件。
下面的實例示范了StackView的功能和用法。
該實例會使用StackView將照片疊在一起,並允許用戶通過拖動或單擊按鈕來顯示上一張、下一張圖片。該實例的布局文件如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" > <StackView android:id="@+id/mStackView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:loopViews="true"/> <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="上一個" android:onClick="prev" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下一個" android:onClick="next" /> </LinearLayout> </LinearLayout>
上面的布局文件中定義了一個StackView組件,該StackView將會以“疊”的方式顯示多個View組件。與所有AdapterView類似的是,只要為StackView設置Adapter即可。
下面Activity將會創建一個SimpleAdapter作為StackView的Adapter,並為布局文件中的兩個按鈕的onClick事件提供處理方法。下面是該Activity的代碼。
package org.crazyit.helloworld; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.*; public class StackViewTest extends Activity { StackView stackView; int[] imageIds=new int[]{ R.drawable.bomb5, R.drawable.bomb6, R.drawable.bomb7, R.drawable.bomb8, R.drawable.bomb9, R.drawable.bomb10, R.drawable.bomb11, R.drawable.bomb12, R.drawable.bomb13, R.drawable.bomb14, R.drawable.bomb15, R.drawable.bomb16 }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.stack_view_test); stackView=(StackView)findViewById(R.id.mStackView); //創建一個List對象,List對象的元素是Map List<Map<String,Object>> listItems=new ArrayList<Map<String,Object>>(); for(int i=0;i<imageIds.length;i++) { Map<String,Object> listItem=new HashMap<String,Object>(); listItem.put("image",imageIds[i]); listItems.add(listItem); } //創建一個SimpleAdapter SimpleAdapter simpleAdapter=new SimpleAdapter(this,listItems,R.layout.cell, new String[]{"image"},new int[]{R.id.image1}); stackView.setAdapter(simpleAdapter); } public void prev(View view) { //顯示上一個組件 stackView.showPrevious(); } public void next(View view) { //顯示下一個組件 stackView.showNext(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.stack_view_test, menu); return true; } }
上面的程序中使用了R.layout.cell布局文件如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:gravity="center_horizontal" android:padding="2pt" > <ImageView android:id="@+id/image1" android:layout_width="120dp" android:layout_height="120dp" /> </LinearLayout>
運行上面的Activity程序將會看到如下效果: