main4.xml
專業(yè)成都網(wǎng)站建設(shè)公司,做排名好的好網(wǎng)站,排在同行前面,為您帶來客戶和效益!創(chuàng)新互聯(lián)為您提供成都網(wǎng)站建設(shè),五站合一網(wǎng)站設(shè)計(jì)制作,服務(wù)好的網(wǎng)站設(shè)計(jì)公司,成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、成都外貿(mào)網(wǎng)站建設(shè)公司負(fù)責(zé)任的成都網(wǎng)站制作公司!
main4_item.xml
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); //一個(gè)列表項(xiàng)的內(nèi)容 Mapmap=new HashMap (); map.put("p_w_picpath", R.drawable.ic_launcher); map.put("name", "張三"); Map map2=new HashMap (); map2.put("p_w_picpath", R.drawable.ic_launcher); map2.put("name", "王五"); List
亦可以自定義Adapter
mian5.xml
main5_item.xml
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{ //獲取列表項(xiàng)總數(shù) @Override public int getCount() { // TODO Auto-generated method stub return names.length; } //獲取每一個(gè)列表項(xiàng)(根據(jù)下標(biāo)獲取) @Override public Object getItem(int position) { // TODO Auto-generated method stub return names[position]; } //獲取每一個(gè)列表項(xiàng)的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
ListView設(shè)置成 android:layout_height="fill_parent"
//屏幕一次顯示7(假設(shè))條記錄,則調(diào)用7次getView方法,只負(fù)責(zé)在屏幕中顯示的內(nèi)容的創(chuàng)建,其他的在滑動顯示時(shí)才創(chuàng)建 //因?yàn)長istView的高度設(shè)置為 android:layout_height="wrap_content" //調(diào)用一次getView,繪制一次,增加子組件時(shí)會重新繪制(計(jì)算高度),所以會多次調(diào)用getView()方法 //增加一個(gè)組件,重新繪制ListView, //每繪制一次ListView,每一個(gè)子組件都重新繪制(有幾個(gè)子組件調(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,用于對控件的實(shí)力存儲進(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{ //獲取列表項(xiàng)總數(shù) @Override public int getCount() { // TODO Auto-generated method stub return names.length; } //獲取每一個(gè)列表項(xiàng)(根據(jù)下標(biāo)獲取) @Override public Object getItem(int position) { // TODO Auto-generated method stub return names[position]; } //獲取每一個(gè)列表項(xiàng)的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)建,其他的在滑動顯示時(shí)才創(chuàng)建 //因?yàn)長istView的高度設(shè)置為 android:layout_height="wrap_content" //調(diào)用一次getView,繪制一次,增加子組件時(shí)會重新繪制(計(jì)算高度),所以會多次調(diào)用getView()方法 //增加一個(gè)組件,重新繪制ListView, //每繪制一次ListView,每一個(gè)子組件都重新繪制(有幾個(gè)子組件調(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; } }