Listview Loses Focus Back

Asked

Viewed 107 times

0

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

My list: inserir a descrição da imagem aqui Details: inserir a descrição da imagem aqui

1 answer

1


One option would be you save the index and the position of the first item on the list before going to the layout details, this is if you are not finishing the item screen. The reason you are losing position is because in onResume from the items screen, be checking the list again (check this). See then how it would be:

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

To restore, you can use the method setSelectionFromTop in his onResume():

listview.setSelectionFromTop(index, itemTop);

Behold that answer in the Soen.


Another option would be to save the state using Parcelable. Exploring the life cycle of Activity, you use the onPause so that the moment you exit the activity, save the state using onSaveInstanceState list by list. See an example:

Parcelable state;

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

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

if(state != null) {
    listview.onRestoreInstanceState(state);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.