android的ListView图文混搭
main4.xml
专业成都网站建设公司,做排名好的好网站,排在同行前面,为您带来客户和效益!创新互联为您提供成都网站建设,五站合一网站设计制作,服务好的网站设计公司,成都网站设计、做网站、成都外贸网站建设公司负责任的成都网站制作公司!
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); //一个列表项的内容 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; //定义的数据 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{ //获取列表项总数 @Override public int getCount() { // TODO Auto-generated method stub return names.length; } //获取每一个列表项(根据下标获取) @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查找相应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; } } }
这种方式会多次调用getView()方法
优化方法
main5.xml
ListView设置成 android:layout_height="fill_parent"
//屏幕一次显示7(假设)条记录,则调用7次getView方法,只负责在屏幕中显示的内容的创建,其他的在滑动显示时才创建 //因为ListView的高度设置为 android:layout_height="wrap_content" //调用一次getView,绘制一次,增加子组件时会重新绘制(计算高度),所以会多次调用getView()方法 //增加一个组件,重新绘制ListView, //每绘制一次ListView,每一个子组件都重新绘制(有几个子组件调用几次) //ListView设置成 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的布局模板,创建布局对象 //如果没有空闲的convertView,创建convertView,否则重复利用convertView if(convertView==null){ convertView=layoutInflater.inflate(R.layout.main5_item, null); }
convertView的使用,主要优化加载布局问题
增加优化二:内部类ViewHolder的使用。
主要优化getView方法中每次回调用findviewByID()方法来获取一次控件的代码。
新增加内部类ViewHolder,用于对控件的实力存储进行缓存。
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; //定义的数据 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的优化: //1.重复使用convertView对象 //2.使用viewHolder提高在容器中查找组件的效率 class MyAdapter extends BaseAdapter{ //获取列表项总数 @Override public int getCount() { // TODO Auto-generated method stub return names.length; } //获取每一个列表项(根据下标获取) @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(假设)条记录,则调用7次getView方法,只负责在屏幕中显示的内容的创建,其他的在滑动显示时才创建 //因为ListView的高度设置为 android:layout_height="wrap_content" //调用一次getView,绘制一次,增加子组件时会重新绘制(计算高度),所以会多次调用getView()方法 //增加一个组件,重新绘制ListView, //每绘制一次ListView,每一个子组件都重新绘制(有几个子组件调用几次) //ListView设置成 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的布局模板,创建布局对象 //如果没有空闲的convertView,创建convertView,否则重复利用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查找相应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; } }
本文名称:android的ListView图文混搭
URL链接:http://pwwzsj.com/article/pgcpei.html