开发者

highlight previous selected item when user come back from another activity

开发者 https://www.devze.com 2023-03-16 21:03 出处:网络
Activity1 has a listView. Clicking one item (let\'s say item 3) will start Activity2. Activity2 have a back button once clicked will bring user back to Activity1. What I want to achieve is to highligh

Activity1 has a listView. Clicking one item (let's say item 3) will start Activity2. Activity2 have a back button once clicked will bring user back to Activity1. What I want to achieve is to highlight the item 3 when user back to Activity1 so that the user have a sense where to continue. (May be i need to set the focus to item 3 as well.)

EDIT: Following code works.

   public void onResume()
{开发者_开发知识库
    super.onResume();

     //lastSelectedPosition saved in OnItemClickListener
    lv.setSelection(lastSelectedPosition);
    lv.requestFocusFromTouch();

}


well its pretty simple. just save the clicked item position of the list to a field when the list is clicked to launch your new activity.

Afterwards in the onResume() method just use myList.setSelection(savedPosition);

as for the highlighting, well focus works kinda bad especially if you have a bit more complex rows(buttons,checkboxes etc) and other ui elements beside the list that can take away the focus. i believe the best way to achieve this is just set the background of that particullar item onResume to the highlighted one and override onScroll listener to just change the background to your default when the list is scrolled. indeed its a workaround but it will work in 100% of the cases opposite to just focusing an item. plus maybe you can add animations on the view so you can make it look really nice and smooth.

0

精彩评论

暂无评论...
验证码 换一张
取 消