码迷,mamicode.com
首页 > 移动开发 > 详细

Android之ListView优化

时间:2016-01-30 02:25:04      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:

 1 import java.util.List;
 2 
 3 import com.example.contentprovider.R;
 4 import com.example.entity.ImagePath;
 5 import com.example.util.Util;
 6 
 7 import android.content.Context;
 8 import android.view.LayoutInflater;
 9 import android.view.View;
10 import android.view.ViewGroup;
11 import android.widget.BaseAdapter;
12 import android.widget.ImageView;
13 import android.widget.TextView;
14 
15 public class MyBaseAdapter extends BaseAdapter {
16     private List<ImagePath> imagePaths;
17     Context context;
18     
19     public MyBaseAdapter(List<ImagePath> list, Context context) {
20         super();
21         this.imagePaths = list;
22         this.context = context;
23     }
24 
25     @Override
26     public int getCount() {
27         // TODO Auto-generated method stub
28         return imagePaths.size();
29     }
30 
31     @Override
32     public Object getItem(int arg0) {
33         // TODO Auto-generated method stub
34         return imagePaths.get(arg0);
35     }
36 
37     @Override
38     public long getItemId(int arg0) {
39         // TODO Auto-generated method stub
40         return arg0;
41     }
42 
43     @Override
44     public View getView(int arg0, View arg1, ViewGroup arg2) {
45         MyView myView = null;
46         View view ;
47         //arg1 如果为null则执行该代码;该if判断的作用是优化List:如果arg1为null的时候获取布局该布局会缓存在arg1中,创建myView对象
48         //将myView对象存放在view中!当arg1不为空时取出view中的myView对象和缓存中的view
49         if(arg1 == null){
50             //实例化MyView对象
51             myView = new MyView();
52             //获取LayoutInflater对象
53             LayoutInflater inflater = LayoutInflater.from(context);
54             //通过inflater获取ListView中的item的布局
55             view = inflater.inflate(R.layout.mybaseadapter,null);
56             //获取item布局的组件
57             myView.imageView = (ImageView) view.findViewById(R.id.id_image);
58             myView.textName = (TextView) view.findViewById(R.id.id_textName);
59             myView.textNum = (TextView) view.findViewById(R.id.id_textNum);
60             //将myView对象存进view中
61             view.setTag(myView);
62         }else{
63             //如果arg1不为null,将arg1赋给view
64             view = arg1;
65             //将存放在view中的MyView对象取出来
66             MyView myViewTag = (MyView) view.getTag();
67         }
68         myView.textName.setText(imagePaths.get(arg0).getPakagePath());
69         myView.textNum.setText("共"+imagePaths.get(arg0).getListPth().size()+"张");
70         myView.imageView.setImageBitmap(Util.getBitmapByPath(imagePaths.get(arg0).getListPth().get(0),86,86));
71         return view;
72     }
73 
74 }
75     class MyView{
76         TextView textName;
77         TextView textNum;
78         ImageView imageView;
79     }

才接触android,在慢慢的自学!后续会更新

Android之ListView优化

标签:

原文地址:http://www.cnblogs.com/shiguotao-com/p/5170298.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!