实现效果:
主要还是ListView的优化:
基本思路:
<1>使用自定义适配器完成操作
<2>将listView进行优化处理
示例代码:
package com.hsj.example.baseadapterdemo05; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import com.hsj.example.baseadapterdemo05.com.hsj.domain.Person; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private ListView listView_persons; private MyAdapter adapter; private List<Person> personList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.listView_persons= (ListView) this.findViewById(R.id.listView_persons); this.personList=this.getData(); this.adapter=new MyAdapter(this.personList); this.listView_persons.setAdapter(this.adapter); } private List<Person> getData() { personList=new ArrayList<>(); for(int i=0;i<24;i++){ Person person=new Person( (R.drawable.png_01+i), "小丽"+i, 20+i ); personList.add(person); } return personList; } private final class MyAdapter extends BaseAdapter{ private List<Person> personList; public MyAdapter(List<Person> personList) { this.personList=personList; } @Override public int getCount() { return personList.size(); } @Override public Object getItem(int position) { return personList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { //原始方式:没有重用条目,造成极大的资源浪费 /*//1.得到当前条目对应的数据对象 Person person=personList.get(position); //2.将布局文件转换成View 对象 View view=View.inflate(MainActivity.this,R.layout.person_item,null); //3.找到当前条目对象中相应的控件对象 ImageView imageView_header= (ImageView) view.findViewById(R.id.imageView_header); TextView textView_name= (TextView) view.findViewById(R.id.textView_name); TextView textView_age= (TextView) view.findViewById(R.id.textView_age); //4.为控件对象设置相应的值 imageView_header.setImageResource(person.getHeader()); textView_name.setText(person.getName()); //此处报异常:android.content.res.Resources$NotFoundException: String resource ID #0x14 // textView_age.setText(person.getAge()); textView_age.setText(String.valueOf(person.getAge()));*/ //1.得到当前条目对应的数据对象 Person person=personList.get(position); /* View view=null; if(convertView==null){ //2.将布局文件转换成View 对象 view=View.inflate(MainActivity.this,R.layout.person_item,null); }else{ view=convertView; } //3.找到当前条目对象中相应的控件对象 ImageView imageView_header= (ImageView) view.findViewById(R.id.imageView_header); TextView textView_name= (TextView) view.findViewById(R.id.textView_name); TextView textView_age= (TextView) view.findViewById(R.id.textView_age); //4.为控件对象设置相应的值 imageView_header.setImageResource(person.getHeader()); textView_name.setText(person.getName()); //此处报异常:android.content.res.Resources$NotFoundException: String resource ID #0x14 // textView_age.setText(person.getAge()); textView_age.setText(String.valueOf(person.getAge()));*/ /* //ListView完成了重用条目的优化操作 if(convertView==null){ //2.将布局文件转换成View 对象 convertView=View.inflate(MainActivity.this,R.layout.person_item,null); } //3.找到当前条目对象中相应的控件对象 ImageView imageView_header= (ImageView) convertView.findViewById(R.id.imageView_header); TextView textView_name= (TextView) convertView.findViewById(R.id.textView_name); TextView textView_age= (TextView) convertView.findViewById(R.id.textView_age); //4.为控件对象设置相应的值 imageView_header.setImageResource(person.getHeader()); textView_name.setText(person.getName()); //此处报异常:android.content.res.Resources$NotFoundException: String resource ID #0x14 // textView_age.setText(person.getAge()); textView_age.setText(String.valueOf(person.getAge()));*/ //ListView完成了重用条目的优化操作,继续解决重复查找控件的问题 ViewHolder viewHolder=null; if(convertView==null){ viewHolder=new ViewHolder(); //2.将布局文件转换成View 对象 convertView=View.inflate(MainActivity.this,R.layout.person_item,null); //3.找到当前条目对象中相应的控件对象 ImageView imageView_header= (ImageView) convertView.findViewById(R.id.imageView_header); TextView textView_name= (TextView) convertView.findViewById(R.id.textView_name); TextView textView_age= (TextView) convertView.findViewById(R.id.textView_age); viewHolder.imageView_header=imageView_header; viewHolder.textView_name=textView_name; viewHolder.textView_age=textView_age; //将当前条目控件封装成ViewHolder对象并和当前条目进行绑定 convertView.setTag(viewHolder); }else{ viewHolder= (ViewHolder) convertView.getTag(); } //4.为控件对象设置相应的值 viewHolder.imageView_header.setImageResource(person.getHeader()); viewHolder.textView_name.setText(person.getName()); viewHolder.textView_age.setText(String.valueOf(person.getAge())); return convertView; } /** * 声明每一个条目中拥有的控件对象 */ class ViewHolder{ ImageView imageView_header; TextView textView_name; TextView textView_age; } } }