ExpandableListView单击组而不展开

时间:2017-10-14 16:21:38

标签: android xamarin.android event-handling expandablelistview

情况:

我正在设置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();
    }

问题:

当我单击列表中的组标题时,会触发click事件,但不会触发expand事件。如果我点击左边的小扩展克拉或者只是点击列表项目的中间,这没关系。

询问:

是否有任何设置视图的方法,以便点击克拉只会触发展开事件(而不是点击事件),反之亦然点击列表项的中间?如果单击克拉或列表项的中间是同一个东西,(怎么样)我可以得到扩展和点击处理程序来启动?或者我只选择一个而不是另一个?

1 个答案:

答案 0 :(得分:2)

  

有没有办法设置视图,以便点击克拉只会触发展开事件(而不是点击事件),反之亦然点击列表项的中间?

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

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

并创建标题布局,例如:

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

现在,您可以单击每个标题项的指标图像以展开列表:

enter image description here