content_main.xml布局
<? 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"
>
<StackView
android :id= "@+id/mStackView"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :loopViews= "false"
/>
<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>
cell.xml布局
<? xml version= "1.0" encoding= "utf-8" ?>
<LinearLayout xmlns: android ="http://schemas.android.com/apk/res/android"
android :orientation= "vertical"
android :layout_width= "match_parent"
android :layout_height= "match_parent"
>
<ImageView
android :id= "@+id/image1"
android :layout_width= "150dp"
android :layout_height= "200dp" />
</LinearLayout>
用SimpleAdapter将数据显示在布局上
package com.eson.stackview ;
import android.os.Bundle ;
import android.support.v7.app.AppCompatActivity ;
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 StackViewTest extends AppCompatActivity {
int [] imageIds= new int []{
R.drawable. img1, R.drawable. img2, R.drawable. img3, R.drawable. img4,
R.drawable. img5 ,R.drawable. img6 ,R.drawable. img7 ,R.drawable. img8 ,
R.drawable. img9 ,R.drawable. img10 ,R.drawable. img11 ,R.drawable. img12 ,
} ;
private StackView stackView ;
@Override
protected void onCreate (Bundle savedInstanceState) {
super .onCreate(savedInstanceState) ;
setContentView(R.layout. content_main );
stackView = (StackView) findViewById(R.id. mStackView );
//创建一个List对象,List对象的元素是Map
List<Map<String , Object>> listItems= new ArrayList<>() ;
for ( int i = 0 ; i < imageIds . length; i++) {
Map<String ,Object> listItem= new HashMap<>();
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() ;
}
}