自定义可扩展列表视图中 ToggleButton 的状态

发布于 2024-12-28 14:50:54 字数 3854 浏览 5 评论 0原文

在我的应用程序中,我使用自定义 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;
    }
}

In my application I am using custom ExpandableListView. In the ExpandableListView there is a ToggleButton in every child. states of the ToggleButton are not remain unchanged when I expand and collapse other groups.

My adapter code is:

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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恍梦境° 2025-01-04 14:50:54

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

Remove condition if (convertView == null) from getChildView

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文