StackView的功能和用法

   StackView也是AdapterViewAnimator的子类,它也用于显示Adapter提供的系列View。SackView将会以“堆叠(Stack)”方式来显示多个列表项。

    为了控制StackView显示的View组件,StackView提供了如下两种控制方式。

  • 拖走StackView中处于顶端的View,下一个View将会显示出来。将上一个View拖进StackView,将使之显示出来。
  • 通过调用StackView的showNext()、showPrevious()控制显示上一个、下一个组件。

    下面的实例示范了StackView的功能和用法。

     该实例会使用StackView将照片叠在一起,并允许用户通过拖动或单击按钮来显示上一张、下一张图片。该实例的布局文件如下:

   

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
  <StackView android:id="@+id/mStackView"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:loopViews="true"/>
  <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>

    上面的布局文件中定义了一个StackView组件,该StackView将会以“叠”的方式显示多个View组件。与所有AdapterView类似的是,只要为StackView设置Adapter即可。

     下面Activity将会创建一个SimpleAdapter作为StackView的Adapter,并为布局文件中的两个按钮的onClick事件提供处理方法。下面是该Activity的代码。

package org.crazyit.helloworld;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.*;

public class StackViewTest extends Activity {
     StackView stackView;
     int[] imageIds=new int[]{
             R.drawable.bomb5,
             R.drawable.bomb6,
             R.drawable.bomb7,
             R.drawable.bomb8,
             R.drawable.bomb9,
             R.drawable.bomb10,
             R.drawable.bomb11,
             R.drawable.bomb12,
             R.drawable.bomb13,
             R.drawable.bomb14,
             R.drawable.bomb15,
             R.drawable.bomb16
     };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stack_view_test);
        stackView=(StackView)findViewById(R.id.mStackView);
        //创建一个List对象,List对象的元素是Map
        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
        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();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.stack_view_test, menu);
        return true;
    }

}

上面的程序中使用了R.layout.cell布局文件如下:

<?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"
    android:gravity="center_horizontal"
    android:padding="2pt" >
  <ImageView android:id="@+id/image1"
      android:layout_width="120dp"
      android:layout_height="120dp" />  

</LinearLayout>

运行上面的Activity程序将会看到如下效果:

原文地址:https://www.cnblogs.com/wolipengbo/p/3381817.html