標籤:android android教程 java baseadapter 適配器
擴充BaseAdapter可以對各清單項目進行最大限度的定製,具體描述請看以下代碼:
baseadapter_list布局檔案:
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/lv_baseadapter" android:layout_width="fill_parent" android:layout_height="wrap_content" > </ListView></LinearLayout></span>
baseadapter布局檔案:
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/ll_top" android:layout_height="wrap_content" android:layout_width="fill_parent" android:orientation="horizontal"> <ImageView android:id="@+id/iv_image" android:layout_height="wrap_content" android:layout_width="wrap_content" android:scaleType="fitXY" android:src="@drawable/a5i" android:layout_gravity="center"/> <LinearLayout android:id="@+id/ll_right" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginTop="4dp"> <TextView android:id="@+id/tv_title" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="20sp" android:text="AS"/> <TextView android:id="@+id/tv_des" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="13sp" android:text="AS" /> </LinearLayout></LinearLayout></RelativeLayout></span>
BaseAdapterTest主檔案:
<span style="font-size:18px;">public class BaseAdapterTest extends Activity {int[] images = new int[] { R.drawable.a5i, R.drawable.a5j, R.drawable.a5k };String[] titles = new String[] { "電話", "QQ", "連絡人" };String[] des = new String[] { "當前無電話記錄", "當前無QQ聊天記錄", "最近無連絡人" };private ListView lv_baseadapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.baseadapter_list);initView();setDate();}private void initView(){//註冊控制項lv_baseadapter=(ListView)findViewById(R.id.lv_baseadapter);//註冊單擊事件lv_baseadapter.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {Toast.makeText(BaseAdapterTest.this, "點擊了第"+position+"項", Toast.LENGTH_SHORT).show();}});}private void setDate(){//綁定適配器lv_baseadapter.setAdapter(baseAdapter);}BaseAdapter baseAdapter=new BaseAdapter() {/* *返回第postion處的清單項目組件 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup) */@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder viewHolder=new ViewHolder();if(null==convertView){LayoutInflater layoutInflater=LayoutInflater.from(BaseAdapterTest.this);convertView=layoutInflater.inflate(R.layout.baseadapter, null);viewHolder.iv_image=(ImageView)convertView.findViewById(R.id.iv_image);viewHolder.tv_title=(TextView)convertView.findViewById(R.id.tv_title);viewHolder.tv_desc=(TextView)convertView.findViewById(R.id.tv_des);/* * 將當前的View和ViewHolder綁定 */convertView.setTag(viewHolder);}else{/* * 為已經取得綁定的View調用getTag()方法,當前的view就不必重新繪製, * 減少記憶體資源。 */viewHolder=(ViewHolder)convertView.getTag();}viewHolder.iv_image.setImageResource(images[position]);viewHolder.tv_title.setText(titles[position]);viewHolder.tv_desc.setText(des[position]);return convertView;}/* * 返回第postion處的清單項目的ID * @see android.widget.Adapter#getItemId(int) */@Overridepublic long getItemId(int position) {return position;}/* * 返回第postion處的清單項目的內容。該方法在事件監聽時會調用 * @see android.widget.Adapter#getItem(int) */@Overridepublic Object getItem(int position) {return position;}/* * 返回該Adapter將會包含多少個清單項目 * @see android.widget.Adapter#getCount() */@Overridepublic int getCount() {return titles.length;}}; private class ViewHolder{ImageView iv_image;TextView tv_title;TextView tv_desc;}}</span>
轉載請註明出處:http://blog.csdn.net/hai_qing_xu_kong/article/details/42400261 情緒控_
一起學android之BaseAdapter使用(14)