Android AdapterViewFlipper 使用示例

布局文件:




    

    

主程序代码:

package com.toby.personal.testlistview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterViewFlipper;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

    final private static String TAG = "Toby_Test";

    private AdapterViewFlipper adapterViewFlipper = null;
    private Button autoButton = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final int[] images = new int[]{
                R.drawable.img01, R.drawable.img02, R.drawable.img03, R.drawable.img04,
                R.drawable.img05, R.drawable.img06, R.drawable.img07, R.drawable.img08,
                R.drawable.img09, R.drawable.img10, R.drawable.img11, R.drawable.img12,
                R.drawable.dog_001, R.drawable.dog_002, R.drawable.dog_003, R.drawable.dog_004,
                R.drawable.dog_005,
                R.drawable.girl01, R.drawable.girl02, R.drawable.girl03, R.drawable.girl04,
                R.drawable.girl05
        };

        BaseAdapter adapter = new BaseAdapter() {
            @Override
            public int getCount() {
                return images.length;
            }

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

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

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

        adapterViewFlipper = (AdapterViewFlipper) findViewById(R.id.flipper);
        adapterViewFlipper.setAdapter(adapter);

        autoButton = (Button) findViewById(R.id.autoButton);
    }


    public void prev(View view) {
        adapterViewFlipper.showPrevious();
    }

    public void next(View view) {
        adapterViewFlipper.showNext();
    }

    public void auto(View view) {
        if (adapterViewFlipper.isFlipping()) {
            adapterViewFlipper.stopFlipping();
            autoButton.setText(R.string.auto);
        } else {
            adapterViewFlipper.startFlipping();
            autoButton.setText(R.string.stop);
        }
    }
}

显示效果:


Android AdapterViewFlipper 使用示例_第1张图片
显示效果

参考文献:《疯狂Android讲义(第2版)》

你可能感兴趣的:(Android AdapterViewFlipper 使用示例)