您好,登錄后才能下訂單哦!
main4.xml
<?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" > <ListView android:id="@+id/listView4_1_1" android:layout_width="match_parent" android:layout_height="wrap_content" ></ListView> </LinearLayout>
main4_item.xml
<?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="wrap_content" android:orientation="horizontal" android:gravity="center" > <ImageView android:id="@+id/p_w_picpathView4_1_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/textView4_1_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="textView" /> </LinearLayout>
MainActivity4
package com.example.listview; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.R.anim; import android.R.drawable; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; import android.widget.SimpleAdapter; public class MainActivity4 extends Activity{ private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main4); listView=(ListView) findViewById(R.id.listView4_1_1); //一個列表項的內(nèi)容 Map<String, Object> map=new HashMap<String, Object>(); map.put("p_w_picpath", R.drawable.ic_launcher); map.put("name", "張三"); Map<String, Object> map2=new HashMap<String, Object>(); map2.put("p_w_picpath", R.drawable.ic_launcher); map2.put("name", "王五"); List<Map<String, Object>> list=new ArrayList<Map<String,Object>>(); list.add(map); list.add(map2); SimpleAdapter simpleAdapter=new SimpleAdapter(this, list, R.layout.main4_item, new String [] {"p_w_picpath","name"}, new int [] {R.id.p_w_picpathView4_1_1,R.id.textView4_1_1}); listView.setAdapter(simpleAdapter); } }
亦可以自定義Adapter
mian5.xml
<?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" > <ListView android:id="@+id/listView5_1_1" android:layout_width="match_parent" android:layout_height="wrap_content" ></ListView> </LinearLayout>
main5_item.xml
<?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="wrap_content" android:orientation="horizontal" android:gravity="center" > <ImageView android:id="@+id/p_w_picpathView5_1_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/a" /> <TextView android:id="@+id/textView5_1_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="textView" /> </LinearLayout>
MainActivity5
package com.example.listview; import android.app.Activity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; public class MainActivity5 extends Activity { private ListView listView; //定義的數(shù)據(jù) private int [] p_w_picpaths={R.drawable.ic_launcher,R.drawable.ic_launcher}; private String [] names={"xiaobai","xiaoming"}; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main5); listView=(ListView) findViewById(R.id.listView5_1_1); listView.setAdapter(new MyAdapter()); } //自定義適配器 class MyAdapter extends BaseAdapter{ //獲取列表項總數(shù) @Override public int getCount() { // TODO Auto-generated method stub return names.length; } //獲取每一個列表項(根據(jù)下標(biāo)獲取) @Override public Object getItem(int position) { // TODO Auto-generated method stub return names[position]; } //獲取每一個列表項的id @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } // @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub //獲取布局填充器 LayoutInflater layoutInflater=getLayoutInflater(); //獲取main5_item的布局模板 View view=layoutInflater.inflate(R.layout.main5_item, null); //從得到的view查找相應(yīng)view ImageView p_w_picpathView=(ImageView) view.findViewById(R.id.p_w_picpathView5_1_1); TextView textView=(TextView) view.findViewById(R.id.textView5_1_1); p_w_picpathView.setImageResource(p_w_picpaths[position]); textView.setText(names[position]); return view; } } }
這種方式會多次調(diào)用getView()方法
優(yōu)化方法
main5.xml
<?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" > <ListView android:id="@+id/listView5_1_1" android:layout_width="match_parent" android:layout_height="fill_parent" ></ListView> </LinearLayout> <!-- android:layout_height="wrap_content" 會導(dǎo)致在ListView中增加子組件時,多次重復(fù)調(diào)用getView方法 設(shè)置成 android:layout_height="fill_parent" 則每次增加新組建時調(diào)用一次 -->
ListView設(shè)置成 android:layout_height="fill_parent"
//屏幕一次顯示7(假設(shè))條記錄,則調(diào)用7次getView方法,只負(fù)責(zé)在屏幕中顯示的內(nèi)容的創(chuàng)建,其他的在滑動顯示時才創(chuàng)建 //因為ListView的高度設(shè)置為 android:layout_height="wrap_content" //調(diào)用一次getView,繪制一次,增加子組件時會重新繪制(計算高度),所以會多次調(diào)用getView()方法 //增加一個組件,重新繪制ListView, //每繪制一次ListView,每一個子組件都重新繪制(有幾個子組件調(diào)用幾次) //ListView設(shè)置成 android:layout_height="fill_parent" //只輸出0,1,2,3,4,5一次 System.out.println("position:"+position);
將activity中代碼修改為:
System.out.print("position:"+position); System.out.println(" "+convertView); //獲取布局填充器 LayoutInflater layoutInflater=getLayoutInflater(); //獲取main5_item的布局模板,創(chuàng)建布局對象 //如果沒有空閑的convertView,創(chuàng)建convertView,否則重復(fù)利用convertView if(convertView==null){ convertView=layoutInflater.inflate(R.layout.main5_item, null); }
convertView的使用,主要優(yōu)化加載布局問題
增加優(yōu)化二:內(nèi)部類ViewHolder的使用。
主要優(yōu)化getView方法中每次回調(diào)用findviewByID()方法來獲取一次控件的代碼。
新增加內(nèi)部類ViewHolder,用于對控件的實力存儲進(jìn)行緩存。
package com.example.listview; import android.app.Activity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; public class MainActivity5 extends Activity { private ListView listView; //定義的數(shù)據(jù) private int [] p_w_picpaths={R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher}; private String [] names={"xiaobai","xiaoming","xiaobai","xiaoming","xiaobai","xiaoming","xiaobai","xiaoming","xiaobai","xiaoming","xiaobai","xiaoming"}; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main5); listView=(ListView) findViewById(R.id.listView5_1_1); listView.setAdapter(new MyAdapter()); } //自定義適配器 //ListView的優(yōu)化: //1.重復(fù)使用convertView對象 //2.使用viewHolder提高在容器中查找組件的效率 class MyAdapter extends BaseAdapter{ //獲取列表項總數(shù) @Override public int getCount() { // TODO Auto-generated method stub return names.length; } //獲取每一個列表項(根據(jù)下標(biāo)獲取) @Override public Object getItem(int position) { // TODO Auto-generated method stub return names[position]; } //獲取每一個列表項的id @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } // @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub //屏幕一次顯示7(假設(shè))條記錄,則調(diào)用7次getView方法,只負(fù)責(zé)在屏幕中顯示的內(nèi)容的創(chuàng)建,其他的在滑動顯示時才創(chuàng)建 //因為ListView的高度設(shè)置為 android:layout_height="wrap_content" //調(diào)用一次getView,繪制一次,增加子組件時會重新繪制(計算高度),所以會多次調(diào)用getView()方法 //增加一個組件,重新繪制ListView, //每繪制一次ListView,每一個子組件都重新繪制(有幾個子組件調(diào)用幾次) //ListView設(shè)置成 android:layout_height="fill_parent" //只輸出0,1,2,3,4,5一次 System.out.print("position:"+position); System.out.println(" "+convertView); //獲取布局填充器 LayoutInflater layoutInflater=getLayoutInflater(); //獲取main5_item的布局模板,創(chuàng)建布局對象 //如果沒有空閑的convertView,創(chuàng)建convertView,否則重復(fù)利用convertView ViewHolder viewHolder; if(convertView==null){ convertView=layoutInflater.inflate(R.layout.main5_item, null); viewHolder=new ViewHolder(); viewHolder.p_w_picpathView=(ImageView) convertView.findViewById(R.id.p_w_picpathView5_1_1); viewHolder.textView=(TextView) convertView.findViewById(R.id.textView5_1_1); convertView.setTag(viewHolder); }else { viewHolder=(ViewHolder) convertView.getTag(); } //View convertView=layoutInflater.inflate(R.layout.main5_item, null); //從得到的view查找相應(yīng)view /*ImageView p_w_picpathView=(ImageView) convertView.findViewById(R.id.p_w_picpathView5_1_1); TextView textView=(TextView) convertView.findViewById(R.id.textView5_1_1); p_w_picpathView.setImageResource(p_w_picpaths[position]); textView.setText(names[position]);*/ viewHolder.p_w_picpathView.setImageResource(p_w_picpaths[position]); viewHolder.textView.setText(names[position]); return convertView; } } class ViewHolder{ ImageView p_w_picpathView; TextView textView; } }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。