gpt4 book ai didi

c# - Android - 在 ListView 项目点击时打开开关 "On"

转载 作者:太空宇宙 更新时间:2023-11-03 12:14:30 26 4
gpt4 key购买 nike

我的 C# Android 应用程序中有一个自定义 ListView ,每一行都包含一个 textviewImageView 和一个 switch。单击 Listview 项目时,我想打开该行的项目开关。

主要 Activity :

List<TableList> list = = new List<TableList>();
list.Add(new TableList("Germany"));
list.Add(new TableList("France"));
list.Add(new TableList("Finland"));
listView.ItemClick += delegate (object sender, AdapterView.ItemClickEventArgs e)
{
string selected = t.Name;
if (selected == "France")
{
// Turn the proper switch for France row ON
}
};

ListView 的 ListAdapter 和 ListClass:

public class ListAdapter : BaseAdapter<TableList>
{
List<TableList> items;
Activity context;
public ListAdapter(Activity context, List<TableList> items)
: base()
{
this.context = context;
this.items = items;
}
public override long GetItemId(int position)
{
return position;
}
public override TableList this[int position]
{
get { return items[position]; }
}
public override int Count
{
get { return items.Count; }
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = items[position];
View view = convertView;
if (view == null) // no view to re-use, create new
view = context.LayoutInflater.Inflate(Resource.Layout.CoinList, null);
view.FindViewById<TextView>(Resource.Id.CoinName).Text = item.Name;
view.FindViewById<ImageView>(Resource.Id.imageView1).SetImageResource(Resource.Drawable.n);

If item is clicked set it on
{
view.FindViewById<Switch>(Resource.Id.switch).SetOn
}
else
{
view.FindViewById<Switch>(Resource.Id.switch).SetOf
}
return view;
}
}
public class TableList
{
public string Name;
public TableList(string Name)
{
this.Name = Name;
}
}

我不知道应该在哪里设置 Switch ON(在 listView.ItemClick 事件中或在 ListAdapter 中)并且我不知道如何设置它打开。请帮助我这样做。

最佳答案

Here是我的演示。

您可以选择一个来实现您的目标。我将向您展示如何通过 ItemClick 事件执行此操作:

When a Listview item is clicked, I want to turn the row's item switch on.

因为,Switch 会从 ViewGroup 中获取焦点。因此,我将焦点从 MyAdapter 中的 Switch 移开:

holder.ms.Focusable = false;//ms is Switch

现在,这是我的 ItemClick 事件(点击项目时打开开关):

private void MListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
var ll = e.View as LinearLayout;
var sw = ll.GetChildAt(1) as Switch;
if (sw.Checked)
{
sw.Checked = false;
adapter.changeState((int)sw.Tag,false);
}
else
{
sw.Checked = true;
adapter.changeState((int)sw.Tag, true);
}
}

众所周知,ListView存在重用问题,所以,我添加了一个bool属性来控制Switch的状态:

public class MyData:Java.Lang.Object {
public MyData(string p,bool b) {
this.position = p;
this.isCheck = b;
}
public string position { get; set; }
public bool isCheck { get; set; }
}

下面是changeState方法:

internal void changeState(int position, bool v)
{
mitems[position].isCheck = v;
this.NotifyDataSetChanged();
}

这是 CheckedChange 事件:

private void Ms_CheckedChange(object sender, CompoundButton.CheckedChangeEventArgs e)
{
var sm = sender as Switch;
Log.Error("Ms_CheckedChange", (int)sm.Tag+"");
if (e.IsChecked&&!mitems[(int)sm.Tag].isCheck)
{
mitems[(int)sm.Tag].isCheck = true;
this.NotifyDataSetChanged();
}
else if(!e.IsChecked&& mitems[(int)sm.Tag].isCheck)
{
mitems[(int)sm.Tag].isCheck = false;
this.NotifyDataSetChanged();
}

}

关于c# - Android - 在 ListView 项目点击时打开开关 "On",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50423429/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com