一起學android之BaseAdapter使用(14)
發表時間:2020-10-19
發布人:葵宇科技
浏覽次數:50
擴大BaseAdapter可(kě)以對各列表項進行最大年夜限度的定制,具體描述請看以下(xià)代碼:
baseadapter_list構造文(wén)件:
<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構造文(wén)件:
<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=http://www.sjsjw.com/100/000275MYM020387/"@drawable/a5i" android:layout_gravity="center"/>
BaseAdapterTest主文(wén)件:
<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; @Override protected 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() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(BaseAdapterTest.this, "點擊了第"+position+"項", Toast.LENGTH_SHORT).show(); } }); } private void setDate(){ //綁定适配器(qì) lv_baseadapter.setAdapter(baseAdapter); } BaseAdapter baseAdapter=new BaseAdapter() { /* *返回第postion處的列表項組件 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup) */ @Override public 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和(hé)ViewHolder綁定 */ convertView.setTag(viewHolder); }else{ /* * 為已經取得綁定的View調用getTag()辦法,當前的view就不必從新繪制, * 削減内存資(zī)本。 */ 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) */ @Override public long getItemId(int position) { return position; } /* * 返回第postion處的列表項的内容。該辦法在事宜監聽時會調用 * @see android.widget.Adapter#getItem(int) */ @Override public Object getItem(int position) { return position; } /* * 返回該Adapter将會包含若幹個(gè)列表項 * @see android.widget.Adapter#getCount() */ @Override public int getCount() { return titles.length; } }; private class ViewHolder{ ImageView iv_image; TextView tv_title; TextView tv_desc; } }</span>
[img]http://img.blog.csdn.net/20150104210322832?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGFpX3FpbmdfeHVfa29uZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
轉載請注明出處:http://blog.csdn.net/hai_qing_xu_kong/article/details/42400261 情感控_