代码最主要的就是封装的viewHolder工具类,
package com.example.util;
import Android.util.SparseArray;
import android.view.View;
public class ViewHolder {
// I added a generic return type to reduce the casting noise in client code
@SuppressWarnings("unchecked")
public static <T extends View> T get(View view, int id) {
SparseArray<View> viewHolder = (SparseArray<View>) view.getTag();
if (viewHolder == null) {
viewHolder = new SparseArray<View>();
view.setTag(viewHolder);
}
View childView = viewHolder.get(id);
if (childView == null) {
childView = view.findViewById(id);
viewHolder.put(id, childView);
}
return (T) childView;
}
}
下面是怎么使用这个工具类:
public class MainActivity extends Activity {
private ListView listview;
private List<StudentInfo> list = new ArrayList<>();
private StudentInfo stu;
private ListAdapter adapter;
private LinearLayout inflate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.listview);
filldata();
show();
}
private void filldata() {
// TODO Auto-generated method stub
stu = new StudentInfo("sssss", R.drawable.head);
list.add(stu);
stu = new StudentInfo("sssaa", R.drawable.head);
list.add(stu);
stu = new StudentInfo("www", R.drawable.head);
list.add(stu);
stu = new StudentInfo("rrr", R.drawable.head);
list.add(stu);
stu = new StudentInfo("tt", R.drawable.head);
list.add(stu);
}
private void show() {
adapter = new ListAdapter();
listview.setAdapter(adapter);
}
public class ListAdapter extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@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
StudentInfo info = list.get(position);
// MyTag tag;
// if (convertView == null || convertView.getTag() == null) {
// inflate = (LinearLayout)
// getLayoutInflater().inflate(R.layout.item, null);
// tv = (TextView) findViewById(R.id.tv);
// iv = (ImageView) findViewById(R.id.iv);
//
// tag = new MyTag();
// tag.iv = iv;
// tag.tv = tv;
//
// convertView = inflate;
//
// convertView.setTag(tag);
// } else {
// tag = (MyTag) convertView.getTag();
// iv = tag.iv;
// tv = tag.tv;
//
// }
/**
* 将item布局文件引入
*/
if (convertView == null) {
convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item, parent, false);
}
/**
* list 实体 (View view, int id):(convertView,item.id)
*/
ImageView iv = ViewHolder.get(convertView, R.id.iv);
TextView tv = ViewHolder.get(convertView, R.id.tv);
/**
* list 实体结果录入
*/
tv.setText(stu.getName());
iv.setImageResource(stu.getPic());
int imgStr = info.getPic();
if ("null".equals(imgStr)) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.head);
iv.setImageBitmap(bitmap);
}
String name = info.getName();
if ("null".equals(name)) {
tv.setText("22222");
}
return convertView;
}
// private class MyTag {
// ImageView iv;
// TextView tv;
// }
}
}
注:加备注的是没封装之前的代码,使用工具类之后的代码明显比普通viewholder写的代码少,并且在项目中适配器用到的回很多,如果每次都写这么多相同的代码,会又什么感觉,所以,我们尽量要使用工具类来改进我们的代码,
demo下载地址:http://download.csdn.net/detail/qq_34411756/9529030