简体   繁体   English

ExpandableListView点击组而不扩展

[英]ExpandableListView click group without expanding

The situation: 情况:

I am working on setting up an ExpandableListView 's event handlers. 我正在设置ExpandableListView的事件处理程序。 I currently have a group click event and an expand event. 我目前有一个组点击事件和一个扩展事件。

The codez: 编码:

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

The problem: 问题:

When I click a group header in the list, the click event fires, but the expand event does not. 当我单击列表中的组标题时,将触发click事件,但不会触发expand事件。 It doesn't matter if I click on the little expand carat to the left or just click in the middle of the list item. 我单击左侧的小克拉按钮或仅单击列表项的中间都没关系。

The inquiry: 查询:

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? 有什么方法可以设置视图,以便单击克拉仅会触发扩展事件(而不触发click事件),反之亦然,当单击列表项的中间时会触发该事件吗? And if clicking the carat or the middle of the list item are one and the same thing, (how) can I get both expand and click handlers to fire off? 如果单击克拉或列表项的中间是一回事,(如何)我可以同时展开和单击处理程序来启动? Or do I just choose one over the other? 还是我只选择一个?

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? 有什么方法可以设置视图,以便单击克拉仅会触发扩展事件(而不触发click事件),反之亦然,当单击列表项的中间时会触发该事件吗?

Firstly you can create your own indicator for group 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" />

And create your header layout for example like this: 并创建示例标题布局,如下所示:

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

Then create your ExpandableDataAdapter for example like this: 然后创建您的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
}

Finally use this adapter for example like this: 最后使用该适配器,例如:

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

Now you can click the indicator image of each header item to expand the list: 现在,您可以单击每个标题项目的指标图像以展开列表:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM