【问题标题】:Custom ExpandableListView with Button in Header triggers its Event multiple times while expanded标题中带有按钮的自定义 ExpandableListView 在展开时多次触发其事件
【发布时间】:2014-03-12 19:42:56
【问题描述】:

我写了一个自定义的 ExpandableListView,它的 Header 是一个相对布局,带有 TextView 和 Button 的子级。

在 GetGroupView 中,我定义了一个 btn.Click 委托,它启动了一个新的 Activity:

      public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)

    {

        var view = convertView;

        var group = this.groups[groupPosition];

        if (view == null)

        {

            var inflater = context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater;

            view = inflater.Inflate(Resource.Layout.ExpListViewItem, null);

        }

        view.FindViewById<TextView>(Resource.Id.expListViewItem_txt).Text = group.GetHeader();

        view.FindViewById<Button>(Resource.Id.expListViewItem_btn).Focusable = false;

        view.FindViewById<Button>(Resource.Id.expListViewItem_btn).Click += (object sender, EventArgs e) =>

        {

            var type = e.GetType();

            if(sender.GetType() == typeof(Button))

            {

            Intent temp = new Intent(context,typeof(TestActivity));

            temp.PutExtra("TestValue", group.GetHeader());

            context.StartActivity(typeof(TestActivity));

            }

        };

        return view;

    }

如果我点击一个 ChildItem 我想启动另一个 Activity:

    public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)

    {

        var view = convertView;

        var group = this.groups[groupPosition];

        var item = group.GetItems()[childPosition];

        if (view == null)

        {

            var inflater = context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater;

            view = inflater.Inflate(Resource.Layout.expListViewChildren, null);

        }

        view.FindViewById<TextView>(Resource.Id.expListViewChildren_txt).Text = item.GetHeader();

        view.Click += delegate  

        {

            Intent temp = new Intent(context, typeof(test));

            temp.PutExtra("TestValue", item.GetHeader());

            context.StartActivity(temp);

        };



        return view;
    }

如果 ListView 折叠,一切都很好,但如果它展开,Activity 会启动两次(因为 btn.click 委托被触发两次)。

我在这里错过了什么?

感谢您的帮助!

【问题讨论】:

    标签: android click expandablelistview eventhandler


    【解决方案1】:

    我遇到了非常相似的情况 - 确保您的委托没有通过被调用两次来声明两个实例。当列表视图展开时,您是否再次调用委托声明?

    可能有两个处理程序,您需要找到每个处理程序并消除其中一个。

    【讨论】:

    • 记录声明调用 - 查看您的日志以了解发生了什么。
    • @user1021605 你是从不同的地方创建这个列表吗?听起来它是在多个上下文中创建的。
    • 所以我再次检查 - 我注意到以下内容:如前所述,GetGroupView 被称为所有组中的子项总数。遍历第一个视图,视图为空并膨胀。遍历 n+1 视图视图不为空,不会膨胀,因此委托会附加 n+1 次。由于我多次遍历每个组,每个按钮都会附加几个代表 - 所以主要问题是 - 为什么 getgroupview 调用每个子项的总数?
    • 你是如何实例化你的列表视图实现的?你能发布你的代码吗?
    • 我上传了一个测试应用程序:dropbox.com/sh/v5jbx3qv2e432j0/gOjFxeN680 如果一切都崩溃了,并且你点击了 Spoiler,它的工作原理是正常的。第二次您展开一项并单击 Spoiler,标题将不是您单击的那个 - 所以关于以下代码的某些东西似乎是混乱的 view.FindViewById
    猜你喜欢
    • 1970-01-01
    • 2020-03-05
    • 2018-01-12
    • 2014-05-13
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    • 2023-01-03
    相关资源
    最近更新 更多