<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <StackView android:id="@+id/stackView" android:layout_width="match_parent" android:layout_height="wrap_content" android:loopViews="true"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/button1" 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>
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_horizontal" android:padding="2pt" > <ImageView android:id="@+id/image1" android:layout_width="120dp" android:layout_height="120dp" /> </LinearLayout>
package com.example.stackview; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.SimpleAdapter; import android.widget.StackView; public class MainActivity extends Activity { int[] imageIds=new int[]{ R.drawable.shuangzi,R.drawable.shuangyu,R.drawable.chunv, R.drawable.tiancheng,R.drawable.tianxie,R.drawable.sheshou,R.drawable.juxie, R.drawable.shuiping,R.drawable.shizi,R.drawable.baiyang,R.drawable.jinniu,R.drawable.mojie }; StackView stackView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); stackView=(StackView) findViewById(R.id.stackView); 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 adapter=new SimpleAdapter(this, listItems, R.layout.cell,new String[]{"image"}, new int[]{R.id.image1}); stackView.setAdapter(adapter); } public void prev(View source){ stackView.showPrevious(); } public void next(View source){ stackView.showNext(); } }
功能和上一课的差不多,代码同样很容易理解。