[Android]ListView学习笔记
基本用法流程
-
创建Adapter并且派生自
BaseAdapter
,实现其必要的接口方法 -
将创建的Adapter分配给ListView对象:
mPhoneBookListView.setAdapter(phoneBookAdapter);
-
针对每一个ItemView,创建Layout,并且在Adapter的
getView
方法中指定。为了优化性能,这里获取的对象要尽量重用如下:@Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; //Get a view if (convertView == null) { convertView = mlayoutInflater.inflate(R.layout.item_phone_book_friend,null); viewHolder = new ViewHolder(); //获取控件 viewHolder.nameTextView = (TextView) convertView.findViewById(R.id.name_text_view); viewHolder.ageTextView = (TextView) convertView.findViewById(R.id.age_text_view); viewHolder.avatarimageView = (ImageView) convertView.findViewById(R.id.avatar_image_view); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } //Binding data viewHolder.nameTextView.setText(mUserInfos.get(position).getUserName()); viewHolder.ageTextView.setText(String.valueOf(mUserInfos.get(position).getAge())); viewHolder.avatarimageView.setImageResource(R.drawable.james22); return convertView; } class ViewHolder { //获取控件 TextView nameTextView; TextView ageTextView; ImageView avatarimageView; }
-
若更新了ListView使用的数据源,则通过Adapter的
notifyDataSetChanged()
方法来更新 -
其它常用属性和方法
一把吉他,一部单反,行走江湖