简体   繁体   中英

Have an element of the list clicked on load activity Monodroid

I have a little problem. When I select an item from a list is selected and is colored, until everything is right. Then when I go to another activity and I come back I want the item in the list is selected again. To summarize select an item in the list is reloaded when the activity, the ListView must know the item you want to click the loading for MonoDroid

Sorry for the bad English, I hope I explained. Thanks

The functionality you want is not normal behavior of Android, when the phone is in Touch Mode when and uses API level of less than 11.

So the easiest way is to target your application for API 11 or higher and add the following to your list item layout:

android:background="?android:attr/activatedBackgroundIndicator"

You also have to set ChoiceMode on your list view:

listView.ChoiceMode = ChoiceMode.Single;

Now to remember the position of the item selected for when returning to the ListView you need to save it somewhere. So in the ItemClick event handler you could save the position to the SharedPreferences like so:

var prefs = GetPreferences (FileCreationMode.Append);
var editor = prefs.Edit();
editor.PutInt("ListViewSelectedItem", args.Position);
editor.Commit();

And to set the highlighted item when coming back to the Activity with the ListView you could place the following code in OnResume :

var prefs = GetPreferences(FileCreationMode.Append);
var val = prefs.GetInt("ListViewSelectedItem", 0);
listView.SetItemChecked(val, true);

Given that listView is accessible from the OnResume method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM