【问题标题】:Programmatically Expand/Collapse Group以编程方式展开/折叠组
【发布时间】:2012-07-01 09:22:03
【问题描述】:

我想向用户显示所有单元及其所有子单元的列表,以在 ExpandableListView 中进行选择,他们可以在其中检查要移动的单元。由于在 ListView 中似乎有可点击的视图会阻止它自然展开和折叠,所以我将简单地允许 CheckedChange 事件根据 groupPosition 展开/折叠。但是,当我更改一个 CheckBox 上的检查时,会响应多个或所有组,而不仅仅是我单击的组。

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

}

我尝试使用 this suggestion 解决此问题,但它对 ListViews 自然展开或折叠的能力没有影响。

我尝试将复选框放在子级而不是父级中,并强制所有组打开。但是现在如果我检查其中的 3 个框,另外 3 个框将随机检查自己。我知道有一种方法可以在 ListViews 中使用复选框,但它们的行为与我的预期不同。是 MonoDroid 的问题吗?

【问题讨论】:

    标签: android xamarin.android expandablelistview


    【解决方案1】:

    我自己最近刚刚经历了这一切,最后终于让它一切正常。我发布了我最初的问题here 以及我的最终解决方案,以使一切正常运行。不确定这是否会对您有所帮助,但这听起来与我遇到的问题非常接近,并且使用我的解决方案,我能够让它正常工作。

    【讨论】:

    • 谢谢。实际上,我在研究中遇到了该帖子,以解决我在可扩展列表方面遇到的另一个问题。是否将所有内容的可聚焦属性设置为 false 是否会使我的可点击视图不再起作用?我仍然有随机检查复选框的奇怪问题,但我想这需要单独发布。
    • 将 focusable 属性设置为 false 允许事件在链上一直运行。因此,如果您有一个可展开的列表视图并希望在活动本身中管理单击或长按事件,您可以在适配器视图上设置该可聚焦属性。我对事件没有一直到列表视图本身有疑问。至于复选框,我遇到了同样的问题。如果您查看问题中 CheckListItemAdapter.cs 的底部,您会注意到我有 2 个用于项目的类。按照该模式修复复选框。
    • 通过这些子类管理复选框的选中值,这将缓解随机复选框被选中/取消选中的问题。此时它由数据列表管理,而不是由列表视图本身管理。
    • 谢谢,但是当我将布局及其视图设置为 focusable=false 时,如您的示例所示,ListView 将停止扩展。我还看到您在 GetChildView 中删除了单击事件的位置。那么你在哪里添加了你的点击事件呢?
    • 这里的事情已经变得很模糊,所以我发布了一个新问题,其中包含关于我现在面临的确切问题的更清晰和最新的信息。 stackoverflow.com/questions/11318920/…
    猜你喜欢
    • 2015-08-19
    • 2017-05-07
    • 2011-07-06
    • 2014-12-18
    • 1970-01-01
    • 1970-01-01
    • 2018-06-17
    • 2019-11-11
    • 1970-01-01
    相关资源
    最近更新 更多