【问题标题】:HoloEverywhere: BaseExpandableListAdapter getChildView and Checkbox stateHoloEverywhere:BaseExpandableListAdapter getChildView 和 Checkbox 状态
【发布时间】:2013-08-02 15:18:01
【问题描述】:

我正在使用 Holoeverywhere 库和相应的类:ExpandableListviewBaseExpandableListAdapter

getChildView 的类扩展 BaseExpandableListAdapter 我有包含:

    checkBox.setText(option.getTitle());
    checkBox.setChecked(true);

checkBox 元素被渲染时,文本被正确设置,但它的状态保持未选中。为什么不检查?

一个示例适配器:

/**
 * A simple adapter which maintains an ArrayList of photo resource Ids.
 * Each photo is displayed as an image. This adapter supports clearing the
 * list of photos and adding a new photo.
 *
 */
public class MyExpandableListAdapter extends BaseExpandableListAdapter {

    // Sample data set.  children[i] contains the children (String[]) for groups[i].
    private String[] groups = { "People Names", "Dog Names", "Cat Names", "Fish Names" };
    private String[][] children = {
            { "Arnold", "Barry", "Chuck", "David" },
            { "Ace", "Bandit", "Cha-Cha", "Deuce" },
            { "Fluffy", "Snuggles" },
            { "Goldy", "Bubbles" }
    };

    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 android.widget.TextView getGenericView() {
        // Layout parameters for the ExpandableListView
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, 64);

        TextView textView = new TextView(TestActivity.this);
        textView.setLayoutParams(lp);
        // Center the text vertically
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        // Set the text starting position
        textView.setPadding(36, 0, 0, 0);
        return textView;
    }

    public CheckBox getCheckbox() {
        // Layout parameters for the ExpandableListView
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, 64);

        CheckBox checkBox = new CheckBox(TestActivity.this);
        checkBox.setLayoutParams(lp);
        // Center the text vertically
        checkBox.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        // Set the text starting position
        checkBox.setPadding(36, 0, 0, 0);
        return checkBox;
    }

    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                             View convertView, ViewGroup parent) {
        CheckBox checkBox = (CheckBox) convertView;
        if(checkBox == null) {
            checkBox = getCheckbox();
        }

        checkBox.setChecked(true);
        checkBox.setText(getChild(groupPosition, childPosition).toString());
        return checkBox;
    }

    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) {
        android.widget.TextView textView = getGenericView();
        textView.setText(getGroup(groupPosition).toString());
        return textView;
    }

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

    public boolean hasStableIds() {
        return true;
    }

}

布局位:

<org.holoeverywhere.widget.ExpandableListView
        android:id="@+id/expandable_listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:choiceMode="multipleChoice"/>

【问题讨论】:

    标签: android android-layout android-holo-everywhere


    【解决方案1】:

    我不知道您的代码到底有什么问题,但我建议您尝试将 CheckBox 更改为 CheckedTextView http://developer.android.com/reference/android/widget/CheckedTextView.html

    【讨论】:

    • 同样的结果,很遗憾
    【解决方案2】:

    可能你必须尝试公开声明CheckBox

    【讨论】:

      【解决方案3】:

      这只是一种可能,但请尝试checkBox.toggle()

      【讨论】:

        猜你喜欢
        • 2010-10-30
        • 1970-01-01
        • 1970-01-01
        • 2016-05-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多