dafodil
dafodil

Reputation: 513

trouble in Hide/show of listview on a click in xamarin android

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);
        }

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

Upvotes: 0

Views: 1849

Answers (1)

Ryan Alford
Ryan Alford

Reputation: 7594

This is wrong...

 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;
 };

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 is an expensive call. Try not to make it over and over if you don't have to.

Upvotes: 1

Related Questions