ExpandableListView点击组不展开

ExpandableListView click group without expanding

情况:

我正在设置 ExpandableListView 的事件处理程序。我目前有一个组点击事件和一个展开事件。

代码:

    private void ListView_GroupExpand(object sender, ExpandableListView.GroupExpandEventArgs e)
    {
        var dialogBuilder = new AlertDialog.Builder(this);
        dialogBuilder.SetTitle(Resource.String.group_title);
        dialogBuilder.SetMessage(string.Format("Expanded group {0}", e.GroupPosition));
        dialogBuilder.Create().Show();
    }

    private void ListView_GroupClick(object sender, ExpandableListView.GroupClickEventArgs e)
    {
        var dialogBuilder = new AlertDialog.Builder(this);
        dialogBuilder.SetTitle(Resource.String.group_title);
        dialogBuilder.SetMessage(string.Format("Clicked group {0}", e.GroupPosition));
        dialogBuilder.Create().Show();
    }

问题:

当我单击列表中的组 header 时,会触发 click 事件,但不会触发 expand 事件。我点击左边的小展开克拉或者只点击列表项中间的都没有关系。

询价:

有没有什么方法可以设置视图,以便单击 carat 仅触发展开事件(而不是单击事件),反之亦然,当单击列表项的中间时?如果单击克拉或列表项的中间是一回事,(如何)我可以同时触发展开和单击处理程序?还是我只选择其中之一?

Is there any way of setting the view up so that clicking the carat only fires the expand event (and not the click event), and vice versa when clicking the middle of the list item instead?

首先,您可以为 header 组创建自己的指标:

<ExpandableListView
    android:id="@+id/myExpandableListview"
    android:groupIndicator="@null"
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

并创建您的 header 布局,例如:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
  <ImageView
      android:layout_height="50dp"
      android:layout_width="50dp"
      android:id="@+id/indicator"
      android:src="@drawable/downicon" />
  <TextView
      android:id="@+id/DataHeader"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:textAppearance="?android:attr/textAppearanceLarge"
      android:text="DataHeader"
      android:layout_margin="2dp"
      android:textStyle="bold"
      android:paddingStart="50dp"
      android:paddingLeft="50dp" />
</LinearLayout>

然后像这样创建您的 ExpandableDataAdapter

public class ExpandableDataAdapter : BaseExpandableListAdapter
{
    private readonly Activity Context;

    private ExpandableListView _listview;

    public ExpandableDataAdapter(Activity newContext, List<Data> newList, ExpandableListView listview) : base()
    {
        Context = newContext;
        DataList = newList;
        _listview = listview;
    }

    protected List<Data> DataList { get; set; }

    public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
    {
        MyViewHolder holder;
        var view = convertView;

        if (view != null)
            holder = view.Tag as MyViewHolder;

        holder = new MyViewHolder();
        view = Context.LayoutInflater.Inflate(Resource.Layout.ListGroup, null);
        holder.Header = view.FindViewById<TextView>(Resource.Id.DataHeader);
        holder.Indicator = view.FindViewById<ImageView>(Resource.Id.indicator);
        view.Tag = holder;

        holder.Header.Text = ((char)(65 + groupPosition)).ToString();

        holder.Indicator.Click += (sender, e) =>
        {
            if (!isExpanded)
            {
                _listview.ExpandGroup(groupPosition);
            }
            else
            {
                _listview.CollapseGroup(groupPosition);
            }
        };

        return view;
    }

    private class MyViewHolder : Java.Lang.Object
    {
        public TextView Header { get; set; }
        public ImageView Indicator { get; set; }
    }

    //...And more other code here
}

最终使用此适配器,例如:

var adapter = new ExpandableDataAdapter(this, Data.SampleData(), listView);

现在您可以单击每个 header 项的指示器图像来展开列表: