BaseAdapter 可以讓使用者自己定義許多種 View ,像是 Spinner, ListView, GridView等,而且可以重用View來節省資源:使用setTag將View存起來,之後就可以利用getTag將數據取出來
透過繼承 BaseAdapter,我們可以重新覆寫4個方法:
public int getCount();
取得 ListView 列表 Item 的數量。通常數量就是從建構子傳入的陣列或是集合大小。
public Object getItem(int position);
取得 ListView 列表於 position 位置上的 Item。position 通常是資料在陣列或是集合上的位置。
public long getItemId(int position);
取得 ListView 列表於 position 位置上的 Item 的 ID,一般用 position 的值即可。
public View getView(int position, View view, ViewGroup viewGroup);
通常會設定與回傳View 作為顯示在這個 position 位置的 Item 的 View。
基本上只要處理兩個方法 getCount跟getView即可。
getCount回傳我所宣告的String陣列st1的長度。
在getView之前,要先取得LayoutInflater
再來要先宣告一個類別viewHolder,用來存放list_content.xml內所有元件的內容
接著判斷view是否為空,如果為空就要對他進行初始化:
1,透過LayoutInflater將我們自訂的list_content.xml裝進去
2,new一個viewHolder出來,並將元件內容裝進去
3,透過setTag把viewHolder裝進去
如果view不為空,就利用getTag將數據讀出來
最後設定元件要放置的內容,並return view
完整MainActivity.java程式碼:
執行結果:
參考資料:賽肥膩膩、GiveMePasS's Android惡補筆記
Step1 在activity_main.xml中新增ListView
例如:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" | |
android:padding="16dp"> | |
<ListView | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:id="@+id/lv"/> | |
</LinearLayout> |
Step2 在layout中新增list_content.xml,並排好想要的ListView item佈局
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal" | |
android:paddingTop="16dp" | |
android:paddingBottom="16dp" | |
android:gravity="center_vertical"> | |
<ImageView | |
android:id="@+id/iv" | |
android:layout_width="64dp" | |
android:layout_height="64dp" | |
android:src="@drawable/photos"/> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="vertical"> | |
<TextView | |
android:id="@+id/tv1" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" /> | |
<TextView | |
android:id="@+id/tv2" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" /> | |
</LinearLayout> | |
</LinearLayout> |
Step3 在MainActivity.java 新增一個類別BA並繼承BaseAdapter
透過繼承 BaseAdapter,我們可以重新覆寫4個方法:
public int getCount();
取得 ListView 列表 Item 的數量。通常數量就是從建構子傳入的陣列或是集合大小。
public Object getItem(int position);
取得 ListView 列表於 position 位置上的 Item。position 通常是資料在陣列或是集合上的位置。
public long getItemId(int position);
取得 ListView 列表於 position 位置上的 Item 的 ID,一般用 position 的值即可。
public View getView(int position, View view, ViewGroup viewGroup);
通常會設定與回傳View 作為顯示在這個 position 位置的 Item 的 View。
基本上只要處理兩個方法 getCount跟getView即可。
getCount回傳我所宣告的String陣列st1的長度。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int getCount() { | |
return st1.length; | |
} |
在getView之前,要先取得LayoutInflater
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LayoutInflater loi; | |
public BA(){ | |
loi = (LayoutInflater) MainActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE); | |
} |
再來要先宣告一個類別viewHolder,用來存放list_content.xml內所有元件的內容
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private class viewHolder { | |
ImageView iv; | |
TextView tv1,tv2; | |
} |
接著判斷view是否為空,如果為空就要對他進行初始化:
1,透過LayoutInflater將我們自訂的list_content.xml裝進去
2,new一個viewHolder出來,並將元件內容裝進去
3,透過setTag把viewHolder裝進去
如果view不為空,就利用getTag將數據讀出來
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (view == null) { | |
//將自訂的list_contnet灌進view裡 | |
view = loi.inflate(R.layout.list_content, null); | |
//new一個VH,並將iv,tv1,tv2裝進去 | |
vh = new viewHolder(); | |
vh.iv = (ImageView) view.findViewById(R.id.iv); | |
vh.tv1 = (TextView) view.findViewById(R.id.tv1); | |
vh.tv2 = (TextView) view.findViewById(R.id.tv2); | |
//再透過view.setTag把vh裝進去 | |
view.setTag(vh); | |
}else { | |
vh = (viewHolder) view.getTag(); | |
} |
最後設定元件要放置的內容,並return view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vh.iv.setImageResource(R.drawable.photos); | |
vh.tv1.setText(st1[position]); | |
vh.tv2.setText("xxxxxxxx"); | |
return view; |
Step4 在MainActivity.java onCreate中使用BA這個 Adapter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lv = (ListView) findViewById(R.id.lv); | |
lv.setAdapter(new BA()); |
完整MainActivity.java程式碼:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity { | |
ListView lv; | |
String st1[] = {"Name01","Name02","Name03","Name04","Name05","Name06"}; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
lv = (ListView) findViewById(R.id.lv); | |
lv.setAdapter(new BA()); | |
} | |
private class BA extends BaseAdapter { | |
LayoutInflater loi; | |
public BA(){ | |
loi = (LayoutInflater) MainActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE); | |
} | |
@Override | |
public int getCount() { | |
return st1.length; | |
} | |
@Override | |
public Object getItem(int i) { | |
return null; | |
} | |
@Override | |
public long getItemId(int i) { | |
return 0; | |
} | |
@Override | |
public View getView(int position, View view, ViewGroup viewGroup) { | |
viewHolder vh; | |
//判斷view是否為空,為空才設置view | |
if (view == null) { | |
//將自訂的list_contnet灌進view裡 | |
view = loi.inflate(R.layout.list_content, null); | |
//new一個VH,並將iv,tv1,tv2裝進去 | |
vh = new viewHolder(); | |
vh.iv = (ImageView) view.findViewById(R.id.iv); | |
vh.tv1 = (TextView) view.findViewById(R.id.tv1); | |
vh.tv2 = (TextView) view.findViewById(R.id.tv2); | |
//再透過view.setTag把vh裝進去 | |
view.setTag(vh); | |
}else { | |
vh = (viewHolder) view.getTag(); | |
} | |
//設定iv,tv1,tv2要放置的內容 | |
vh.iv.setImageResource(R.drawable.photos); | |
vh.tv1.setText(st1[position]); | |
vh.tv2.setText("xxxxxxxx"); | |
return view; | |
} | |
} | |
//宣告類別 viewHolder,記住list_content.xml中的item | |
private class viewHolder { | |
ImageView iv; | |
TextView tv1,tv2; | |
} | |
} |
執行結果:
參考資料:賽肥膩膩、GiveMePasS's Android惡補筆記
留言
張貼留言