cell.xml 布局文件:
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cellImage"
android:layout_width="200dp"
android:layout_height="200dp"
/>
主布局文件:
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@color/colorGray"
>
android:id="@+id/stackView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:loopViews="true"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="prev"
android:text="@string/prev"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="next"
android:text="@string/next"
/>
主程序代码:
package com.toby.personal.testlistview;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.SimpleAdapter;
import android.widget.StackView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends Activity {
final private static String TAG = "Toby_Test";
private StackView stackView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final int[] images = new int[]{
R.drawable.img01, R.drawable.img02, R.drawable.img03, R.drawable.img04,
R.drawable.img05, R.drawable.img06, R.drawable.img07, R.drawable.img08,
R.drawable.img09, R.drawable.img10, R.drawable.img11, R.drawable.img12,
R.drawable.dog_001, R.drawable.dog_002, R.drawable.dog_003, R.drawable.dog_004,
R.drawable.dog_005,
R.drawable.girl01, R.drawable.girl02, R.drawable.girl03, R.drawable.girl04,
R.drawable.girl05
};
List> listItems = new ArrayList<>();
for (int i = 0; i < images.length; ++i) {
Map listItem = new HashMap<>();
listItem.put("image", images[i]);
listItems.add(listItem);
}
SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems, R.layout.cell,
new String[] {"image"}, new int[] {R.id.cellImage});
stackView = (StackView) findViewById(R.id.stackView);
stackView.setAdapter(simpleAdapter);
}
public void prev(View view) {
stackView.showPrevious();
}
public void next(View view) {
stackView.showNext();
}
}
运行效果:
运行效果
参考文献:《疯狂Android讲义(第2版)》