Android:CheckBox树

时间:2013-11-18 08:02:35

标签: android checkbox expandablelistview

在我的应用程序中,我有一个列表和一个按钮,在我的列表中我希望当我选择父项时,所有子项都被选中,我确实需要一个复选框树。我使用可扩展列表视图但我的代码中存在问题:

我的代码是: list_group.xml:

<?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:orientation="vertical"
              android:padding="8dp"
              android:background="#000000">


    <CheckBox
            android:id="@+id/checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:layout_marginLeft="30dp"
            android:layout_marginRight="2dp"
            android:focusable="false"/>

    <TextView
            android:id="@+id/lblListHeader"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
            android:textSize="17dp"
            android:textColor="#ffffff" />

</LinearLayout>

list_item2.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="55dip"
              android:orientation="horizontal"
              android:background="#ffffff">

    <CheckBox
            android:id="@+id/checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:layout_marginLeft="30dp"
            android:layout_marginRight="2dp"
            android:focusable="false"/>

    <TextView
            android:id="@+id/lblListItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="17dip"
            android:paddingTop="5dp"
            android:paddingBottom="5dp"
            android:layout_marginLeft="-40dp"
            android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"/>

</LinearLayout>

backup_list.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="#000000">

    <ExpandableListView
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:id="@+id/expandableListView"/>

    <Button
            android:id="@+id/backup"
            android:layout_gravity="center_horizontal"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:text="BackUp"
            android:clickable="true" />

</LinearLayout>

我该如何解决?

ExpandableListAdapter:

package com.mCloud.android.ui.activity;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import com.mCloud.android.R;

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<String>> _listDataChild;

    public ExpandableListAdapter(Context context, List<String> listDataHeader,
                                 HashMap<String, List<String>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item2, null);
        }

        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.lblListItem);

        txtListChild.setText(childText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent){
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }

        TextView lblListHeader = (TextView) convertView.findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition){
        return true;
    }
}

ParentActivity:

package com.mCloud.android.ui.activity;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.widget.ExpandableListView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.mCloud.android.Log_OC;
import com.mCloud.android.R;
import com.mCloud.android.filters.MediaFilenameFilter;

public class ParentActivity extends SherlockFragmentActivity {

    private static final String TAG = "mCloudPreferences";
    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.backup_list);
        ActionBar actionBar = getSherlock().getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        // get the listview
        expListView = (ExpandableListView) findViewById(R.id.expandableListView);

        // preparing list data
        prepareListData();

        listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);


        // setting list adapter
        expListView.setAdapter(listAdapter);
    }


    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        super.onMenuItemSelected(featureId, item);
        Intent intent;

        switch (item.getItemId()) {
            case android.R.id.home:
                intent = new Intent(getBaseContext(), DisplayInfoActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                break;
            default:
                Log_OC.w(TAG, "Unknown menu item triggered");
                return false;
        }
        return true;
    }

    /*
     * Preparing the list data
     */

    private void prepareListData() {
        listDataHeader = new ArrayList<String>();
        listDataHeader.add("Files");
        listDataHeader.add("Apps");
        listDataHeader.add("SMS");
        listDataHeader.add("Contacts");
        listDataHeader.add("Call Logs");
        listDataHeader.add("Calender");
        listDataHeader.add("Setting");


        List<String> files = new ArrayList<String>();
        File file = Environment.getExternalStorageDirectory();
        String[] list = file.list(new MediaFilenameFilter());
        if (list != null) {
            for (String name : list)
                files.add(name);
        }
        files.add("Advance Selection");

        List<String> apps = new ArrayList<String>();
        apps.add("All Apps");

        List<String> sms = new ArrayList<String>();
        sms.add("Inbox");
        sms.add("Draft");
        sms.add("Send");

        List<String> contacts = new ArrayList<String>();
        contacts.add("All Contacts");

        List<String> calllogs = new ArrayList<String>();
        calllogs.add("Resived");
        calllogs.add("Missed");
        calllogs.add("Dailed");

        List<String> calender = new ArrayList<String>();
        calender.add("All calender");

        List<String> setting = new ArrayList<String>();
        setting.add("All setting");

        listDataChild = new HashMap<String, List<String>>();
        listDataChild.put(listDataHeader.get(0), files);
        listDataChild.put(listDataHeader.get(1), apps);
        listDataChild.put(listDataHeader.get(2), sms);
        listDataChild.put(listDataHeader.get(3), contacts);
        listDataChild.put(listDataHeader.get(4), calllogs);
        listDataChild.put(listDataHeader.get(5), calender);
        listDataChild.put(listDataHeader.get(6), setting);

    }
}

1 个答案:

答案 0 :(得分:0)

但你明白ExpandableListView就像我问你的那样工作..?  当您按下父级时,父级会扩展(除非您另有说明),当您再次对其进行clikc时,父级会折叠..  如果您希望能够扩展父级并且还能够选中和取消选中父级复选框,则需要在父级的同一行中提供两个视图,第一个是复选框,第二个是父级标题(可以像“”一样空 他们应该并肩。

你可以做这样的事情:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Rl11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/desserts_main_expand"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:clickable="true"
    android:cropToPadding="true"
    android:padding="4dp"
    android:scaleType="fitXY"
    android:src="@android:drawable/ic_menu_add" />

<TextView
    android:id="@+id/ParentTextI"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    android:layout_toLeftOf="@+id/addIng"
    android:layout_toRightOf="@+id/Side_image"
    android:gravity="center_vertical"
    android:scrollHorizontally="true"
    android:text="dummy text"
    android:textAlignment="center"
    android:textColor="@color/white"
    android:textSize="20sp"
    android:textStyle="bold|italic" />
</RelativeLayout>

然后在适配器中:

声明一个全局变量

CheckBox checkbox;

**这仅适用于父复选框部分

public View getGroupView(int groupPosition, boolean isExpanded, View convertView, 
      ViewGroup parent) {
checkbox.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                    if(checkbox.isChecked()=true)
                      {checkbox.setChecked(false);}
                    else checkbox.setChecked(true);
                ParentActivity.checkall();
                }
            });

然后在ParentActivity中:

添加一个检查同一父级的每个孩子的函数:

public static void checkall()
{
for(int j=0 ; j<files.size() ; j++)
 {if (listAdapter.checkbox.isChecked()==true)
 {files.get(j).setChecked(true)};
} 

我已经专门为您制作了代码,因此可能会出现一些错误,因为我已经检查过了.. 但它应该是这样的事情。