Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 的 AdapterViewFlipper 與 StackView 詳解

編輯:關於 Android 程式設計

AdapterViewFlipper 簡介

AdapterViewFlipper 繼承自 AdapterViewAnimator,每次只顯示一個子組件,通過 showPrevious() 與 showNext() 顯示上一個和下一個,還可用 startFlipping() 控制自動播放。它非常適合做圖片輪播、公告展示等場景。

AdapterViewFlipper 屬性

代碼實例

下面用一個 BaseAdapter 為 Flipper 提供圖片數據:

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 imageIds.length; }
            @Override public Object getItem(int i) { return imageIds[i]; }
            @Override public long getItemId(int i) { return i; }

            @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;
            }
        };
        flipper.setAdapter(adapter);
    }

    // 上一個 / 下一個 / 自動播放,可分別在布局的按鈕中調用
    public void previous(View v) { flipper.showPrevious(); }
    public void next(View v)     { flipper.showNext(); }
    public void auto(View v)     { flipper.startFlipping(); }
}

布局代碼

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/previous"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="previous"
            android:text="previous" />
        <Button
            android:id="@+id/next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="next"
            android:text="next" />
        <Button
            android:id="@+id/auto"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="auto"
            android:text="auto" />
    </LinearLayout>

    <AdapterViewFlipper
        android:id="@+id/flipper"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

StackView 簡介

StackView 同樣繼承自 AdapterViewAnimator,但它的子項是層層重疊堆疊的,通過 showPrevious() 與 showNext() 分別顯示上一張和下一張,效果類似一疊卡片。它的屬性與 AdapterViewFlipper 相近。

實例

這裡用 SimpleAdapter 為 StackView 提供數據:

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;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        stack = (StackView) findViewById(R.id.stack);

        List<Map<String, Object>> listItems = new ArrayList<>();
        for (int i = 0; i < imageIds.length; i++) {
            Map<String, Object> 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);
    }
}
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <StackView
        android:id="@+id/stack"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:loopViews="true" />

    <Button
        android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="next"
        android:text="next" />
</LinearLayout>

簡單清單項布局 simple_item.xml:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/image01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

對比可見:AdapterViewFlipper 適合「一次一屏、可自動輪播」的展示,而 StackView 則更擅長「卡片堆疊、層層翻轉」的視覺效果。兩者都繼承自 AdapterViewAnimator,因此在數據適配方式上高度一致,你可以根據實際的交互需求靈活選用。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved