ListView多选

最近做项目需要实现ListView的多选功能,虽然系统支持多选但是选中效果不太好,如果有多个界面有多选,但是风格又不一样,就不能更改Android系统的底层,否则效果就一样了。在网上搜了一下不外乎使用checbox等控件,设置ListView的多选属性,我觉得有点麻烦。所以重新写了一个支持多选的ListView的小例子,效果如下:

ListView多选_第1张图片

代码如下:

首先定义main.xml



    
    

    

        

然后定义了每个item




    

        

        
    
最后在代码中实现:

package com.bn.listviewchose;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
/**
 * 如果需要向服务器传递所选项的参数,只需再定义一个List,将选中的项加入List即可
 */
public class MainActivity extends Activity {
	private ListView mListView;
	private List mList;
	private  ListAdapter mAdapter;
	/**
	 * 测试数据
	 */
	String[] parent = { "一", "二", "三", "四", "五", "六", "七",
			"八", "九", "十", "十一", "十二", "十三", "十四",
			"十五"};

	List mListCount;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.main);
		super.onCreate(savedInstanceState);

		mListView = (ListView) findViewById(R.id.listView1);
		mListCount = new ArrayList();
		mList = new ArrayList();
		for (int i = 0; i < parent.length; i++) {
			mList.add(parent[i] + "");
		}
		initData();
	}

	public void initData() {
		mAdapter = new ListAdapter(this, mList);
		mListView.setAdapter(mAdapter);
		for (int i = 0; i < mList.size(); i++) {
			mListCount.add(0);
		}

		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView adapterView, View view,
					int position, long id) {
				if (mListCount.get(position) != 1) {
					mListCount.set(position, 1);
				} else {
					mListCount.set(position, 0);
				}

				MainActivity.this.mAdapter.notifyDataSetChanged();
			}
		});
	}

	public class ListAdapter extends BaseAdapter {
		private Context mContext;
		private List mUsers;
		private LayoutInflater mInflater;

		public ListAdapter(Context mContext, List mUsers) {
			this.mContext = mContext;
			this.mUsers = mUsers;
			mInflater = LayoutInflater.from(mContext);
		}

		@Override
		public int getCount() {

			return mUsers.size();
		}

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

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

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder;
			if (convertView == null) {
				viewHolder = new ViewHolder();
				convertView = mInflater.inflate(
						R.layout.list_item, null);
				viewHolder.mName = (TextView) convertView
						.findViewById(R.id.chose_name);
				viewHolder.mChose = (ImageView) convertView
						.findViewById(R.id.chose_img);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}

			if (mListCount.get(position) == 1) {
				viewHolder.mChose
						.setBackgroundResource(R.drawable.hose_select);
			} else {
				viewHolder.mChose
						.setBackgroundResource(R.drawable.chose_normal);
			}
			viewHolder.mName.setText(mList.get(position));

			return convertView;
		}
	}

	class ViewHolder {
		TextView mName;
		ImageView mChose;
	}
}



你可能感兴趣的:(android)