編輯:關於 Android 程式設計
要做的是新浪微博那種 Tab 效果:手勢滑動或點擊標題都能切換頁面,而下方那條白色指示條會平滑地滑到對應的頁卡下方。

ViewPager 是 Google 在 support 套件中提供的類別,專門用來實作螢幕間的切換。它位在 android-support-v4.jar 裡,請把它放進專案的 libs 目錄(也可以自行下載最新版本)。

上方放三個 TextView 作為頁卡標題(用 layout_weight 平均分配),中間放一張 ImageView 當指示條,最下方是 ViewPager:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="100.0dip"
android:background="#FFFFFF">
<TextView
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:gravity="center"
android:text="頁卡1"
android:textColor="#000000"
android:textSize="22.0dip" />
<TextView
android:id="@+id/text2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:gravity="center"
android:text="頁卡2"
android:textColor="#000000"
android:textSize="22.0dip" />
<TextView
android:id="@+id/text3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:gravity="center"
android:text="頁卡3"
android:textColor="#000000"
android:textSize="22.0dip" />
</LinearLayout>
<ImageView
android:id="@+id/cursor"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="matrix"
android:src="@drawable/a" />
<android.support.v4.view.ViewPager
android:id="@+id/vPager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1.0"
android:background="#000000"
android:flipInterval="30"
android:persistentDrawingCache="animation" />
</LinearLayout>
這裡有個關鍵細節:指示條的 scaleType 必須設為 matrix,因為後續我們要用 Matrix 平移它,其他 scaleType 會讓位移失效。
分別建立 lay1、lay2、lay3 三個版面,這裡只放背景色以便區分:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#158684">
</LinearLayout>
private ViewPager mPager; // 頁卡內容 private List<View> listViews; // Tab 頁面列表 private ImageView cursor; // 動畫圖片 private TextView t1, t2, t3; // 頁卡頭標 private int offset = 0; // 動畫圖片偏移量 private int currIndex = 0; // 當前頁卡編號 private int bmpW; // 動畫圖片寬度
/** 初始化頭標 */
private void InitTextView() {
t1 = (TextView) findViewById(R.id.text1);
t2 = (TextView) findViewById(R.id.text2);
t3 = (TextView) findViewById(R.id.text3);
t1.setOnClickListener(new MyOnClickListener(0));
t2.setOnClickListener(new MyOnClickListener(1));
t3.setOnClickListener(new MyOnClickListener(2));
}
/** 頭標點擊監聽 */
public class MyOnClickListener implements View.OnClickListener {
private int index = 0;
public MyOnClickListener(int i) {
index = i;
}
@Override
public void onClick(View v) {
mPager.setCurrentItem(index);
}
}
點第幾個就切換到第幾頁,ViewPager 的 setCurrentItem 會一併觸發後面的頁面監聽。
/** 初始化 ViewPager */
private void InitViewPager() {
mPager = (ViewPager) findViewById(R.id.vPager);
listViews = new ArrayList<View>();
LayoutInflater mInflater = getLayoutInflater();
listViews.add(mInflater.inflate(R.layout.lay1, null));
listViews.add(mInflater.inflate(R.layout.lay2, null));
listViews.add(mInflater.inflate(R.layout.lay3, null));
mPager.setAdapter(new MyPagerAdapter(listViews));
mPager.setCurrentItem(0);
mPager.setOnPageChangeListener(new MyOnPageChangeListener());
}
/** ViewPager 適配器 */
public class MyPagerAdapter extends PagerAdapter {
public List<View> mListViews;
public MyPagerAdapter(List<View> mListViews) {
this.mListViews = mListViews;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView(mListViews.get(arg1));
}
@Override
public void finishUpdate(View arg0) { }
@Override
public int getCount() {
return mListViews.size();
}
@Override
public Object instantiateItem(View arg0, int arg1) {
((ViewPager) arg0).addView(mListViews.get(arg1), 0);
return mListViews.get(arg1);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) { }
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View arg0) { }
}
/** 初始化動畫 */
private void InitImageView() {
cursor = (ImageView) findViewById(R.id.cursor);
bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.a).getWidth();
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenW = dm.widthPixels;
offset = (screenW / 3 - bmpW) / 2; // 計算置中偏移量
Matrix matrix = new Matrix();
matrix.postTranslate(offset, 0);
cursor.setImageMatrix(matrix); // 設定動畫初始位置
}
這裡依螢幕解析度與圖片寬度算出讓指示條置中所需的偏移量。

/** 頁卡切換監聽 */
public class MyOnPageChangeListener implements OnPageChangeListener {
int one = offset * 2 + bmpW; // 頁卡1 → 頁卡2 的偏移量
int two = one * 2; // 頁卡1 → 頁卡3 的偏移量
@Override
public void onPageSelected(int arg0) {
Animation animation = null;
switch (arg0) {
case 0:
if (currIndex == 1) {
animation = new TranslateAnimation(one, 0, 0, 0);
} else if (currIndex == 2) {
animation = new TranslateAnimation(two, 0, 0, 0);
}
break;
case 1:
if (currIndex == 0) {
animation = new TranslateAnimation(offset, one, 0, 0);
} else if (currIndex == 2) {
animation = new TranslateAnimation(two, one, 0, 0);
}
break;
case 2:
if (currIndex == 0) {
animation = new TranslateAnimation(offset, two, 0, 0);
} else if (currIndex == 1) {
animation = new TranslateAnimation(one, two, 0, 0);
}
break;
}
currIndex = arg0;
animation.setFillAfter(true); // 動畫結束後停在終點
animation.setDuration(300);
cursor.startAnimation(animation);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) { }
@Override
public void onPageScrollStateChanged(int arg0) { }
}
這裡刻意用「起點 → 終點」的形式建立 TranslateAnimation,而不是每次都從頭移動,這樣指示條無論往前或往後切換都能正確對位。setFillAfter(true) 則是為了讓它停在最後的位置而不跳回原點。

用 HttpURLConnection 在 Android 客戶端與伺服器之間傳遞物件
一般情況下,客戶端與伺服器的資料互動多使用 JSON 或 XML。相比 XML,JSON 更輕量、更省流量。但無論用哪一種,我們通常都得先把資料封裝成字串再傳輸。那麼,有
Volley 源碼深度分析:從請求創建到響應分發
我曾寫過一篇對 Volley 源碼分析得毫無頭緒的文章,回頭看時不甚滿意,於是刪掉重寫,便有了今天這篇。 Volley 的使用步驟 1. 創建一個 RequestQueu
ViewPager 基本用法詳解:PagerAdapter、標題欄與 TabHost 效果實現
概述 ViewPager 是 Android 3.0 之後引入的 UI 控件(視圖滑動切換工具)。它的能力很單純:透過手勢左右滑動完成 View 的切換,通常用來做 Ap
Android 單位換算與用程式碼動態修改控制項布局
XML 布局並非萬能。當我們需要依據執行期條件決定控制項的尺寸或位置時,就必須在程式碼中動態調整。要做到這件事,先得把 Android 的各種單位搞清楚。 Android