【问题标题】:Xamarin Android Child methods never called in ExpandableListAdapterXamarin Android 子方法从未在 ExpandableListAdapter 中调用
【发布时间】:2015-01-22 16:47:58
【问题描述】:

我遇到了 ExpandableListAdapter 的问题。我无法扩展列表。问题似乎出在所有这些子方法上: Java.Lang.Object GetChild(int groupPosition, int childPosition) public override long GetChildId(int groupPosition, int childPosition) public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent) public override int GetChildrenCount(int groupPosition)

这些方法永远不会被调用。我似乎无法弄清楚为什么。这是我的代码:

ExpandableTaskListAdapter.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace MyProject
{
    public class ExpandableTaskListAdapter : BaseExpandableListAdapter
    {
        private readonly Context context;
        private readonly List<string> headers;
        private readonly Dictionary<string, List<string>> listChildData;
        private readonly LayoutInflater inflater;
        private readonly bool[] expanded;

        public ExpandableTaskListAdapter(Context context, List<string> headers, Dictionary<string, List<string>> listChildData, LayoutInflater inflater)
        {
            this.context = context;
            this.headers = headers;
            this.listChildData = listChildData;
            this.inflater = inflater;
            expanded = new bool[headers.Count];
        }

        public override void OnGroupCollapsed(int groupPosition)
        {
            expanded[groupPosition] = !expanded[groupPosition];
            base.OnGroupCollapsed(groupPosition);
        }

        public override void OnGroupExpanded(int groupPosition)
        {
            expanded[groupPosition] = !expanded[groupPosition];
            base.OnGroupExpanded(groupPosition);
        }

        public override Java.Lang.Object GetChild(int groupPosition, int childPosition)
        {
            return listChildData[headers[groupPosition]][childPosition];
        }

        public override long GetChildId(int groupPosition, int childPosition)
        {
            return childPosition;
        }

        public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
        {
            string childText = (string)GetChild(groupPosition, childPosition);
            convertView = inflater.Inflate(Resource.Layout.task_child, null);

            TextView listChild = convertView.FindViewById<TextView>(Resource.Id.label);
            listChild.Text = childText;

            return convertView;
        }

        public override int GetChildrenCount(int groupPosition)
        {
            return listChildData[headers[groupPosition]].Count;
        }

        public override Java.Lang.Object GetGroup(int groupPosition)
        {
            return headers[groupPosition];
        }

        public override long GetGroupId(int groupPosition)
        {
            return groupPosition;
        }

        public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
        {
            string headerTitle = (string)GetGroup(groupPosition);

            convertView = inflater.Inflate(Resource.Layout.task_child, null);

            TextView header = convertView.FindViewById<TextView>(Resource.Id.label);
            header.Text = headerTitle;

            convertView.FindViewById<LinearLayout>(Resource.Id.main_layout).Click += (sender, e) =>
            {
                if (expanded[groupPosition])
                    OnGroupCollapsed(groupPosition);
                else
                    OnGroupExpanded(groupPosition);
            };

            return convertView;
        }

        public override int GroupCount
        {
            get { return headers.Count; }
        }

        public override bool HasStableIds
        {
            get { return true; }
        }

        public override bool IsChildSelectable(int groupPosition, int childPosition)
        {
            return true;
        }
    }
}

task_child.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dp">

  <TextView
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:layout_gravity="center_vertical"
    android:textSize="17sp"
    android:paddingLeft="40dp"/>

  <ImageView
    android:id="@+id/icon"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="5dp"
    android:gravity="center_vertical"
    android:layout_gravity="center_vertical"
    android:background="@drawable/ic_delete"/>

  <CheckBox
    android:id="@+id/checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:layout_gravity="center_vertical"/>

</LinearLayout>

在构造函数中放一个断点,我可以看到我传入了正确的数据。为什么永远不会调用子方法?

【问题讨论】:

  • public override int GroupCount { get { return headers.Count; } }
  • 你能调试那行并检查值吗?
  • 在 get { return headers.Count; 上设置断点可以看到 headers.Count 的值为 1。
  • 我猜你有可能在调试器暂停应用时调用方法,所以尝试使用 0 或 1 调用 GetChildrenCount 并查看结果。
  • 如果结果为0,表示你没有孩子。如果不止于此,那么 hasStableIds 会变为 false,否则我会要求 DeIcaza 退款:D

标签: c# android xamarin expandablelistadapter


【解决方案1】:

当您的GroupHeaderView 被点击时,ExpandableListView 会调用设置了expanded[position] = trueOnGroupExpanded

但是,GroupHeaderView 中 ListView 上的 .Click 订阅者也会被调用,它会在其中找到 expanded[position] == true 并调用 OnGroupCollapsed 方法。这导致 Group 根本没有被扩展,也没有对其子项提出请求。

只需删除 GetGroupView 方法中的 .Click 处理程序,一切都会正常工作。

    public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
    {
        string headerTitle = (string)GetGroup(groupPosition);

        convertView = inflater.Inflate(Resource.Layout.task_child, null);

        TextView header = convertView.FindViewById<TextView>(Resource.Id.label);
        header.Text = headerTitle;

        return convertView;
    }

【讨论】:

  • 否定,没有解决问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-21
  • 1970-01-01
相关资源
最近更新 更多