android自定义ListView
2010-01-22 11:32
public class PeoplesAdapter extends BaseAdapter {

private Context context;
private LinearLayout peopleLineLayout;
private EditText headTxtSearch;


public PeoplesAdapter(Context c) {
this.context = c;
mInflater = (LayoutInflater) c
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

private ArrayList<PeopleVO> peoples = null;
private LayoutInflater mInflater = null;

@Override
public int getCount() {
if (peoples != null) {
return peoples.size();
} else {
return 0;
}

}

@Override
public Object getItem(int position) {
return position;
}

@Override
public long getItemId(int position) {
return position;
}

private static class ViewHolder {
ImageView imgCheck;
TextView txtName;
TextView txtMobile;
TableLayout peopleTableLayout;
PeopleVO people;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder = null;

if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.list_people, null);
holder.imgCheck = (ImageView) convertView
.findViewById(R.id.imgCheck);
holder.txtName = (TextView) convertView
.findViewById(R.id.txtPeopleName);
holder.txtMobile = (TextView) convertView
.findViewById(R.id.txtPeopleMobile);
} else {
holder = (ViewHolder) convertView.getTag();
}
PeopleVO currentPeople = peoples.get(position);
holder.txtName.setText(currentPeople.name);
holder.txtMobile.setText(currentPeople.mobile);
holder.people = currentPeople;
convertView.setTag(holder);
convertView.setOnClickListener(viewOnClick);

return convertView;
}

View.OnClickListener viewOnClick = new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
v.setSelected(true);
ImageView imgView = (ImageView) v.findViewById(R.id.imgCheck);
ViewHolder holder = (ViewHolder) v.getTag();
PeopleVO people = holder.people;
if (people.isSelect) {
imgView.setImageResource(R.drawable.checkbox_off_background);
} else {
imgView.setImageResource(R.drawable.checkbox_on_background);
}
people.isSelect = !people.isSelect;
}

};

public ArrayList<PeopleVO> getPeoples() {
return peoples;
}

public void setPeoples(ArrayList<PeopleVO> peoples) {
this.peoples = peoples;
notifyDataSetChanged();
}

public void addPeople(PeopleVO people) {
if (peoples == null) {
peoples = new ArrayList();
}
peoples.add(people);
}

Activity 继承 ListActivity

在onCreate中设置Adapter

setListAdapter(new PeoplesAdapter (this));

list_people.xml:

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content">

<ImageView android:layout_width="wrap_content"
android:layout_height="fill_parent" android:id="@+id/imgCheck" android:src="@drawable/checkbox_off_background" />

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="8">
<TextView android:id="@+id/txtPeopleName"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="32px" />

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="手机:" />
<TextView android:layout_width="fill_parent" android:id="@+id/txtPeopleMobile"
android:layout_height="wrap_content" />
</LinearLayout>

</LinearLayout>
</LinearLayout>

效果如下:

更多相关文章

  1. 如何用代码区分android TV和手机? 适配Android TV ?
  2. Android P 手机厂商(华为、Oppo,Vivo)屏幕适配实现记录
  3. 怎样在android实现uc和墨迹天气那样的左右拖动效果
  4. android调用手机铃声
  5. 【Android自学笔记】Android获取手机和存储卡上的图片
  6. Android 判断当前设备是手机还是平板的最有效的方法
  7. Android手机开机动画的修改

随机推荐

  1. Android之内存缓存——LruCache的使用及
  2. Android TextView的特殊使用:阴影,加样式
  3. Handler的运行机制
  4. Android(安卓)zip文件中读取图片实现Gall
  5. Android 4.0 input touch解析(一)
  6. android自定义布局中的平滑移动
  7. Java讲师与Android讲师通缉令--悬赏2000
  8. Android 开发者必备的十个工具
  9. android 进程之间通信--Android 使用【AI
  10. Android开发中的游戏开发使用View还是Sur