android+listView中嵌入checkbox

1.ListView的布局文件

<?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="fill_parent"
  android:paddingLeft="10dp"
  >
  
  <!-- <ImageView 
  	android:id="@+id/iv_open_file_select_icon"
  	android:layout_width="30dp"
  	android:layout_height="30dp"
  	android:layout_marginLeft="10dp"
  	android:layout_marginTop="10dp"
  /> -->
	<CheckBox android:id="@+id/cb_open_selector" android:button="@drawable/checkbox"
		android:duplicateParentState="true" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:layout_marginLeft="20px"
		android:layout_marginTop="3px" android:visibility="gone"
		android:focusable="false"          
		android:focusableInTouchMode="false"      
		android:clickable="false"
	></CheckBox>
  
  <ImageView 
  	android:id="@+id/iv_open_external_file_icon"
  	android:layout_width="70dp"
  	android:layout_height="70dp"
  	android:layout_marginLeft="10dp"
  />  
  <TextView 
  	android:id="@+id/tv_open_external_file_name"
  	android:layout_width="wrap_content"
  	android:layout_height="wrap_content"
  	android:layout_marginLeft="10dp"
  	android:layout_gravity="center_vertical"
  	android:textColor="#000000"
  	android:textSize="16sp"
  />
</LinearLayout>

2.自定义Adapter

    这里的listview有两种显示模式,一种是普通模式,另一种是编辑模式。在普通模式下checkbox不可见,在编辑模式下checkbox可见。

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.chaoxing.R;

public class FileAdapter extends BaseAdapter {
	private final static String TAG = "System.out";
	private Context m_context;
	private List<Map<String, Object>> m_listItems;
	private LayoutInflater m_listContainer;
	private boolean isEditMode = false;
	public Map<String, Boolean> isSelectedMap;

	public final class ListItemView {
		public CheckBox cbSelector;
		public ImageView ivFileIcon;
		public TextView tvFileName;
	}
	
	public FileAdapter(Context context, List<Map<String, Object>> listItems) {
		this.m_context = context;
		this.m_listItems = listItems;
		this.m_listContainer = LayoutInflater.from(m_context);
		isSelectedMap = new HashMap<String, Boolean>();
	}

	@Override
	public int getCount() {
		return m_listItems.size();
	}

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

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

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final int selectedId = position;
		ListItemView listItemView = null;
		if(convertView == null){
			listItemView = new ListItemView();
			convertView = m_listContainer.inflate(R.layout.open_external_file_list_item, null);
			listItemView.cbSelector = (CheckBox) convertView.findViewById(R.id.cb_open_selector);
			listItemView.ivFileIcon = (ImageView) convertView.findViewById(R.id.iv_open_external_file_icon);
			listItemView.tvFileName = (TextView) convertView.findViewById(R.id.tv_open_external_file_name);
			convertView.setTag(listItemView);
		}else{
			listItemView = (ListItemView) convertView.getTag();
		}
		
		File file = null;
		if((file = (File) m_listItems.get(selectedId).get("file")) != null){
			listItemView.tvFileName.setText(file.getName());
		}
		
		if(m_listItems.get(selectedId).get("bookType") == null){
			listItemView.cbSelector.setVisibility(View.GONE);
			listItemView.ivFileIcon.setImageResource(R.drawable.fold_icon);
		}
		else{
			if(isEditMode == true){
				listItemView.cbSelector.setVisibility(View.VISIBLE);
				if(this.isSelectedMap.get(file.getAbsolutePath()) != null)
					listItemView.cbSelector.setChecked(this.isSelectedMap.get(file.getAbsolutePath()));
				else
					listItemView.cbSelector.setChecked(false);
			}else{
				listItemView.cbSelector.setVisibility(View.GONE);
			}
			listItemView.ivFileIcon.setImageResource(R.drawable.icon);
		}
			
		
		return convertView;
	}
	
	
	public boolean isEditMode() {
		return isEditMode;
	}

	public void setEditMode(boolean isEditMode) {
		this.isEditMode = isEditMode;
	}


}

3.为ListView设置OnItemClickListener监听器

    在onItemClick中做如下处理:

File file = (File) m_fileList.get(position).get("file");
    ListItemView listItemView = (ListItemView) view.getTag();
    listItemView.cbSelector.toggle();
    m_fileAdapter.isSelectedMap.put(file.getAbsolutePath(), listItemView.cbSelector.isChecked());


 

 

阅读更多
个人分类: Android
想对作者说点什么? 我来说一句

FLEX中下拉框嵌入Checkbox

2014年02月08日 5.35MB 下载

CheckboxInDataGridSample

2009年04月23日 16KB 下载

向dataGrid中添加checkBox

2017年08月23日 64KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭