android stackView

1.activity_stack_view.xhtml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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=".StackViewActivity" >
    <StackView
        android:id="@+id/stackView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:loopViews="true"
        android:layout_toRightOf="@+id/stackView1"
        android:orientation="vertical" >
        <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>
</RelativeLayout>

2.StackViewActivity.java

package com.example.webview;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.StackView;
public class StackViewActivity extends Activity {
    private int[] mColors = { Color.BLUE, Color.CYAN, Color.GRAY, Color.GREEN,
            Color.RED };
    private StackView stackView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stack_view);
        stackView = (StackView) findViewById(R.id.stackView1);
        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();
            }
        });
    }
    @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, menu);
        return true;
    }
}

3.ColorAdapter.java

package com.example.webview;
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;
    }
            
}

5.AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.webview"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <application
        android:allowBackup="false"
        android:icon="@drawable/icon"
        android:label="@string/app_name" >
<activity
            android:name="com.example.webview.StackViewActivity"
            android:label="@string/title_activity_stack_view" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>


你可能感兴趣的:(android,stackview)