android分开布局

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>

效果如下:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值