Android GridView中存在checkbox

Android GridView中存在checkbox的自定义dialog


在项目中遇到了要使用自定义的dialog,其中要存在gridview,gridview中要存在checkbox。

直接贴代码:


主布局:mainlayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/tv_count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="点击选择楼层"/>
</RelativeLayout>

布局名称 :checkdialog

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:layout_width="330dp"
        android:layout_height="330dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/tab_rectangle" >
        <TextView
            android:id="@+id/tv_prompt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="17dp"
            android:layout_marginTop="15dp"
            android:text="请选择楼层"
            android:textColor="#000000"
            android:textSize="15sp" />
        <CheckBox
            android:id="@+id/select_true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/tv_prompt"
            android:layout_alignBottom="@+id/tv_prompt"
            android:layout_alignParentRight="true"
            android:layout_marginRight="22dp"
            android:button="@drawable/selector_radio"
            android:text="全选"
            android:textSize="15sp" />
        <View
            android:id="@+id/view_linerr"
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:layout_alignParentLeft="true"
            android:layout_marginTop="45dp"
            android:background="#e1e1e1" />
        <GridView
            android:id="@+id/gridview_floor"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_marginTop="20dp"
            android:layout_below="@+id/view_linerr"
            android:numColumns="4" />
        <View
            android:id="@+id/view_linerrs"
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:layout_above="@+id/bt_OK"
            android:layout_alignParentLeft="true"
            android:layout_marginBottom="15dp"
            android:background="#e1e1e1" />
        <Button
            android:id="@+id/bt_OK"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginBottom="10dp"
            android:background="@drawable/tab_fille"
            android:text="确定"
            android:textColor="#ffffff"
            android:textSize="15sp" />
    </RelativeLayout>
</RelativeLayout>

gridview 的item布局名称:gridview_item_floor

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp">
    <CheckBox
        android:id="@+id/item_floor_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:button="@drawable/checkbox_floorselect"/>
    <TextView
        android:id="@+id/tv_floor_num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="1"
        android:textColor="#000000" />

</RelativeLayout>

布局中包括圆角矩形和checkbox的图片转换

白底的圆角矩形:tab_rectangle

<?xml version="1.0" encoding="utf-8"?>  
<shape xmlns:android="http://schemas.android.com/apk/res/android">    
    <solid android:color="#ffffff" />    
    <corners android:topLeftRadius="10dp"   
             android:topRightRadius="10dp"    
             android:bottomRightRadius="10dp"   
             android:bottomLeftRadius="10dp"/>    
</shape> 

蓝底的圆角矩形:tab_fill

<?xml version="1.0" encoding="utf-8"?>  
<shape xmlns:android="http://schemas.android.com/apk/res/android">    
    <solid android:color="#0185ce" />    
    <corners android:topLeftRadius="5dp"   
             android:topRightRadius="5dp"    
             android:bottomRightRadius="5dp"   
             android:bottomLeftRadius="5dp"/>    
</shape> 
全选checkbox的图片转换:selector_radio

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_checked="true" android:drawable="@drawable/radio_selected"></item>
    <item android:drawable="@drawable/radio_unselected"></item>
</selector>


checkbox  的图片转换:checkbox_floorselect

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_checked="true" android:drawable="@drawable/selsectflooritem"></item>
    <item android:drawable="@drawable/unselsectflooritem"></item>
</selector>


主activity :DialogGridView

public class DialogGridView extends Activity{
    private TextView tv_count;
    private CheckBox select_true;//全选按钮
    private GridView gridview_floor;
    private String[] floorArray = {"1","2","3","4","5","6","7","8","9","10","11","12"};//数据源
    private GridViewAdapter gridViewAdapter;//适配器
    List<String> listItemID = new ArrayList<String>();//存放选中的楼层内容
    private ArrayList<Integer> list;//存放已经选中的楼层下标
    private boolean[] floorState;//存放item的选中状态集合
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainlayout);
        tv_count = (TextView) findViewById(R.id.tv_count);
        floorState = new boolean[floorArray.length];
        tv_count.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                showdialog();
            }
        });
    }
    //弹出对话框方法
    public void showdialog(){
        final AlertDialog dialog = new AlertDialog.Builder(this).create();
        dialog.show();
        Window window = dialog.getWindow();
        window.setContentView(R.layout.checkdialog);
        select_true = (CheckBox)window.findViewById(R.id.select_true);
        gridview_floor = (GridView) window.findViewById(R.id.gridview_floor);
        Button bt_OK = (Button) window.findViewById(R.id.bt_OK);
        gridViewAdapter = new GridViewAdapter(DialogGridView.this, floorArray,floorState);
        gridview_floor.setAdapter(gridViewAdapter);
        String floor = tv_count.getText().toString();
        //截取字符串并将楼层数存到数组中
        String[] chrstr=floor.split(",");
        if(chrstr.length == floorArray.length){
            select_true.setChecked(true);
        }else{
            select_true.setChecked(false);
        }
        //全选按钮监听事件
        select_true.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(select_true.isChecked()){
                    // 遍历list的长度,将adapter中的map值全部设为true
                    for (int i = 0; i < floorArray.length; i++) {
                        GridViewAdapter.getIsSelected().put(i, true);
                    }            
                    // 刷新listview
                    gridViewAdapter.notifyDataSetChanged();
                }else{
                    for (int i = 0; i < floorArray.length; i++) {
                        if (GridViewAdapter.getIsSelected().get(i)) {
                            GridViewAdapter.getIsSelected().put(i, false);
                        }
                    }
                    gridViewAdapter.notifyDataSetChanged();
                }
            }
        });
        //gridview的监听事件
        gridview_floor.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
                // TODO Auto-generated method stub
                ViewHolder holder = (ViewHolder) view.getTag();
                //改变checkBox的选择状态
                holder.item_floor_image.toggle();
                gridViewAdapter.getIsSelected().put(position,holder.item_floor_image.isChecked());
                if (holder.item_floor_image.isChecked() == true) {
                    holder.tv_floor_num.setTextColor(Color.parseColor("#ffffff"));
                    //list  用来存放已经选中的楼层数
                    list = new ArrayList<Integer>();
                    for(int i=0;i< gridViewAdapter.getIsSelected().size();i++){
                        if( gridViewAdapter.getIsSelected().get(i)){                         
                            list.add(i);
                        }
                    }
                    //判断已经选中的楼层集合是否与总楼层数集合大小相等,用来控制全选按钮的状态
                    if(list.size() == floorArray.length){
                        select_true.setChecked(true);
                    }else{
                        select_true.setChecked(false);
                    }
                } else {
                    holder.tv_floor_num.setTextColor(Color.parseColor("#000000"));
                    list = new ArrayList<Integer>();
                    for(int i=0;i< gridViewAdapter.getIsSelected().size();i++){
                        if( gridViewAdapter.getIsSelected().get(i)){
                            list.add(i);
                        }  
                    }
                    if(list.size() == floorArray.length){
                        select_true.setChecked(true);
                    }else{
                        select_true.setChecked(false);
                    }
                }
            }
        });
        //确定按钮的监听事件
        bt_OK.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                listItemID.clear();
                String s = "";
                list = new ArrayList<Integer>();
                for(int i=0;i< gridViewAdapter.getIsSelected().size();i++){  
                    if( gridViewAdapter.getIsSelected().get(i)){
                        //得到选中的楼层数
                        listItemID.add(floorArray[i]);
                        list.add(i);
                    }  
                }  
                floorState = new boolean[floorArray.length];
                if(list != null){
                    for(int i = 0; i<list.size();i++){
                        //将选中的楼层的状态设置为true
                        floorState[list.get(i)] = true;
                    }
                }else{
                    floorState[0] = true;
                }
                
                if(listItemID.size()==0){
                    tv_count.setText("暂无数据");
                }else{
                    StringBuilder sb = new StringBuilder();  
                    for(int i=0;i<listItemID.size();i++){
                        if (i == listItemID.size()-1) {
                            sb.append(listItemID.get(i));
                        }else{
                            sb.append(listItemID.get(i)+",");
                        }
                    }
                    tv_count.setText("您选择了:" + sb.toString() + "");
                }    
                dialog.dismiss();        
            }
        });
    }
}

适配器 名称:GridViewAdapter

public class GridViewAdapter extends BaseAdapter{
    //用来控制checkBox的选中状况
    private static HashMap<Integer, Boolean> isSelected;
    private Context context;
    private String[] items;
    //传过来的checkbox的选中状况数组
    boolean[] statas;
    //用来导入布局
    private LayoutInflater inflater = null;
    public GridViewAdapter(Context context,String[] items,boolean[] statas){
        this.context = context;
        this.items = items;
        isSelected = new HashMap<Integer, Boolean>();
        inflater = LayoutInflater.from(context);
        this.statas = statas;
        initDate();
    }
     // 初始化isSelected的数据
    private void initDate(){
        for (int i = 0; i < statas.length; i++) {
            if(statas[i] == true){
                getIsSelected().put(i,true);
            }else{
                getIsSelected().put(i,false);
            }
        }
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return items.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @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 = inflater.inflate(R.layout.gride_item_floor, null);
            holder.tv_floor_num = (TextView) convertView.findViewById(R.id.tv_floor_num);
            holder.item_floor_image = (CheckBox) convertView.findViewById(R.id.item_floor_image);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        //给TextView赋值
        holder.tv_floor_num.setText(items[position]);
        // 根据isSelected来设置checkbox的选中状况
        holder.item_floor_image.setChecked(getIsSelected().get(position));
        if(getIsSelected().get(position) == true){
            holder.tv_floor_num.setTextColor(Color.parseColor("#ffffff"));
        }else{
            holder.tv_floor_num.setTextColor(Color.parseColor("#000000"));
        }
        return convertView;
    }
    public static class ViewHolder{
        public TextView tv_floor_num;
        public CheckBox item_floor_image;
    }
    public static HashMap<Integer,Boolean> getIsSelected() {
        return isSelected;
    }
}


源码下载地址:http://download.csdn.net/my



猜你喜欢

转载自blog.csdn.net/wkh11/article/details/53158243
今日推荐