我正在尝试在扩展另一个群组后折叠之前的扩展群组。这里有很多教程,但对于java而言没有c#。有人可以告诉我如何在C#上实现,特别是Xamarin? 我的ExpandableListViewAdapter:
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;
using Java.Lang;
using static Android.Widget.ExpandableListView;
using Java.Util.Zip;
namespace BarStar
{
public class ExpandableListViewAdapter : BaseExpandableListAdapter, IFilterable
{
private Context context;
public List<string> listGroup;
public Dictionary<string, List<string>> listChild;
private GroupFilter _filter;
private int previousItem;
private ExpandableListView _list;
public ExpandableListViewAdapter(Context context, List<string> listGroup, Dictionary<string, List<string>> listChild)
{
this.context = context;
this.listGroup = listGroup;
this.listChild = listChild;
_filter = new GroupFilter(this);
}
public ExpandableListViewAdapter(Context context, List<string> listGroup, Dictionary<string, List<string>> listChild, ExpandableListView list) : this(context, listGroup, listChild)
{
_list = list;
}
public override int GroupCount
{
get
{
return listGroup.Count;
}
}
public override bool HasStableIds
{
get
{
return false;
}
}
public Filter Filter => _filter;
public override Java.Lang.Object GetChild(int groupPosition, int childPosition)
{
var result = new List<string>();
listChild.TryGetValue(listGroup[groupPosition], out result);
return result[childPosition];
}
public override long GetChildId(int groupPosition, int childPosition)
{
return childPosition;
}
public override int GetChildrenCount(int groupPosition)
{
var result = new List<string>();
listChild.TryGetValue(listGroup[groupPosition], out result);
return result.Count;
}
public override void OnGroupCollapsed(int groupPosition)
{
base.OnGroupCollapsed(groupPosition);
}
public override void OnGroupExpanded(int groupPosition)
{
if (groupPosition != previousItem)
{
_list.CollapseGroup(previousItem);
}
previousItem = groupPosition;
}
public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
{
{
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
convertView = inflater.Inflate(Resource.Layout.Children, null);
}
if (isLastChild == true)
{
EditText Submit = convertView.FindViewById<EditText>(Resource.Id.Submit);
Button button = convertView.FindViewById<Button>(Resource.Id.Button);
Submit.Visibility = ViewStates.Visible;
button.Visibility = ViewStates.Visible;
TextView textViewItem = convertView.FindViewById<TextView>(Resource.Id.DataValue);
textViewItem.Visibility = ViewStates.Gone;
}
else
{
TextView textViewItem = convertView.FindViewById<TextView>(Resource.Id.DataValue);
EditText Submit = convertView.FindViewById<EditText>(Resource.Id.Submit);
Button button = convertView.FindViewById<Button>(Resource.Id.Button);
button.Visibility = ViewStates.Gone;
Submit.Visibility = ViewStates.Gone;
textViewItem.Visibility = ViewStates.Visible;
string content = (string)GetChild(groupPosition, childPosition);
textViewItem.Text = content;
}
return convertView;
}
}
public override Java.Lang.Object GetGroup(int groupPosition)
{
return listGroup[groupPosition];
}
public override long GetGroupId(int groupPosition)
{
return groupPosition;
}
public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
{
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
convertView = inflater.Inflate(Resource.Layout.Groups, null);
}
string textGroup = (string)GetGroup(groupPosition);
TextView textViewGroup = convertView.FindViewById<TextView>(Resource.Id.Header);
textViewGroup.Text = textGroup;
return convertView;
}
public override bool IsChildSelectable(int groupPosition, int childPosition)
{
return true;
}
public bool onGroupClick(ExpandableListView parent, View v, int groupPosition,
long id)
{
parent.SmoothScrollToPosition(groupPosition);
if (parent.IsGroupExpanded(groupPosition))
{
parent.CollapseGroup(groupPosition);
}
else
{
parent.ExpandGroup(groupPosition);
}
return true;
}
}
}
我刚编辑了这个并添加了适配器。一切都很好,但崩溃的部分。
编辑:
这是我的主要:
using Android.App;
using Android.Widget;
using Android.OS;
using System.Net;
using Java.Lang;
using System;
using Newtonsoft.Json.Linq;
using System.Linq;
using Java.Util;
using System.Threading;
using Org.Json;
using Android.Content;
using Android.Views;
using System.Collections.Generic;
using System.Text;
using RestSharp.Extensions.MonoHttp;
using Android.Text;
using Android.Views.InputMethods;
using Android.Util;
namespace BarStar
{
[Activity(Label = "BarStar", MainLauncher = true, Icon = "@drawable/logo")]
public class MainActivity : Activity
{
private ExpandableListView list;
private ExpandableListViewAdapter mAdapter;
private int length;
private List<string> group = new List<string>();
private string[] names;
private Dictionary<string, List<string>> Mapout = new Dictionary<string, List<string>>();
private SearchView searchBar;
private int length2;
private EditText Submit;
public string nameholder;
private Button profileButton;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
ActionBar.Hide();
// Set Views
searchBar = FindViewById<SearchView>(Resource.Id.searchBar);
list = FindViewById<ExpandableListView>(Resource.Id.lv);
Submit = FindViewById<EditText>(Resource.Id.Submit);
var profileButton = FindViewById<Button>(Resource.Id.profileButton);
profileButton.Click += ProfileButton_Click;
//Set Groups
WebClient client = new WebClient();
string json = client.DownloadString("https://******");
JSONArray myarray = new JSONArray(json);
length = myarray.Length();
names = new string[length];
for (int i = 0; i < length; i++)
{
JSONObject Element = myarray.GetJSONObject(i);
names[i] = Element.GetString("name");
}
setData(out mAdapter);
list.SetAdapter(mAdapter);
searchBar.QueryTextChange += searchBar_QueryTextChange;
}
private void ProfileButton_Click(object sender, EventArgs e)
{
Console.WriteLine("Works");
StartActivity(typeof(ProfileActivity));
}
private void searchBar_QueryTextChange(object sender, SearchView.QueryTextChangeEventArgs e)
{
mAdapter.Filter.InvokeFilter(e.NewText);
}
private void setData(out ExpandableListViewAdapter mAdapter)
{
string urlholder;
string url;
string json;
string time;
string timestamp;
string together;
WebClient client1 = new WebClient();
for (int i = 0; i < length; i++)
{
List<string> listplaceholder = new List<string>();
group.Add(names[i]);
urlholder = Uri.EscapeDataString(names[i]);
url = "*********" + urlholder + ".json";
json = client1.DownloadString(url);
JSONArray array2 = new JSONArray(json);
length2 = array2.Length();
for (int j = 0; j < length2; j++)
{
JSONObject Element = array2.GetJSONObject(j);
time = Element.GetString("wait");
JSONObject TimeElement = array2.GetJSONObject(j);
timestamp = TimeElement.GetString("created_at");
timestamp = timestamp.Replace("T", " at ");
int index = timestamp.IndexOf(".");
if (index > 0)
{
timestamp = timestamp.Substring(0, index);
}
together = time + " minutes posted at " + timestamp;
listplaceholder.Add(together);
}
Mapout.Add(group[i], listplaceholder);
}
mAdapter = new ExpandableListViewAdapter(this, group, Mapout,list);
}
}
答案 0 :(得分:1)
有人可以告诉我如何在C#上实施,特别是Xamarin吗?
您需要覆盖适配器的OutputClaim
并折叠最后打开的组,修改适配器的构造函数以接受它的ExpandableListView,并为最后打开的组创建索引:
SelfAssertedAttributeProvider
以下是经过修改的演示:ExpandableListViewDemo。