1.看到论坛有 同学不会写 listview item 的单选和 复选 所以写了一个发出来 单选类(SignChooseActivity)
package com.zhx.listdemo;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
public class SignChooseActivity extends Activity {
private ListView mListView;
private TextAdapter mAdapter;
private List mList = new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign);
mListView = (ListView) findViewById(R.id.text_listview);
for (int i = 0; i < 20; i++) {
mList.add("去约车" + i);
}
mAdapter = new TextAdapter(mList, this);
mListView.setAdapter(mAdapter);
}
}
2.单选适配器
package com.zhx.listdemo;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
public class TextAdapter extends BaseAdapter {
private List list;
private Activity activity;
private Map isSelectedMap = new HashMap();
public TextAdapter(List list, Activity activity) {
super();
this.list = list;
this.activity = activity;
for (int i = 0; i < list.size(); i++) {
isSelectedMap.put(i + "", false);
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list == null ? 0 : list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = activity.getLayoutInflater().inflate(
R.layout.test_item, null);
holder.tv = (TextView) convertView.findViewById(R.id.test_tv);
holder.box = (ImageView) convertView
.findViewById(R.id.test_check_box);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
for (int i = 0; i < list.size(); i++) {
if (i == position) {
Log.i("点击项目", position + "");
isSelectedMap.put(i + "", true);
} else {
Log.i("未点击项目", position + "");
isSelectedMap.put(i + "", false);
}
}
notifyDataSetChanged();
}
});
Log.i("选中项", position + "@" + isSelectedMap.get(position + "") + "");
if (isSelectedMap.get(position + "")) {
holder.box.setImageResource(R.drawable.multi_select_flag);
} else {
holder.box.setImageResource(R.drawable.ic_launcher);
}
holder.tv.setText(list.get(position));
return convertView;
}
class ViewHolder {
TextView tv;
ImageView box;
}
}
package com.zhx.listdemo;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
public class TextAdapter extends BaseAdapter {
private List list;
private Activity activity;
private Map isSelectedMap = new HashMap();
public TextAdapter(List list, Activity activity) {
super();
this.list = list;
this.activity = activity;
for (int i = 0; i < list.size(); i++) {
isSelectedMap.put(i + "", false);
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list == null ? 0 : list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = activity.getLayoutInflater().inflate(
R.layout.test_item, null);
holder.tv = (TextView) convertView.findViewById(R.id.test_tv);
holder.box = (ImageView) convertView
.findViewById(R.id.test_check_box);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
for (int i = 0; i < list.size(); i++) {
if (i == position) {
Log.i("点击项目", position + "");
isSelectedMap.put(i + "", true);
} else {
Log.i("未点击项目", position + "");
isSelectedMap.put(i + "", false);
}
}
notifyDataSetChanged();
}
});
Log.i("选中项", position + "@" + isSelectedMap.get(position + "") + "");
if (isSelectedMap.get(position + "")) {
holder.box.setImageResource(R.drawable.multi_select_flag);
} else {
holder.box.setImageResource(R.drawable.ic_launcher);
}
holder.tv.setText(list.get(position));
return convertView;
}
class ViewHolder {
TextView tv;
ImageView box;
}
}
3.单选用的是listview 复选用gridview 做例子 其实一样的换个名字就好了 (MultChooseActivity)
package com.zhx.listdemo;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
public class MultChooseActivity extends Activity {
private Button mBack;
private Button mAll;
private GridView mDisplay;
private TextView mCount;
private AlbumAdapter mAdapter;
private List mSelect = new ArrayList();// 存放当前用户选择的图片的编号
private int imagesId[] = { R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,R.drawable.ic_launcher, R.drawable.ic_launcher };
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.album_activity);
findViewById();
setListener();
init();
}
private void findViewById() {
mBack = (Button) findViewById(R.id.album_back);
mAll = (Button) findViewById(R.id.album_all);
mDisplay = (GridView) findViewById(R.id.album_display);
mCount = (TextView) findViewById(R.id.album_count);
}
private void setListener() {
mBack.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// 关闭当前界面
finish();
}
});
mAll.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (mSelect.size() == imagesId.length) {
// 全部选中时,点击为反选,所以清空
mSelect.clear();
} else if (mSelect.size() > 0) {
// 当已选中时,点击为全选,添加没选择中的
for (int i = 0; i < imagesId.length; i++) {
if (mSelect.contains(String.valueOf(i))) {
continue;
}
mSelect.add(String.valueOf(i));
}
} else {
// 没选中,点击为全选,添加所有
for (int i = 0; i < imagesId.length; i++) {
mSelect.add(String.valueOf(i));
}
}
// 如果全部选中则显示反选,否则显示全选
if (mSelect.size() == imagesId.length) {
mAll.setText("反选");
} else {
mAll.setText("全选");
}
// 更新界面显示数量内容
initCount(mSelect.size());
// 刷新界面
mAdapter.notifyDataSetChanged();
}
});
mDisplay.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView> arg0, View arg1, int arg2, long arg3) {
// 查看当前点击的条目是否已经选中,选中则移出,没选中则删除
if (mSelect.contains(String.valueOf(arg2))) {
mSelect.remove(String.valueOf(arg2));
} else {
mSelect.add(String.valueOf(arg2));
}
// 如果全部选中则显示反选,否则显示全选
if (mSelect.size() == imagesId.length) {
mAll.setText("反选");
} else {
mAll.setText("全选");
}
// 更新界面显示数量内容
initCount(mSelect.size());
// 刷新界面
mAdapter.notifyDataSetChanged();
}
});
}
private void init() {
// 更新界面显示数量内容
initCount(mSelect.size());
// 初始化适配器
mAdapter = new AlbumAdapter();
// 添加适配器
mDisplay.setAdapter(mAdapter);
}
/**
* 更新界面显示数量内容
*
* @param count
* 选中的图片数量
*/
private void initCount(int count) {
// 更新选择的图片数量,如果数量大于0,设置确定按钮为可用,反之不可用并修改字体颜色
if (count > 0) {
mCount.setText("已选择" + count + "张");
mCount.setTextColor(0xFFFFFFFF);
} else {
mCount.setText("已选择0张");
mCount.setTextColor(0xFF999999);
}
}
private class AlbumAdapter extends BaseAdapter {
public int getCount() {
return imagesId.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = LayoutInflater.from(MultChooseActivity.this).inflate(R.layout.album_activity_item, null);
holder = new ViewHolder();
holder.photo = (ImageView) convertView.findViewById(R.id.album_item_photo);
holder.select = (ImageView) convertView.findViewById(R.id.album_item_select);
// padding大小为40dip,计算px值
int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, MultChooseActivity.this
.getResources().getDisplayMetrics());
// 设置显示的图片大小为屏幕宽度1/4大小
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.width = (480 - padding) / 4;
params.height = (480 - padding) / 4;
holder.photo.setLayoutParams(params);
holder.select.setLayoutParams(params);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// 添加图片
holder.photo.setImageBitmap(BitmapFactory.decodeResource(getResources(), imagesId[position]));
// 查看是否选中,选中则显示选中效果
if (mSelect.contains(String.valueOf(position))) {
holder.select.setVisibility(View.VISIBLE);
} else {
holder.select.setVisibility(View.GONE);
}
return convertView;
}
class ViewHolder {
ImageView photo;
ImageView select;
}
}
}
4 .下载地址https://github.com/zhoulinxue/Listdemo.git