带有Checkboxes的ExpandableListView在Android中

Sha*_*ark 1 eclipse checkbox layout android expandablelistview

我正在尝试使用Eclipse在Android中创建一个简单的任务列表.我正在尝试构建的布局是这样的:

布局

我希望任务成为组,并在每个任务下,2个具有任务描述和任务限制日期的子项.

在任务/组的右侧,我想要一个用于将任务标记为已完成的复选框,但我真的不知道该怎么做.我甚至在使用ExpandableListView的适配器时遇到了问题(我设法找到所有关于使用ListViews和字符串映射从废料中创建ExpandableListView的问题,但我想在我的XML布局中使用已创建的ExpandableListView,如果可能的话)或思考如何我将从输入中添加任务(输入是按下按钮时出现的AlertDialog).

任何想法将不胜感激,并抱歉我的英语(不是我的母语).

man*_*ngo 8

我做这样的事情的方法是使用自定义适配器(这些扩展baseexpandableadapter类)使您的可扩展列表视图.然后,您可以为组和子视图充气或创建不同的视图.如果您使用可扩展的列表活动(与使用可扩展列表视图的常规活动),这是一个如何设置它的示例:

public class ExpandableList1 extends ExpandableListActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Set up our adapter
    ExpandableListAdapter mAdapter = new MyExpandableListAdapter(this);
    setListAdapter(mAdapter);
}
    // The custom adapter part
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
    // Sample data set.  children[i] contains the children (String[]) for groups[i].
    private String[] groups = { "Task1", "Task2", "Task3", "Task4"};
    private String[][] children = {
            { "Task Description", "Task time limit date" },
            { "Task Description", "Task time limit date" },
            { "Task Description", "Task time limit date" },
            { "Task Description", "Task time limit date" }
    };

public MyExpandableListAdapter(Context context) {
    super();
    this.context = context;
}

private Context context;

static class ViewHolder {
        TextView text;
        CheckBox checkbox;
    }

    public Object getChild(int groupPosition, int childPosition) {
        return children[groupPosition][childPosition];
    }

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

    public int getChildrenCount(int groupPosition) {
        return children[groupPosition].length;
    }

    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent) {
    return convertView;
    }

    public Object getGroup(int groupPosition) {
        return groups[groupPosition];
    }

    public int getGroupCount() {
        return groups.length;
    }

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

    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
            ViewGroup parent) {

    final ViewHolder holder;

        if (convertView == null) {
            LayoutInflater viewInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = viewInflater.inflate(R.layout.mtopics_groupview, parent, false);
            holder = new ViewHolder();
            holder.text = (TextView)convertView.findViewById(R.id.mtopicsgrouptv);
            holder.checkbox = (CheckBox)convertView.findViewById(R.id.cb_group);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

    holder.text.setText(getGroup(groupPosition).toString());
    return convertView;
    }

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

    public boolean hasStableIds() {
        return true;
    }

}
Run Code Online (Sandbox Code Playgroud)

您可以以编程方式创建组视图和子视图,也可以从xml中扩展它们.在我的例子中,我已经从xml中为组视图充气,因为它对于更复杂的视图来说要容易得多.我知道这种模式似乎是使用两行膨胀代码的圆形方法,但它将极大地帮助您的视图性能.您可以在此处了解详情:http://www.youtube.com/watch?v = wDBM6wVEO70.至于现在,xml非常简单,它只是一个textview和一个复选框.(小提示:groupviews中的复选框必须将"focusable"设置为false并且不能直接与组视图指示符相邻,否则组视图将无法展开)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <CheckBox
        android:id="@+id/cb_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:checked="true"
        android:focusable="false" />

    <TextView
        android:id="@+id/mtopicsgrouptv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="40dp"
        android:focusable="false"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>   
Run Code Online (Sandbox Code Playgroud)

我没有进行子视图,但根据您想要的操作,您可以在组视图之后对其进行建模.您可以在其中放置textview并根据子位置设置文本.但是我要警告你,截至目前你很快就会发现你的复选框状态在你点击它们时会很古怪.有时支票会移动或消失或根本不工作.如果你想知道为什么我可以告诉你,但我现在就发布解决方案.你必须设置一个arraylist或map或任何真正的东西来保存复选框状态,然后加载它们(因为它们的位置将对应).以下是我在getGroupView部分中的操作方法.

// make the list
ArrayList<Boolean> group_check_states = new ArrayList<Boolean> (groups.length);
        for(int i = 0; i < groups.length; i++) {
        group_check_states.add(true);
    }
// load the checkstates
    if ((group_check_states.get(groupPosition)) == true) {
        holder.checkbox.setChecked(true);
    } else {
        holder.checkbox.setChecked(false);
    }

//   save the checkstates
holder.checkbox.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            if (holder.checkbox.isChecked()) {
                group_check_states.set(groupPosition, true);
            } else {
                group_check_states.set(groupPosition, false);
                }
            }
    });
Run Code Online (Sandbox Code Playgroud)

您也可以将复选框操作代码放在那里.现在适配器看起来是部件,但它可能对你想要的东西来说太简单了.您希望能够根据警报对话框输入的内容动态设置任务.在这种情况下,你不希望使用那个简单的数组我使用(组和子),你可能想从一个arraylist加载,或者你可以动态地动态添加和删除东西的其他类型的列表,这将由警报对话框修改.

我希望它有所帮助,但我相信在此过程中还会有更多问题.如果有事情出现的话,就好了.