HoneyComb3.0技术系列之StackView

1. 概述:

 

    StackView(堆View),一系列View的集合,这些View以层叠样式显示,并且View之间可以进行切换(切换方式有两种:(1)拖动StackView

 

    组件中的某一个View,(2)通过代码控制,下面介绍),这样每一个View都有机会显示给用户,供用户使用,并且在多个View切换过程中可以

 

    添加动画效果。

 

2. 效果图:

 

   (1)StackView默认显示效果,如下图:           HoneyComb3.0技术系列之StackView_第1张图片

   (2)用鼠标向下拖动蓝色View,如下图:           HoneyComb3.0技术系列之StackView_第2张图片

   (3)释放拖动的蓝色View,StackView会将下一个View切换到前台显示,如下图:           HoneyComb3.0技术系列之StackView_第3张图片

3. 代码实现:

 

   (1)res/layout/main.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>

 

   (2)主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};
    
}

 

   (3)StackView的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,layout,null,button,encoding,colors)