如何将一个checkBox添加进listview,再将listview添加进Tab呢?
展开全部
item 布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:background="@drawable/preference_single_item"
android:gravity="center_vertical"
android:orientation="horizontal" >
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/mini_avatar_shadow" />
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="4"
android:padding="8dp"
android:textColor="#000"
android:textSize="17sp" />
<CheckBox
android:id="@+id/item_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center_vertical" />
</LinearLayout>
然后 自定义adapter
public class AddGroupFriendsListAdapter extends MyBaseAdapter {
private Context context;
private Vector<Friends> items = null;
private Map<Integer, View> viewMap3 = new HashMap<Integer, View>();
public Map<Friends,Boolean> mChecked;
HashSet<String> delContactsIdSet;
/**
* 构造函数,初始化Adapter,将数据传
*
* @param items2
* @param context
*/
public AddGroupFriendsListAdapter(Vector<Friends> items2, Context context) {
this.items = items2;
this.context = context;
mChecked = new HashMap<Friends,Boolean>();
}
@Override
public Friends getItem(int position) {
return (Friends) (items.get(position));
}
@Override
public int getCount() {
return items.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
AddListFriendsItemViewHolder holder = null;
// 缓存器
View rowView = this.viewMap3.get(position);
final Friends friendtBean = getItem(position);
if (rowView == null) {
// 装载view
LayoutInflater layoutInflater = LayoutInflater.from(this.context);
rowView = layoutInflater.inflate(R.layout.addgroup_friends_list,
null);
holder = new AddListFriendsItemViewHolder();
// 获取控件
holder.headImageView = (ImageView) rowView
.findViewById(R.id.addgroup_head);
holder.nameTextView = (TextView) rowView
.findViewById(R.id.addgroup_friend_name);
holder.checkBox = (CheckBox) rowView.findViewById(R.id.item_cb);
// final String fName = friendtBean.getName();
holder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
mChecked.put(friendtBean, cb.isChecked());
if (cb.isChecked()) {
AddGroupFriends.num++;
} else {
AddGroupFriends.num--;
}
}
});
rowView.setTag(holder);
viewMap3.put(position, rowView);
} else {
holder = (AddListFriendsItemViewHolder) rowView.getTag();
}
// 对控件 赋值
holder.nameTextView.setText(friendtBean.getName());
viewMap3.put(position, rowView);
return rowView;
}
}
class AddListFriendsItemViewHolder {
ImageView headImageView;
TextView nameTextView;
CheckBox checkBox;
}
然后你在要显示listview界面添加
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="@null"
android:fadingEdge="none" />
最后在你的activity中假如
private AddGroupFriendsListAdapter addfriendsListAdapter;
addfriendsListAdapter = new AddGroupFriendsListAdapter(
item集合, this);
listView.setAdapter(addfriendsListAdapter);
addfriendsListAdapter.notifyDataSetChanged();
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:background="@drawable/preference_single_item"
android:gravity="center_vertical"
android:orientation="horizontal" >
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/mini_avatar_shadow" />
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="4"
android:padding="8dp"
android:textColor="#000"
android:textSize="17sp" />
<CheckBox
android:id="@+id/item_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center_vertical" />
</LinearLayout>
然后 自定义adapter
public class AddGroupFriendsListAdapter extends MyBaseAdapter {
private Context context;
private Vector<Friends> items = null;
private Map<Integer, View> viewMap3 = new HashMap<Integer, View>();
public Map<Friends,Boolean> mChecked;
HashSet<String> delContactsIdSet;
/**
* 构造函数,初始化Adapter,将数据传
*
* @param items2
* @param context
*/
public AddGroupFriendsListAdapter(Vector<Friends> items2, Context context) {
this.items = items2;
this.context = context;
mChecked = new HashMap<Friends,Boolean>();
}
@Override
public Friends getItem(int position) {
return (Friends) (items.get(position));
}
@Override
public int getCount() {
return items.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
AddListFriendsItemViewHolder holder = null;
// 缓存器
View rowView = this.viewMap3.get(position);
final Friends friendtBean = getItem(position);
if (rowView == null) {
// 装载view
LayoutInflater layoutInflater = LayoutInflater.from(this.context);
rowView = layoutInflater.inflate(R.layout.addgroup_friends_list,
null);
holder = new AddListFriendsItemViewHolder();
// 获取控件
holder.headImageView = (ImageView) rowView
.findViewById(R.id.addgroup_head);
holder.nameTextView = (TextView) rowView
.findViewById(R.id.addgroup_friend_name);
holder.checkBox = (CheckBox) rowView.findViewById(R.id.item_cb);
// final String fName = friendtBean.getName();
holder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
mChecked.put(friendtBean, cb.isChecked());
if (cb.isChecked()) {
AddGroupFriends.num++;
} else {
AddGroupFriends.num--;
}
}
});
rowView.setTag(holder);
viewMap3.put(position, rowView);
} else {
holder = (AddListFriendsItemViewHolder) rowView.getTag();
}
// 对控件 赋值
holder.nameTextView.setText(friendtBean.getName());
viewMap3.put(position, rowView);
return rowView;
}
}
class AddListFriendsItemViewHolder {
ImageView headImageView;
TextView nameTextView;
CheckBox checkBox;
}
然后你在要显示listview界面添加
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="@null"
android:fadingEdge="none" />
最后在你的activity中假如
private AddGroupFriendsListAdapter addfriendsListAdapter;
addfriendsListAdapter = new AddGroupFriendsListAdapter(
item集合, this);
listView.setAdapter(addfriendsListAdapter);
addfriendsListAdapter.notifyDataSetChanged();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询