WenJieWangFlyToWorld

导航

StackView实现叠加在一起的图片循环移动像循环队列一样

MainActivity.java

  1. public class MainActivity extends Activity
  2. {
  3.  
  4. StackView stackView;
  5. int[] imageIds=new int[]
  6. {R.drawable.bomb5,R.drawable.bomb6,R.drawable.bomb7,R.drawable.bomb8,
  7. R.drawable.bomb9,R.drawable.bomb10,R.drawable.bomb11,R.drawable.bomb12,R.drawable.bomb13,
  8. R.drawable.bomb14,R.drawable.bomb15,R.drawable.bomb16};
  9.  
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.main);
  14.  
  15. stackView = (StackView) findViewById(R.id.mStackView);
  16. List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
  17. for (int i=0;i<imageIds.length;i++) {
  18. Map<String, Object> listItem=new HashMap<String, Object>();
  19. listItem.put("image",imageIds[i]);
  20. listItems.add(listItem);
  21. }
  22.  
  23. SimpleAdapter simpleAdapter=new SimpleAdapter(this,listItems,
  24. R.layout.cell,new String[] {"image"},new int[] {R.id.image1});
  25. stackView.setAdapter(simpleAdapter);
  26.  
  27.  
  28. }
  29. public void prev(View view) {
  30. stackView.showPrevious();
  31. }
  32.  
  33. public void next(View view) {
  34. stackView.showNext();
  35.  
  36. }
  37. }

 

XML文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:orientation="horizontal"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent">
  7. <StackView
  8. android:id="@+id/mStackView"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:loopViews="true" />
  12. <LinearLayout
  13. android:orientation="horizontal"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content">
  16. <Button
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:text="上一个"
  20. android:onClick="prev"/>
  21. <Button
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:text="下一个"
  25. android:onClick="next"/>
  26. </LinearLayout>
  27. </LinearLayout>

 

Cell文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="horizontal"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:gravity="center_horizontal"
  7. android:padding="2pt">
  8. <ImageView
  9. android:id="@+id/image1"
  10. android:layout_width="120dp"
  11. android:layout_height="120dp"
  12. />
  13. </LinearLayout>

 

效果:

 

posted on 2017-06-01 16:38  WenjieWangFlyToWorld  阅读(1114)  评论(0编辑  收藏  举报