編輯:關於 Android 程式設計
前陣子專案有個需求:同一個頁面要依不同按鈕載入不同的版面。原本想用 TabHost,但美工給的設計稿完全用不上頁籤樣式,所以改走「動態載入」這條路。整理成 DEMO 記錄如下。

中間白色區域是一個空的 LinearLayout,用來承載動態載入的版面。我畫圖時習慣用不同顏色標示不同區塊,方便對照。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button android:text="加載ListView"
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button android:text="加載另外一個頁面"
android:id="@+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
</LinearLayout>
<!-- 這塊白色區域用來動態載入傳進來的版面 -->
<LinearLayout android:id="@+id/LinearLayout01"
android:background="#FFFFFF"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></LinearLayout>
</LinearLayout>
先準備一個用來放 ListView 的版面 listview.xml:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="https://schemas.android.com/apk/res/android">
<ListView android:id="@+id/ListView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
ListView 每一列的樣式 list_view_row.xml:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="https://schemas.android.com/apk/res/android">
<TextView android:text="@+id/TextView01"
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</LinearLayout>
另一個用來驗證「確實換了版面」的簡單頁面 hello.xml:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout android:id="@+id/hellolayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="https://schemas.android.com/apk/res/android">
<TextView android:text="HELLO"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</LinearLayout>
package com.terry;
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class listAdapter extends BaseAdapter {
ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
private LayoutInflater inflater;
public listAdapter(Context contex) {
inflater = LayoutInflater.from(contex);
for (int i = 0; i < 10; i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("name", "例子" + i);
list.add(map);
}
}
@Override public int getCount() { return list.size(); }
@Override public Object getItem(int position) { return list.get(position); }
@Override public long getItemId(int position) { return position; }
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final viewHolder myHolder;
if (convertView == null) {
myHolder = new viewHolder();
convertView = inflater.inflate(R.layout.list_view_row, null);
myHolder.tv = (TextView) convertView.findViewById(R.id.TextView01);
convertView.setTag(myHolder);
} else {
myHolder = (viewHolder) convertView.getTag();
}
myHolder.tv.setText(list.get(position).get("name").toString());
return convertView;
}
static class viewHolder {
TextView tv;
}
}
這裡用了 ViewHolder 模式(setTag/getTag)避免重複 findViewById,是列表效能的基本功。
專案結構如下:


點擊第一個按鈕:

點擊第二個按鈕:
package com.terry;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
public class dynaActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final LayoutInflater inflater = LayoutInflater.from(this);
Button btn = (Button) findViewById(R.id.Button01);
Button btn2 = (Button) findViewById(R.id.Button02);
final LinearLayout lin = (LinearLayout) findViewById(R.id.LinearLayout01);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout layout = (LinearLayout) inflater.inflate(
R.layout.listview, null).findViewById(R.id.layout);
ListView lv = (ListView) layout.getChildAt(0);
lv.setAdapter(new listAdapter(dynaActivity.this));
lin.removeAllViews(); // 先清掉舊的
lin.addView(layout); // 再放新的
}
});
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout layout = (LinearLayout) inflater.inflate(
R.layout.hello, null).findViewById(R.id.hellolayout);
TextView tv = (TextView) layout.getChildAt(0);
tv.setTextColor(Color.RED);
lin.removeAllViews();
lin.addView(layout);
}
});
}
}
原理就是:用 LayoutInflater 把目標版面讀成 View 物件,找到裡面的子 View 做操作,最後掛進 setContentView 裡預留的容器中。每次動態載入前一定要呼叫 removeAllViews(),否則舊 View 會疊在新 View 底下,造成「換頁後內容重疊」的經典 bug。
SparseArray 裡,切換時只做 removeAllViews + addView,省下解析 XML 的時間。OnTouchListener、動畫或 Timer,換頁時要一併停止,否則容易記憶體洩漏。Animation 或 Transition 做淡入淡出、平移,觀賞性提升明顯。FragmentTransaction.replace())會比純 View 切換好維護。純動態載入適合「內容單純、只是換個樣式」的情境。以上就是不用 TabHost 也能在同一頁切換版面的做法,重點是 LayoutInflater 加 removeAllViews() 這兩個動作。当然还有别的写法,多试几种或许会有不一样的心得。
ConstraintLayout 入門:用約束打造扁平化布局
序 在 Google I/O 大會上,除了 Android Studio 2.2 預覽版,還帶來了一個基於約束的布局庫。 簡單說,它是 RelativeLayout 的進
高仿今日頭條新聞APP:ZYFNewsCTZF 完整實作教學
本文記錄了一個名為 ZYFNewsCTZF 的新聞閱讀類應用的完整開發過程,整體風格與交互向「今日頭條」看齊。文章從界面效果展示入手,逐個環節拆解核心代碼,涵蓋主界面框架
在 Android 中用 AspectJ 落地 AOP:從原理到完整配置步驟
先把原理說清楚 在 Eclipse 時代,AspectJ 有 AJDT 插件現成可用,編譯器與關鍵字提示都已經集成完畢。但到了 Android Studio,官方並沒有提
用 android-wheel 實作省市縣三級聯動選單
今天沒事在群裡聊天,有位朋友提到 Android Wheel 這個控制項,我還以為是 Android 內建的,搜了一下才發現是 GitHub 上的開源專案。 下載地址:h