android StackView将照片堆叠在一起通过拖动或单机按钮来显示上一张下一张图片

本文介绍如何在Android应用中使用StackView组件创建一个图片堆叠效果,并通过按钮点击实现上下滚动查看图片的功能。在MainActivity中设置StackView的适配器并监听按钮点击事件,布局文件中包含两个导航按钮和StackView组件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主要的activity


public class MainActivity extends Activity {

 private Button pre;
 private Button next;
 private StackView stackView;;
 private int[] imageIds = { R.drawable.fy_d_go, R.drawable.fy_d_kf,
   R.drawable.fy_d_kf2, R.drawable.fy_d_kf_s, R.drawable.fy_d_more_s,
   R.drawable.ic_launcher };
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  pre = (Button) findViewById(R.id.button1);
  next = (Button) findViewById(R.id.button2);
  stackView = (StackView) findViewById(R.id.stackView1);
  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.textview, new String[]{"image"}, new int[]{R.id.imageview3});
  stackView.setAdapter(adapter);
  pre.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View arg0) {
    stackView.showPrevious();
   }
  });
  next.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View arg0) {
    stackView.showNext();
   }
  });
 }
}

布局文件xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >


    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="上一个" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button1"
        android:layout_toRightOf="@+id/button1"
        android:text="下一个" />

    <StackView
        android:id="@+id/stackView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button2"
        android:layout_marginTop="74dp" >
    </StackView>

</RelativeLayout>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值