ListView Loses focus when returning

0

I have a list of Products, when I press the product number 50 for example I am redirected to the product details screen when pressing on return the list loses focus and back to the top, I wanted to know how to get the position of the item which was pressed for when you click it back to the list item that I selected:

My list: Details:

    
asked by anonymous 09.06.2017 / 17:21

1 answer

1

One option would be to save the index and the position of the first item in the list before going to the layout of details, this is not finalizing the items screen . The reason you're losing position, maybe because% of the% of the items screen is re-browsing the list (check this). Here's what it would look like:

int index = listview.getFirstVisiblePosition();
View v = listview.getChildAt(0);
int itemTop = (v == null) ? 0 : (v.getTop() - listview.getPaddingTop());

To restore, you can use the onResume method on your setSelectionFromTop :

listview.setSelectionFromTop(index, itemTop);

See this answer in SOen.

Another option would be to save the state using onResume() . By exploring the activity lifecycle, you use Parcelable so that by the time you quit the activity, save the state using onPause list relation. Here's an example:

Parcelable state;

@Override
public void onPause() {    
    state = listview.onSaveInstanceState();
    super.onPause();
}

To restore, first check whether there is a saved state, for an example, if a value of onSaveInstanceState has been assigned a value. Soon after, use the method state passing as parameter the state. See:

if(state != null) {
    listview.onRestoreInstanceState(state);
}
    
09.06.2017 / 17:31