C# C中的折叠群#
我正在尝试在扩展另一个组后,将先前扩展的组折叠。除了java,这里有很多教程,而c#没有。有人能告诉我如何在C#上实现,特别是Xamarin吗? 我的ExpandableListViewAdapter:C# C中的折叠群#,c#,xamarin,xamarin.android,expandablelistview,C#,Xamarin,Xamarin.android,Expandablelistview,我正在尝试在扩展另一个组后,将先前扩展的组折叠。除了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; us
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;
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用Android.App;
使用Android.Content;
使用Android.OS;
使用Android.Runtime;
使用Android.Views;
使用Android.Widget;
使用Java.Lang;
使用静态Android.Widget.ExpandableListView;
使用Java.Util.Zip;
名称空间棒星
{
公共类ExpandableListViewAdapter:BaseExpandableListAdapter,IFilterable
{
私人语境;
公共列表列表组;
公共儿童词典;
私有组过滤器_过滤器;
私人物品;
私有可扩展列表视图_列表;
public ExpandableListViewAdapter(上下文上下文、列表组、字典listChild)
{
this.context=上下文;
this.listGroup=listGroup;
this.listChild=listChild;
_过滤器=新的组过滤器(此);
}
公共ExpandableListViewAdapter(上下文、列表列表组、字典列表子项、ExpandableListView列表):此(上下文、列表组、列表子项)
{
_列表=列表;
}
公共覆盖int-GroupCount
{
收到
{
返回listGroup.Count;
}
}
公共覆盖布尔表ID
{
收到
{
返回false;
}
}
公共过滤器=>\u过滤器;
public override Java.Lang.Object GetChild(int-groupPosition,int-childPosition)
{
var result=新列表();
TryGetValue(listGroup[groupPosition],输出结果);
返回结果[childPosition];
}
公共覆盖长GetChildId(int-groupPosition,int-childPosition)
{
返回子位置;
}
公共覆盖int GetChildrenCount(int groupPosition)
{
var result=新列表();
TryGetValue(listGroup[groupPosition],输出结果);
返回结果。计数;
}
公共覆盖无效组折叠(int-groupPosition)
{
base.ongroupposition(groupPosition);
}
公共覆盖无效OnGroupExpanded(int groupPosition)
{
if(groupPosition!=上一项)
{
_列表.折叠组合(上一项);
}
previousItem=组位置;
}
公共覆盖视图GetChildView(int-groupPosition、int-childPosition、bool-isLastChild、视图转换视图、视图组父视图)
{
{
if(convertView==null)
{
LayoutInflater充气器=(LayoutInflater)context.GetSystemService(context.LayoutInflaterService);
convertView=充气机.充气(Resource.Layout.Children,null);
}
if(isLastChild==true)
{
EditText Submit=convertView.FindViewById(Resource.Id.Submit);
Button Button=convertView.FindViewById(Resource.Id.Button);
Submit.Visibility=ViewStates.Visible;
button.Visibility=ViewStates.Visible;
TextView textViewItem=convertView.FindViewById(Resource.Id.DataValue);
textViewItem.Visibility=ViewStates.Gone;
}
其他的
{
TextView textViewItem=convertView.FindViewById(Resource.Id.DataValue);
EditText Submit=convertView.FindViewById(Resource.Id.Submit);
Button Button=convertView.FindViewById(Resource.Id.Button);
button.Visibility=ViewStates.Gone;
Submit.Visibility=ViewStates.Gone;
textViewItem.Visibility=ViewStates.Visible;
字符串内容=(字符串)GetChild(groupPosition,childPosition);
Text=content;
}
返回视图;
}
}
public override Java.Lang.Object GetGroup(int-groupPosition)
{
返回列表组[groupPosition];
}
公共覆盖长GetGroupId(int-groupPosition)
{
返回组位置;
}
公共覆盖视图GetGroupView(int-groupPosition、bool-isExpanded、视图转换视图、视图组父级)
{
if(convertView==null)
{
LayoutInflater充气器=(LayoutInflater)context.GetSystemService(context.LayoutInflaterService);
convertView=充气机.充气(Resource.Layout.Groups,null);
}
string textGroup=(string)GetGroup(groupPosition);
TextView textViewGroup=convertView.FindViewById(Resource.Id.Header);
textViewGroup.Text=textGroup;
返回视图;
}
公共覆盖bool ischildselect(int-groupPosition,int-childPosition)
{
返回true;
}
public bool onGroupClick(ExpandableListView父视图、视图v、int-groupPosition、,
长id)
{
父.平滑滚动位置(groupPosition);
if(父级.IsGroupExpanded(groupPosition))
{
父.折叠组(groupPosition);
}
其他的
{
父.ExpandGroup(groupPosition);
}
返回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);
}
}
使用Android.App;
使用Android.Widget;
使用Android.OS;
Net系统;
使用Java.Lang;
使用制度;
使用Newtonsoft.Json.Linq;
使用System.Linq;
使用Java.Util;
使用系统线程;
使用Org.Json;
使用Android.Content;
使用Android.Views;
使用System.Collections.Generic;
使用系统文本;
使用RestSharp.Extensions.MonoHttp;
使用Android.Text;
使用Android.Views.InputMethods;
使用Android.Util;
名称空间棒星
{
[活动(Label=“BarStar”,MainLauncher=true,Icon=“@drawable/logo”)]
公共课活动:活动
{
私人出口
public class ExpandableListViewAdapter : BaseExpandableListAdapter,IFilterable
{
...
//this adapter's applied expandablelistview
private ExpandableListView _exListView;
//the index for last opened group
private int _lastExpandGroupPosition;
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);
}
//create a constructor to accept the expandablelistview and reuse the original constructor
public ExpandableListViewAdapter(Context context, List<string> listGroup, Dictionary<string, List<string>> listChild, ExpandableListView exListview) : this(context, listGroup, listChild)
{
_exListView = exListview;
}
//override OnGroupExpanded to collapse the last opened group
public override void OnGroupExpanded(int groupPosition)
{
if (groupPosition != _lastExpandGroupPosition)
{
_exListView.CollapseGroup(_lastExpandGroupPosition);
}
_lastExpandGroupPosition = groupPosition;
}
...
}