【问题标题】:ExpandableListView Mono for Android适用于 Android 的 ExpandableListView Mono
【发布时间】:2023-03-25 23:30:01
【问题描述】:

我试图做这个例子http://techdroid.kbeanie.com/2010/09/expandablelistview-on-android.html,但使用 monodroid,我的问题是我曾经用 java 编程,但我有问题在 BaseExpandableListAdapter Abstracs 方法上调用子列表,因为我需要例如把列表中的 groupPosition 和 childPosition,那么我该如何解决呢?

【问题讨论】:

  • 我觉得我不太明白这个问题。你会如何在java代码中做到这一点?

标签: c# listview xamarin.android expandablelistview


【解决方案1】:

创建ExpandableListAdapter 的问题在于这些方法要求您返回Java.Lnag.Object. 的类型我无法让您的java 示例工作。

但这里有一个使用ExpandableListView 而不定义自己的ExpandableListAdapter 而只是使用SimpleExpandableListAdapter 类的示例。

此示例将使您能够使用ExpandableListView,但它不会为您提供BaseExpandableListAdapter 附带的额外灵活性。

using System;
using System.Collections.Generic;

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

namespace Scratch.ExpandableListActivity
{
    [Activity (Label = "Scratch.ExpandableListActivity", MainLauncher = true)]
    public class Activity1 : Android.App.ExpandableListActivity
    {
        IExpandableListAdapter mAdapter;
        const string Name = "NAME";
        const string IsEven = "IS_EVEN";

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            using (var groupData = new JavaList<IDictionary<string, object>> ())
            using (var childData = new JavaList<IList<IDictionary<string, object>>> ()) {
                for (int i = 0; i < 20; i++) {
                    using (var curGroupMap = new JavaDictionary<string, object>()) {
                        groupData.Add(curGroupMap);
                        curGroupMap.Add(Name, "Group " + i);
                        curGroupMap.Add(IsEven, (i % 2 == 0) ? "This group is even" : "This group is odd");
                        using (var children = new JavaList<IDictionary<string, object>> ()) {
                            for ( int j = 0; j < 15; j++) {
                                using (var curChildMap = new JavaDictionary<string, object> ()) {
                                    children.Add(curChildMap);
                                    curChildMap.Add(Name, "Child " + j);
                                    curChildMap.Add(IsEven, (j % 2 == 0) ? "This child is even" : "This child is odd");
                                }
                            }
                            childData.Add(children);
                        }
                    }
                }
                // Set up our adapter
                mAdapter = new SimpleExpandableListAdapter (
                        this,
                        groupData,
                        Android.Resource.Layout.SimpleExpandableListItem1,
                        new string[] { Name, IsEven},
                        new int[] { Android.Resource.Id.Text1, Android.Resource.Id.Text2 },
                        childData,
                        Android.Resource.Layout.SimpleExpandableListItem2,
                        new string[] { Name, IsEven },
                        new int[] { Android.Resource.Id.Text1, Android.Resource.Id.Text2 }
                        );
                SetListAdapter(mAdapter);
            }
        }
    }
}

【讨论】:

  • 你会如何在片段中使用它?
【解决方案2】:

在 4.2(如 4.0.6)版本之前使用 Mono for Android 时,应该是这样的:

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Collections.Generic;
namespace MonoAndroidApplication2
{
    [Activity(Label = "Expandable List Activity", MainLauncher = true )]
    public class Activity1 : ExpandableListActivity
    {
        IExpandableListAdapter mAdapter;
        String NAME = "NAME";
        String IS_EVEN = "IS_EVEN";
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            List< IDictionary <String , object >> groupData = new List< IDictionary < string , object >>();
            List< IList<IDictionary< String, object>>> childData = new List < IList < IDictionary < string, object>>>();
            for ( int i = 0; i < 20; i++)
            {
                Dictionary< String, object > curGroupMap = new Dictionary < string , object >();
                groupData.Add(curGroupMap);
                curGroupMap.Add(NAME, "Group " + i);
                curGroupMap.Add(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd");
                List< IDictionary <String , object >> children = new List< IDictionary < string , object >>();
                for ( int j = 0; j < 15; j++)
                {
                    Dictionary< String, object > curChildMap = new Dictionary < string , object >();
                    children.Add(curChildMap);
                    curChildMap.Add(NAME, "Child " + j);
                    curChildMap.Add(IS_EVEN, (j % 2 == 0) ? "This child is even" : "This child is odd");
                }
                childData.Add(children);
            }
            // Set up our adapter
            mAdapter = new SimpleExpandableListAdapter (
                    this,
                    groupData,
                    Android.Resource.Layout.SimpleExpandableListItem1,
                    new String[] { NAME, IS_EVEN },
                    new int[] { Android.Resource.Id.Text1, Android.Resource.Id.Text2 },
                    childData,
                    Android. Resource. Layout.SimpleExpandableListItem2,
                    new String[] { NAME, IS_EVEN },
                    new int[] { Android.Resource .Id.Text1, Android.Resource.Id.Text2 }
                    );
            SetListAdapter(mAdapter);
        }
    }
}

感谢作者lanks

【讨论】:

  • 可能是这样的; “为 4.2.1 更新”的答案也应该适用于 4.0.6,因此即使在 4.0.6 上也应该是首选,以尽量减少 4.0.6 和 4.2.1 之间的变化。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-13
  • 1970-01-01
  • 2012-02-10
  • 1970-01-01
相关资源
最近更新 更多