尝试使用visual studio在xamarin android中单击图像时显示和隐藏(切换)列表视图。但它没有隐藏。我在这里做错了什么。 onclick我无法隐藏并显示listview.below是我的onclick代码
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.HomeScreen);
listView = FindViewById<ListView>(Resource.Id.List);
tableItems.Add(new TableItem() { Heading = "Vegetables", SubHeading = "65 items", ImageResourceId = Resource.Drawable.Vegetables });
tableItems.Add(new TableItem() { Heading = "Fruits", SubHeading = "17 items", ImageResourceId = Resource.Drawable.Fruits });
listView.Adapter = new HomeScreenAdapter(this, tableItems);
listView.ItemClick += OnListItemClick;
ImageView ImageView = FindViewById<ImageView>(Resource.Id.imageView1);
// ListView listView = FindViewById<ListView>(Resource.Id.List);
ImageView.FindViewById<ImageView>(Resource.Id.imageView1).Click += (object sender, System.EventArgs e) =>
{
listView.FindViewById<ListView>(Resource.Id.List).Visibility = Android.Views.ViewStates.Visible;
};
}
protected void OnListItemClick(object sender, Android.Widget.AdapterView.ItemClickEventArgs e)
{
var listView = sender as ListView;
var t = tableItems[e.Position];
Android.Widget.Toast.MakeText(this, t.Heading, Android.Widget.ToastLength.Short).Show();
Console.WriteLine("Clicked on " + t.Heading);
}
我是android和xamarin的新bie.Any帮助表示赞赏。
答案 0 :(得分:0)
这是错误的......
ImageView ImageView = FindViewById<ImageView>(Resource.Id.imageView1);
ImageView.FindViewById<ImageView>(Resource.Id.imageView1).Click += (object sender, System.EventArgs e) =>
{
listView.FindViewById<ListView>(Resource.Id.List).Visibility = Android.Views.ViewStates.Visible;
};
您在第一行中获得对图像视图的引用,但随后在该视图上执行FindViewById以获取子视图。除非你将ImageView嵌套在另一个ImageView中,否则这不会起作用。
你很亲密。这应该没有看到你的布局......
ImageView imageView = FindViewById<ImageView>(Resource.Id.imageView1);
imageView.Click += (object sender, System.EventArgs e) =>
{
if (listView.Visibility == ViewStates.Visible)
listView.Visibility = ViewStates.Invisible;
else
listView.Visibility = ViewStates.Visible;
};
FindViewById
是一个昂贵的电话。如果你不必这样做,尽量不要一遍又一遍。