編輯:關於 Android 程式設計
用 ListView 做出多列的表格效果,關鍵全在 item 佈局上——把 LinearLayout 的 layout_weight 用起來,讓各列按比例分配寬度,再配合自定義 Adapter 綁定數據即可。本文以商品列表為例完整走一遍。
<LinearLayout
android:id="@+id/relativeLayout1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
xmlns:android="https://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/FirstText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="First"
android:layout_weight="1">
</TextView>
<TextView
android:id="@+id/SecondText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Second"
android:layout_weight="2">
</TextView>
<TextView
android:id="@+id/ThirdText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Third"
android:layout_weight="1">
</TextView>
<TextView
android:id="@+id/FourthText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Fourth"
android:layout_weight="1">
</TextView>
</LinearLayout>
注意每個 TextView 的 layout_width 都是 0dp,這樣 weight 才能均勻生效;若寫成 wrap_content,比例會被內容長度干擾。
public class listviewAdapter extends BaseAdapter {
public ArrayList<HashMap<String, String>> list;
Activity activity;
public listviewAdapter(Activity activity, ArrayList<HashMap<String, String>> list) {
super();
this.activity = activity;
this.list = list;
}
public int getCount() {
return list.size();
}
public Object getItem(int position) {
return list.get(position);
}
public long getItemId(int position) {
return 0;
}
private class ViewHolder {
TextView txtFirst;
TextView txtSecond;
TextView txtThird;
TextView txtFourth;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = activity.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.listview_row, null);
holder = new ViewHolder();
holder.txtFirst = (TextView) convertView.findViewById(R.id.FirstText);
holder.txtSecond = (TextView) convertView.findViewById(R.id.SecondText);
holder.txtThird = (TextView) convertView.findViewById(R.id.ThirdText);
holder.txtFourth = (TextView) convertView.findViewById(R.id.FourthText);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
HashMap<String, String> map = list.get(position);
holder.txtFirst.setText(map.get(FIRST_COLUMN));
holder.txtSecond.setText(map.get(SECOND_COLUMN));
holder.txtThird.setText(map.get(THIRD_COLUMN));
holder.txtFourth.setText(map.get(FOURTH_COLUMN));
return convertView;
}
}
這裡用了 ViewHolder 模式:convertView 為空時才 inflate 並 findViewById,否則直接複用 tag 中的引用。列表稍長一點,這個優化就能明顯減少滑動卡頓。
public class MultiColumnActivity extends Activity {
private ArrayList<HashMap<String, String>> list;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lview = (ListView) findViewById(R.id.listview);
populateList();
listviewAdapter adapter = new listviewAdapter(this, list);
lview.setAdapter(adapter);
}
private void populateList() {
list = new ArrayList<HashMap<String, String>>();
HashMap<String, String> temp = new HashMap<String, String>();
temp.put(FIRST_COLUMN, "Colored Notebooks");
temp.put(SECOND_COLUMN, "By NavNeet");
temp.put(THIRD_COLUMN, "Rs. 200");
temp.put(FOURTH_COLUMN, "Per Unit");
list.add(temp);
HashMap<String, String> temp1 = new HashMap<String, String>();
temp1.put(FIRST_COLUMN, "Diaries");
temp1.put(SECOND_COLUMN, "By Amee Products");
temp1.put(THIRD_COLUMN, "Rs. 400");
temp1.put(FOURTH_COLUMN, "Per Unit");
list.add(temp1);
HashMap<String, String> temp2 = new HashMap<String, String>();
temp2.put(FIRST_COLUMN, "Note Books and Stationery");
temp2.put(SECOND_COLUMN, "By National Products");
temp2.put(THIRD_COLUMN, "Rs. 600");
temp2.put(FOURTH_COLUMN, "Per Unit");
list.add(temp2);
HashMap<String, String> temp3 = new HashMap<String, String>();
temp3.put(FIRST_COLUMN, "Corporate Diaries");
temp3.put(SECOND_COLUMN, "By Devarsh Prakashan");
temp3.put(THIRD_COLUMN, "Rs. 800");
temp3.put(FOURTH_COLUMN, "Per Unit");
list.add(temp3);
HashMap<String, String> temp4 = new HashMap<String, String>();
temp4.put(FIRST_COLUMN, "Writing Pad");
temp4.put(SECOND_COLUMN, "By TechnoTalaktive Pvt. Ltd.");
temp4.put(THIRD_COLUMN, "Rs. 100");
temp4.put(FOURTH_COLUMN, "Per Unit");
list.add(temp4);
}
}
以上就是 ListView 多列佈局的完整實現,希望對你有所幫助。
Android 簡訊驗證碼整合實作(Eclipse 版)
許多 App 都需要透過簡訊驗證碼確認手機號碼,例如註冊、登入或帳號綁定。以 Eclipse 專案為例,整合簡訊驗證碼 SDK 通常包含四個步驟:取得 SDK、導入專案、
SparseArray 取代 HashMap 的實測分析
SparseArray 是 Android 框架獨有的類別,標準 JDK 裡並沒有這個東西。官方說法認為它比 HashMap 更省記憶體,某些情境下效能也更好,原因主要有
原始碼剖析:Volley 的架構、執行流程與快取機制
Volley 是一個 HTTP 庫,能讓 Android App 更輕鬆地完成網路操作,而它最大的賣點是快且有效率。原始碼可以直接從 AOSP 倉庫取得,主要優點包括:自
Android ListView 元件詳解與範例程式碼
Android 列表元件 ListView 列表元件是開發中經常使用的元件,使用時需要為它提供一個 Adapter,由 Adapter 決定顯示樣式與資料來源。 以下看一