0

It posibile to delete special View in the LinearLayout onClick in Xamarin Android ? For example I introduce in LinearLyout some TextView's and I touched one to delete . I can do this without Id of TextView , can I delet throught LinearLayout ? Logical scheme :

var layout = FindViewById<LinearLayout>(Resource.Id.layout);
TextView text;
layout.Click += (sender, e) => {text = new TextView(this); text.Text="Text";    
layout.AddView(text) };


// And here I don't know how to make, something like this

layout.Click /*Click on text*/ += (sender,e) =  layout.Remove(/*OnTouched 
TextView in LinearLayout*/);

It is posibile ?

1 Answers1

0

you can use view.SetVisibility(ViewStates.Gone) to remove a view from its parent's hierarchy, try doing that into the Click handler

edit after comment:

then you should add your Click method on each view instead of adding it to the layout:

text.Click /*Click on text*/ += (sender,e) =>
    sender.SetVisibility(ViewStates.Gone);
AdricoM
  • 579
  • 4
  • 11
  • Yes you right , but I forgot to say what i have always new instance and I must get this TextView by arg. I fix code . `` – Andrei Damaschin Jan 13 '18 at 12:10
  • then you should add your Click method on each view instead of adding it to the layout: `text.Click /*Click on text*/ += (sender,e) => sender.SetVisibility(ViewStates.Gone);` – AdricoM Jan 13 '18 at 12:12
  • Yes @AdricoM . I fixed code right now . Can you see now please ? I want to work with layout like an ListView . Something like this . – Andrei Damaschin Jan 13 '18 at 12:17
  • then you should try: `var layout = FindViewById(Resource.Id.layout); TextView text; layout.Click += (sender, e) => {text = new TextView(this); text.Text="Text"; text.Click += (sender,e) => sender.SetVisibility(ViewStates.Gone); layout.AddView(text) };` – AdricoM Jan 13 '18 at 12:25
  • Somethink wrong . I can not work correct with args . Work's only like this but is deleted only last instance . `var layout = FindViewById(Resource.Id.layout); TextView text; layout.Click += (sender, e) => {text = new TextView(this); text.Text="Text"; text.Click += (sender,e) => text.Visibility = ViewStates.Gone; layout.AddView(text) };` Args absolutely not work . – Andrei Damaschin Jan 13 '18 at 12:48
  • Only the last instance is deleted because each time you click on the layout you create a new view but you assign it to the same variable. If you need to keep a track of all the Textviews, consider using a list or an array – AdricoM Jan 13 '18 at 12:55
  • Ok . I hoped to use clear LinearLayout to delete TextView wich contains . Thank you @AdricoM ! – Andrei Damaschin Jan 13 '18 at 12:59
  • @ AdricoM I hoped to work with LinearLayout cause I don't found are solution in my theme here [link](https://stackoverflow.com/questions/48170106/when-i-chose-one-item-in-listview-in-xamarin-android-is-choosen-twin) , may be you can help ? – Andrei Damaschin Jan 13 '18 at 13:02
  • answered there, hope it will help – AdricoM Jan 13 '18 at 13:16
  • @ AdricoM Thank you ! – Andrei Damaschin Jan 13 '18 at 13:22