состояния ToggleButton в настраиваемом расширяемом списке - PullRequest
1 голос
/ 24 января 2012

В моем приложении я использую пользовательский 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;
    }
}

1 Ответ

0 голосов
/ 10 января 2013

Удалить условие if (convertView == null) из getChildView

...