1

在我的应用程序中,我使用 custom ExpandableListView。每个孩子ExpandableListView都有ToggleButton一个。当我展开和折叠其他组时,状态ToggleButton不会保持不变。

我的适配器代码是:

public class SettingsExpandableListAdapter extends BaseExpandableListAdapter {
    private Context mContext;
    private Activity mActivity;
    private LayoutInflater mInflater;
    private String[] mGroups = null;
    private Typeface font;
    private String mChildren[][] ;
    private boolean mStates[] [];
    public SettingsExpandableListAdapter(Activity activity, String[] groups, String children[][],boolean [][] states)
    {
        mActivity = activity;
        mGroups = groups;
        mChildren= children;
        mContext  = activity;
        mStates = states; 

    }





    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mChildren[groupPosition][childPosition];
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return childPosition;
    }

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

        final String children=  (String) getChild(groupPosition, childPosition);
        final boolean state = mStates[groupPosition][childPosition];
        if (convertView == null) {

            LayoutInflater infalInflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.expandable_child_row, null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.childName);
        ToggleButton tb = (ToggleButton)convertView.findViewById(R.id.togbtn);
        if(state)
        {
            tb.setChecked(true);    
        }else
            tb.setChecked(false);
        tv.setText(children);
        tb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton cb, boolean isChecked) {
                if(isChecked)
                {
                    Settings.states[groupPosition][childPosition]=false;
                    mStates[groupPosition][childPosition] = false;
                }else
                {
                    Settings.states[groupPosition][childPosition]=true;
                    mStates[groupPosition][childPosition] = true;
                }

            }
        });

        return convertView;


    }

    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return mChildren[groupPosition].length;
    }

    @Override
    public Object getGroup(int groupPosition) {

        return mGroups[groupPosition];
    }

    @Override
    public int getGroupCount() {
        return mGroups.length;
    }

    @Override
    public long getGroupId(int groupPosition) {

        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        String group = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.expandable_group_layout, null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.tvGroup);
        tv.setText(group);

        return convertView; 

    }

    @Override
    public boolean hasStableIds() {

        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {

        return true;
    }
}
4

1 回答 1

0

if (convertView == null)从 getChildView 中删除条件

于 2013-01-10T12:53:51.787 回答