Erweiterbare ListView mit AbschnittsüberschriftAndroid

Forum für diejenigen, die für Android programmieren
Anonymous
 Erweiterbare ListView mit Abschnittsüberschrift

Post by Anonymous »

Ich möchte eine erweiterbare Listenansicht mit Abschnittsüberschriften erstellen.
Ich habe es geschafft, diese mit listView zum Laufen zu bringen. Als ich jedoch zur Verwendung von expandableListView wechselte, sind einige Probleme aufgetreten:
Image
  • Während ich versuche, es zu implementieren Bei Jeffs Methode scheinen die Kopfzeilen zufällig einen Indikator zu haben und anklickbar zu sein. Wenn ich darüber nachdenke, kommt mir das nicht richtig vor. Die Header sollten keine expandableListView sein oder sich zumindest nicht so verhalten.
  • Mir ist auch aufgefallen, dass ich bei der Implementierung für benutzerdefinierte expandableListAdapter keine Funktion mehr für IsEnabled() habe. Dies könnte der Grund dafür sein, dass meine Kopfzeilen als erweiterbare Listenansicht aktiviert sind. Ich habe darüber nachgedacht, dass ich möglicherweise durch IsChildSelectable() ersetzt wurde, aber es ist nicht das, was ich brauche (nämlich die Gruppenliste aktivieren/deaktivieren).
Ich habe jemanden im IRC-Kanal gefragt und er hat mir gesagt, dass es sein könnte, dass ich ein paar Schritte beim Umbruchteil übersehe, aber da kann ich nicht sicher sein, also werde ich den Code vorerst wegwerfen.
(übrigens verwende ich Xamarins Monodroid, aber der Stil und der Ablauf sind mehr oder weniger die gleichen wie bei Android, daher verstehe ich auch den Android-Code, also hier ist er )
SeparatedExpandableAdapter-Klasse

Code: Select all

using System;
using System.Collections.Generic;
using Android.Content;
using Android.Views;
using Android.Widget;
using Object = Java.Lang.Object;

namespace AndroidApplication10
{
class SeparatedExpandableListAdapter :BaseExpandableListAdapter
{
public readonly Context context;
public readonly Dictionary sections = new Dictionary();
public readonly ArrayAdapter headers;
public static readonly int TYPE_SECTION_HEADER = 0;

public SeparatedExpandableListAdapter(Context context)
{
this.context = context;
headers = new ArrayAdapter(context, Resource.Layout.list_header, Resource.Id.list_header_title);
}

//register our adapter with a section.
public void AddSection(string section, IExpandableListAdapter adapter)
{
headers.Add(section);
sections.Add(section, adapter);
}

/*
* Algorithm for separatedList
*      ::correctly find the selected item among child adapters::
*          - subtract from the original position
*          - do logic once the position find the header (position == 0)
*          - do logic once the position find the adapter (position < size)
*/

public override Object GetGroup(int groupPosition)
{
foreach (var section in sections)
{
var size = section.Value.GroupCount + 1;
if (groupPosition == 0) return section.Key;
if (groupPosition < size) return section.Value.GetGroup(groupPosition - 1);

groupPosition -= size;
}
return null;
}
public override Object GetChild(int groupPosition, int childPosition)
{
foreach (var section in sections)
{
var size = section.Value.GroupCount + 1;
if (groupPosition == 0) return null;
if (groupPosition < size) return section.Value.GetChild(groupPosition - 1, childPosition);

groupPosition -= size;
}
return null;
}

public override int GroupCount
{
get
{
var total = 0;
foreach (var adapter in sections.Values)
{
total += adapter.GroupCount + 1;
}
return total;
}
}
public override int GetChildrenCount(int groupPosition)
{
foreach (var section in sections)
{
var size = section.Value.GroupCount + 1;
if (groupPosition == 0) return 0;
if (groupPosition < size) return section.Value.GetChildrenCount(groupPosition - 1);

groupPosition -= size;
}
return 0;
}
public override bool IsEmpty
{
get { return true; }
}

public override int GroupTypeCount
{
get
{
var total = 1;
foreach (var adapter in sections.Values)
{
Console.WriteLine("[ROXEZ]: GroupTypeCount? " + ((BaseExpandableListAdapter)adapter).GroupTypeCount);
total += ((BaseExpandableListAdapter) adapter).GroupTypeCount; //?
Console.WriteLine("[ROXEZ]: total? "  + total);
}
return total;
}
}
public override int ChildTypeCount
{
get
{
return base.ChildTypeCount;
}
}

public override int GetGroupType(int groupPosition)
{
var type = 1;
foreach (var section in sections)
{
var size = section.Value.GroupCount + 1;
if (groupPosition == 0) return TYPE_SECTION_HEADER;
if (groupPosition < size)
return ((BaseExpandableListAdapter) (section.Value)).GetGroupType(groupPosition);
groupPosition -= size;
type += ((BaseExpandableListAdapter) (section.Value)).GroupTypeCount;
}
return -1;
}
public override int GetChildType(int groupPosition, int childPosition)
{
return base.GetChildType(groupPosition, childPosition);
}

public override bool AreAllItemsEnabled()
{
return false;
}
public override bool IsChildSelectable(int groupPosition, int childPosition)
{
Console.WriteLine("Position: " + groupPosition + " | Output: " + (GetGroupType(groupPosition) != TYPE_SECTION_HEADER));
return (GetGroupType (groupPosition) != TYPE_SECTION_HEADER);
}

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

var sectionNum = 0;
foreach (var section in sections)
{
var size = section.Value.GroupCount + 1;
if (groupPosition == 0)
{
return headers.GetView(groupPosition, convertView, parent);
}
if (groupPosition < size) return section.Value.GetGroupView(groupPosition - 1, isExpanded, convertView, parent);

groupPosition -= size;
sectionNum++;
}
return null;
}
public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
{
var sectionNum = 0;
foreach (var section in sections)
{
var size = section.Value.GroupCount + 1;
if (groupPosition == 0) return null;
if (groupPosition < size) return section.Value.GetChildView(groupPosition - 1, childPosition, isLastChild, convertView, parent);

groupPosition -= size;
sectionNum++;
}
return null;
}

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

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

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post