Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android的AdapterViewFlipper和Stackview-android學習之旅(三十)

Android的AdapterViewFlipper和Stackview-android學習之旅(三十)

編輯:關於Android編程

AdapterViewFlipper簡介

AdapterViewFlipper繼承了AdapterViewAnimater。每次只能顯示一個組件,用showPrevious()和showNext()來顯示這些組件上一個和下一個。還有startFlipping()控制它自動播放。

AdapterViewFlipper屬性

這裡寫圖片描述
這裡寫圖片描述

代碼實例

package peng.liu.testview;

import android.app.Activity;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterViewFlipper;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;


public class MainActivity extends Activity {
    private int[] imageIds = new int[]{
        R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher
    };
    private AdapterViewFlipper flipper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        flipper = (AdapterViewFlipper) findViewById(R.id.flipper);
        BaseAdapter adapter = new BaseAdapter() {
            @Override
            public int getCount() {
                return 0;
            }

            @Override
            public Object getItem(int i) {
                return null;
            }

            @Override
            public long getItemId(int i) {
                return 0;
            }

            @Override
            public View getView(int i, View view, ViewGroup viewGroup) {
               ImageView image = new ImageView(MainActivity.this);
               image.setImageResource(imageIds[i]);
                image.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                image.setScaleType(ImageView.ScaleType.FIT_XY);
                return image;
            }
        };

    }
}

    
    

布局代碼


    
    

StackView簡介

StackView也是繼承於AdapterViewAnimator,它是以重疊在一起的,使用showPrevious()和showNext()分別顯示上一張和下一張。屬性類似AdapterViewFlipper

實例

package peng.liu.testview;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.res.TypedArray;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterViewFlipper;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.StackView;

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


public class MainActivity extends Activity {
    private int[] imageIds = new int[]{
        R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher
    };
    private StackView stack;
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        stack = (StackView) findViewById(R.id.stack);
        List> listItems = new ArrayList>();
        for (int i = 0;i item = new HashMap();
            item.put("image",imageIds[i]);
            listItems.add(item);
        }
        SimpleAdapter adapter = new SimpleAdapter(this,listItems,R.layout.simple_item,new String[]{"image"},new int[]{R.id.image01});
        stack.setAdapter(adapter);
    }

}

    
    

    

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved