Android StackView

分享到:

StackView控件用于将一组view逐个的展示给用户,它是一组view的集合这些view一个压着一个,view之间可以进行随意切换。下面通过实例了解StackView的使用方法。效果图如下所示:

stackview实例 stackview实例 stackview实例
StackView默认显示效果 向下拖动蓝色View,使之离开原来位置 释放拖动的蓝色View,StackView会将下一个View切换到前台显示,注意多出来的红色块

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">
  
  <StackView
          android:id = "@+id/mStackView"
          android:layout_width = "fill_parent" 
          android:layout_height = "wrap_content" 
          android:loopViews = "true"
          />
  
  <LinearLayout
          android:orientation = "vertical"
          android:layout_width = "wrap_content"
          android:layout_height = "wrap_content">
  
  <Button
          android:id = "@+id/previousButton"
          android:layout_width = "wrap_content"
          android:layout_height = "wrap_content"
          android:text = "前一张"/>
  
  <Button
          android:id = "@+id/nextButton"
          android:layout_width = "wrap_content"
          android:layout_height = "wrap_content"
          android:text = "后一张"/>
  
  </LinearLayout>
  
</LinearLayout>

Activity文件源码如下:

package com.focus.stackview;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.StackView;

public class StackViewActivity extends Activity {
          
          @Override
          public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          final StackView stackView = (StackView) findViewById(R.id.mStackView);
  
          ColorAdapter colorAdapter = new ColorAdapter(this, mColors);
          stackView.setAdapter(colorAdapter);
  
          final Button previousButon = (Button) findViewById(R.id.previousButton);
          previousButon.setOnClickListener(new OnClickListener() {
          public void onClick(View view) {
          stackView.showPrevious();
          }
          });
  
          final Button nextButton = (Button) findViewById(R.id.nextButton);
          nextButton.setOnClickListener(new OnClickListener() {
          public void onClick(View view) {
          stackView.showNext();
          }
          });
          }
  
          private int [] mColors = {Color.BLUE, Color.CYAN, Color.GRAY, Color.GREEN, Color.RED};
  
}

自定义的adapter

package com.focus.stackview;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;

public class ColorAdapter extends BaseAdapter {

         private Context mContext;
          
          private int [] mColors;
  
          public ColorAdapter(Context context, int [] colors) {
          mContext = context;
          mColors = colors;
          }
  
          public int getCount() {
          return mColors == null ? 0 : mColors.length;
          }
         public Object getItem(int position) {
          return mColors == null ? null : mColors[position];
          }
         public long getItemId(int position) {
          return position;
          }
         public View getView(int position, View cacheView, ViewGroup parent) {
          LinearLayout.LayoutParams colorLayoutParams = new LinearLayout.LayoutParams(100, 100);
  
          LinearLayout colorLayout = new LinearLayout(mContext);
          colorLayout.setBackgroundColor(mColors[position]);
          colorLayout.setLayoutParams(colorLayoutParams);
  
          return colorLayout;
          }
}     
昵    称:
验证码:

相关文档:

Android控件
Android基础知识
Android组件
Android实例