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

下面用一個 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 同樣繼承自 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,因此在數據適配方式上高度一致,你可以根據實際的交互需求靈活選用。
深入解析 RecyclerView:設計理念、與 ListView 的差異與 API 現狀
先前團隊希望在既有 Android 專案中為 ListView 的項目加上動畫效果,於是決定引入當時最新的 RecyclerView。本文把這段時間研究到的內容做個整理—
Android 消息機制深度解析:Handler、Looper 與 MessageQueue 原理
Android 的消息處理相關類別包括:Looper、Handler、MessageQueue、Message、ThreadLocal、ThreadLocal.Value
手機預裝軟體怎麼移除?用電腦工具一次清乾淨
新買的手機裡總是塞了一堆用不到的預裝程式,佔空間又關不掉,看著就煩。這些軟體能不能刪?答案是能,但需要透過電腦端工具來處理。以下是完整流程。 1. 先搜尋並下載安裝「36
Android 自製輕量彈幕效果的實作分享
直播與影片播放頁越來越常看到彈幕功能。若不想引入過重的第三方庫,也可以自行實作一個輕量級彈幕 View,讓文字從右向左移動,並支援多曲目與隨機軌道。 核心設計 彈幕本