【发布时间】:2018-05-19 08:43:24
【问题描述】:
我的C# Android 应用程序中有一个自定义列表视图,每行包含一个textview、ImageView 和一个switch。单击 Listview 项目时,我想打开该行的项目开关。
MainActivity:
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;
}
}
我不知道应该在哪里设置开关(在listView.ItemClick 事件或ListAdapter 中),我不知道如何将其设置为ON。请帮我这样做。
【问题讨论】:
-
一旦开启,你将如何处理它,听起来你需要使用 mvvm 模式并从你的视图模型中设置这些东西
-
@TheGeneral 没什么,这只是一组设计,所以用户知道他选择了这个项目(而不是复选框)。
-
听起来你想使用 mvvm,有一个表示列表项的视图模型,通过点击命令设置的 ischecked 属性将通知开关
-
你测试了吗?
标签: c# android visual-studio listview xamarin