編輯:關於 Android 開發
上一篇把 TabPageIndicator 跑起來了,這一篇加上 Fragment 內容頁,做出滑動切換 Tab 的效果。

package com.example.csdn; import com.viewpagerindicator.TabPageIndicator; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class MainActivity extends FragmentActivity { private TabPageIndicator tpi_tab; private ViewPager vp_content; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tpi_tab = (TabPageIndicator) findViewById(R.id.vpi_tab); vp_content = (ViewPager) findViewById(R.id.vp_content); FragmentManager fm = getSupportFragmentManager(); TabAdapter adapter = new TabAdapter(fm); // ViewPager設置適配器 vp_content.setAdapter(adapter); // 指示器綁定ViewPager tpi_tab.setViewPager(vp_content, 0); } } /** * 標簽部分適配器 * * @author taoshihan * */ class TabAdapter extends FragmentPagerAdapter { public static final String[] TITLES = new String[] { "業界", "移動", "研發","PHP","Android","Java","前端" }; public TabAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int arg0) { return new ContentFragment(arg0); } @Override public int getCount() { return TITLES.length; } @Override public CharSequence getPageTitle(int position) { // TODO Auto-generated method stub return TITLES[position % TITLES.length]; } } /** * 內容部分 * @author taoshihan * */ class ContentFragment extends Fragment{ private int index; public ContentFragment(int index) { this.index=index; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=View.inflate(getActivity(), R.layout.main_content, null); TextView tv_content=(TextView) view.findViewById(R.id.tv_content); String[] titles=TabAdapter.TITLES; tv_content.setText(titles[index]); return view; } }
TabPageIndicator 需要對應的主題才能正確渲染,所以在 AndroidManifest.xml 裡幫 Activity 掛上 Theme.PageIndicatorDefaults:
<activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/Theme.PageIndicatorDefaults"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
少了這行主題,indicator 會以預設樣式顯示,跟設計稿落差很大——這是使用這個庫最常卡住的一步。
一、綁定的順序不能反。必須先 setAdapter 再呼叫 tpi_tab.setViewPager(vp_content, 0)。第二個參數 0 是初始顯示的頁數索引,改成 2 就會直接停在第三個 Tab。
二、getPageTitle 決定 Tab 上的文字。這裡用 TITLES[position % TITLES.length] 取模,是為了防止陣列長度與頁數不一致時越界。實務上直接用 TITLES[position] 就好,取模反而會掩蓋資料設定錯誤。
三、FragmentPagerAdapter 與 FragmentStateAdapter 的差別。這裡用的是 FragmentPagerAdapter,它會把 Fragment 保留在記憶體中(最多 offscreen limit 個),適合頁數少的 Tab 場景。如果頁數多或每頁都很重,該換成 FragmentStatePagerAdapter,它會銷毀看不見的頁面以節省記憶體。
ContentFragment 把 index 透過建構子傳入,這在 Fragment 中是不推薦的做法。原因是當系統記憶體不足時,Fragment 會被銷毀重建,重建時會呼叫無參數的建構子,你傳進去的 index 就丟了,畫面會顯示錯誤的內容或崩潰。
正確做法是把參數放進 Bundle:
public static ContentFragment newInstance(int index) {
ContentFragment f = new ContentFragment();
Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
index = getArguments().getInt("index", 0);
}
因為 setArguments 存的 Bundle 會由系統一併保存與還原,重建後參數依然在。
ViewPagerIndicator 最後一次更新停在 support-v4 的年代,已經很久沒有維護。新專案其實不需要它——Google 自家的 TabLayout 搭配 ViewPager(或 ViewPager2)已經能做出完全相同的滑動式 Tab 效果,而且直接支援 Material Design 的 indicator 樣式:
tabLayout.setupWithViewPager(viewPager);
一行就搞定綁定,還免費得到 Tab 與頁面標題的自動對應。加上 ViewPager2 底層改用 RecyclerView,支援垂直滑動與更好的記憶體管理,沒有理由再引入一個停止維護的第三方庫。這篇筆記的價值在於理解「indicator 需要綁定 ViewPager」這個機制,換到 TabLayout 上是完全一樣的思路。
Android實用技術(3)——Service入門:定義、註冊與生命週期(Ⅰ)
前面已經大致了解了AsyncTask的多執行緒編程,這次來聊聊Android四大元件之一的Service。要真正精通Service,需要學的東西不少;這一篇的目標是讓新手
Android Transitions 框架解析:一次處理多個 View 的動畫
Transitions 框架的定位 傳統 Animator 多半針對單一物件做動畫,而 Transitions 框架更擅長處理 ViewGroup 層級的狀態變化。開發者
Thread 與 Handler 搭配實戰:徹底搞懂 Android 的消息循環機制
Android 為開發者準備了 Handler 與 Looper 這組搭檔,專門用來解決「不同線程之間怎麼溝通」這個老問題。最典型的場景是:子線程在背景下載圖片,下載完成
ExpandableListView 實戰:自訂群組樣式與四個常見眉角
前言:這段時間在認真學 Android 並實際做專案,累積了一些對 ExpandableListView 的心得。若文章中有理解錯誤或表達不清的地方,歡迎在留言中指正。