编程展开/折叠组 [英] Programatically Expand/Collapse Group

查看:215
本文介绍了编程展开/折叠组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示给用户的所有单位和所有的子单元的名单,从在ExpandableListView选择在那里他们可以勾选自己要移动哪些。因为它似乎具有的ListView prevents从扩大和崩溃,因为它自然不会,我会简单地让CheckedChange事件扩大基础上,groupPosition /折叠内可点击查看。但是,当我改变支票一个复选框,多个或所有组回应,而不是只是我单击组。

I want to display to the user a list of all the units and all their sub-units to select from in an ExpandableListView where they can check off which ones they want to move. Since it would seem having clickable Views inside of the ListView prevents it from expanding and collapsing as it naturally would, I was going to simply allow the CheckedChange event to expand/collapse based on the groupPosition. But when I change the check on one CheckBox, several or all groups responds instead of just the group that I'm clicking in.

equiplistparent.axml

equiplistparent.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:gravity="center_vertical">
<CheckBox
    android:id="@+id/check"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="45dp" />
<TextView
    android:id="@+id/info"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:textSize="20dp"
    android:gravity="left" />
<AutoCompleteTextView
    android:id="@+id/sites"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:textSize="20dp"
    android:textColor="@android:color/black"
    android:background="@android:color/white"
    android:layout_margin="5dp"
    android:textCursorDrawable="@null"
    android:gravity="center_horizontal"
    android:hint="To Store" />
</LinearLayout>

自定义适配器

class EquipAdapter : BaseExpandableListAdapter
{
    private string[] Parent { get; set; }
    private List<List<CPEquipment>> Child { get; set; }
    private Context _context { get; set; }
    private IListAdapter _adapter { get; set; }
    private ExpandableListView _list { get; set; }

    public EquipAdapter(Context context, List<string> parent, List<List<CPEquipment>> child, IListAdapter adapter, ExpandableListView list)
    {
        _context = context;
        Parent = parent.ToArray();
        Child = child;
        _adapter = adapter;
        _list = list;
    }

    public override Object GetChild(int groupPosition, int childPosition)
    {
        List<CPEquipment> level1 = Child.ElementAt(groupPosition);
        CPEquipment level2 = level1.ElementAt(childPosition);

        return level2.Serial + " " + level2.Model;
    }

    public override long GetChildId(int groupPosition, int childPosition)
    {
        return Convert.ToInt32(groupPosition.ToString(CultureInfo.InvariantCulture) + childPosition.ToString(CultureInfo.InvariantCulture));
    }

    public override int GetChildrenCount(int groupPosition)
    {
        return Child.ElementAt(groupPosition).Count;
    }

    public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
    {
        if (convertView == null)
        {
            LayoutInflater inflater = (LayoutInflater) _context.GetSystemService(Context.LayoutInflaterService);
            convertView = inflater.Inflate(Android.Resource.Layout.SimpleExpandableListItem2, null);
        }

        TextView text = (TextView) convertView.FindViewById(Android.Resource.Id.Text2);
        text.Text = GetChild(groupPosition, childPosition).ToString();

        return convertView;
    }

    public override Object GetGroup(int groupPosition)
    {
        return Parent[groupPosition];
    }

    public override long GetGroupId(int groupPosition)
    {
        return groupPosition;
    }

    public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
    {
        if (convertView == null)
        {
            LayoutInflater inflater = (LayoutInflater) _context.GetSystemService(Context.LayoutInflaterService);
            convertView = inflater.Inflate(Resource.Layout.equiplistparent, null);
        }

        TextView info = (TextView) convertView.FindViewById(Resource.Id.info);
        info.Text = GetGroup(groupPosition).ToString();
        AutoCompleteTextView acText = (AutoCompleteTextView) convertView.FindViewById(Resource.Id.sites);
        acText.Adapter = _adapter;

        CheckBox check = (CheckBox) convertView.FindViewById(Resource.Id.check);
        check.CheckedChange += (o, args) =>
                                   {
                                       int currGroup = groupPosition;
                                       if (args.IsChecked)
                                           _list.ExpandGroup(currGroup);
                                       else
                                       {
                                           _list.CollapseGroup(currGroup);
                                       }
                                   };
        return convertView;
    }

    public override bool IsChildSelectable(int groupPosition, int childPosition)
    {
        return true;
    }

    public override int GroupCount
    {
        get { return Parent.Length; }
    }

    public override bool HasStableIds
    {
        get { return true; }
    }

}

我试图解决此问题,使用工作的<一个href=\"http://stackoverflow.com/questions/2322390/android-row-becomes-unclickable-with-button/2323085#2323085\">this建议,但它使在列表视图功能自然展开或折叠没有什么区别。

I attempted working around this issue using this suggestion, but it made no difference in the ListViews ability to naturally expand or collapse.

我试图把复选框中的孩子,而不是父母,只是强迫所有群体开放。但现在,如果我检查框3,3个框将只检查自己随意。我知道有在列表视图使用复选框的一种方式,但预计在矿,他们的行为不。它是用MonoDroid的问题?

I tried putting the checkboxes in the children instead of parent and just forcing all groups to be open. But now if I check 3 of the boxes, 3 more boxes will just check themselves at random. I know there is a way to use checkboxes in ListViews, but they are not behaving as expected in mine. Is it an issue with MonoDroid?

推荐答案

我最近刚刚经历了这个自己终于得到了这一切在我结束工作正常。我贴我原来的问题<一href=\"http://stackoverflow.com/questions/10546200/onchildclick-inside-expandablelistactivity-does-not-fire/10578078#10578078\">here我的最终解决方案相处一切工作正常。不知道这会帮助你,但它听起来REAL接近我是有和使用我的解决方案的问题,我能得到它正常工作。

I just recently went through this myself and finally got it all working properly on my end. I posted my original question here along with my final solution to get everything working properly. Not sure if that will help you out, but it sounds REAL close to the issue that I was having and using my solution, I was able to get it working properly.

这篇关于编程展开/折叠组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆