直接上代码:
package cn.bupt.lg_stackview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.SimpleAdapter.ViewBinder;
import android.widget.StackView;
public class MainActivity extends Activity {
private StackView stackView;
private Button buttonPre,buttonNext;
// int[] imagesId =new int[] {R.drawable.dreamweaver,R.drawable.gnome,R.drawable.kaspersky,R.drawable.luma,R.drawable.wernerboy};
int[] imagesId =new int[] {R.drawable.bomb5,R.drawable.bomb6,R.drawable.bomb7,R.drawable.bomb8,R.drawable.bomb9};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
stackView = (StackView) findViewById(R.id.stackView);
buttonPre = (Button) findViewById(R.id.buttonPre);
buttonNext = (Button) findViewById(R.id.buttonNext);
List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
for (int j = 0; j < imagesId.length; j++) {
Map<String,Object> items = new HashMap<String, Object>();
items.put("image", imagesId[j]);
data.add(items);
}
SimpleAdapter adapter = new SimpleAdapter(this, data,R.layout.cell,new String[] {"name"}, new int[] {R.id.imageView});
stackView.setAdapter(adapter);
}
public void Pre(View view){
stackView.showPrevious();
}
public void Next(View view){
stackView.showNext();
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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/buttonPre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="Pre"
android:text="Pre" />
<Button
android:id="@+id/buttonNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="Next"
android:text="Next" />
</LinearLayout>
</LinearLayout>
cell.xml
<?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/imageView"
android:layout_width="120dp"
android:layout_height="120dp"
/>
</LinearLayout>
logcat(点击Pre,Next或者在stackView中上下滑动图片时:):
12-19 03:05:47.230: E/BitmapFactory(2005): Unable to decode stream: java.io.FileNotFoundException: /: open failed: EISDIR (Is a directory)
12-19 03:05:47.230: I/System.out(2005): resolveUri failed on bad bitmap uri: