在xamarin android上单击时隐藏/显示listview的麻烦

问题描述:

试图使用Visual Studio在xamarin android中单击图像来显示和隐藏(切换)列表视图.但是它没有隐藏.我在这里做错了什么.onclick我无法隐藏和显示listview.下面是我的onclick代码

Trying to show and hide(toggle) a listview on click of image in xamarin android using visual studio.But it doesnt hide.What am i doing wrong here. onclick i am not able to hide and show the listview.below is my code for 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的新手.感谢您的帮助.

I am new bie to android and xamarin.Any help is appreciated.

这是错误的...

 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,否则它将无法正常工作.

You get the reference to the image view in the first line, but then do a FindViewById on that view to get a subview. Unless you have an ImageView nested inside of another ImageView, this isn't going to work.

你很近.这应该工作而不会看到您的布局...

You were close. This should work without seeing your layout...

 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 是一个昂贵的电话.如果不需要,请尽量不要一遍又一遍.

FindViewById is an expensive call. Try not to make it over and over if you don't have to.