1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-12-29 11:30:32 +00:00

Save/restore focused item

This commit is contained in:
Alexander-- 2019-11-14 20:34:31 +06:59
parent b5558a8b78
commit 9801cf50e3

View File

@ -34,6 +34,7 @@ import org.schabi.newpipe.util.NavigationHelper;
import org.schabi.newpipe.util.OnClickGesture; import org.schabi.newpipe.util.OnClickGesture;
import org.schabi.newpipe.util.StateSaver; import org.schabi.newpipe.util.StateSaver;
import org.schabi.newpipe.util.StreamDialogEntry; import org.schabi.newpipe.util.StreamDialogEntry;
import org.schabi.newpipe.views.NewPipeRecyclerView;
import org.schabi.newpipe.views.SuperScrollLayoutManager; import org.schabi.newpipe.views.SuperScrollLayoutManager;
import java.util.List; import java.util.List;
@ -50,6 +51,7 @@ public abstract class BaseListFragment<I, N> extends BaseStateFragment<I> implem
protected InfoListAdapter infoListAdapter; protected InfoListAdapter infoListAdapter;
protected RecyclerView itemsList; protected RecyclerView itemsList;
private int updateFlags = 0; private int updateFlags = 0;
private int focusedPosition = -1;
private static final int LIST_MODE_UPDATE_FLAG = 0x32; private static final int LIST_MODE_UPDATE_FLAG = 0x32;
@ -111,9 +113,22 @@ public abstract class BaseListFragment<I, N> extends BaseStateFragment<I> implem
return "." + infoListAdapter.getItemsList().size() + ".list"; return "." + infoListAdapter.getItemsList().size() + ".list";
} }
private int getFocusedPosition() {
View focusedItem = itemsList.getFocusedChild();
if (focusedItem != null) {
RecyclerView.ViewHolder itemHolder = itemsList.findContainingViewHolder(focusedItem);
if (itemHolder != null) {
return itemHolder.getAdapterPosition();
}
}
return -1;
}
@Override @Override
public void writeTo(Queue<Object> objectsToSave) { public void writeTo(Queue<Object> objectsToSave) {
objectsToSave.add(infoListAdapter.getItemsList()); objectsToSave.add(infoListAdapter.getItemsList());
objectsToSave.add(getFocusedPosition());
} }
@Override @Override
@ -121,6 +136,20 @@ public abstract class BaseListFragment<I, N> extends BaseStateFragment<I> implem
public void readFrom(@NonNull Queue<Object> savedObjects) throws Exception { public void readFrom(@NonNull Queue<Object> savedObjects) throws Exception {
infoListAdapter.getItemsList().clear(); infoListAdapter.getItemsList().clear();
infoListAdapter.getItemsList().addAll((List<InfoItem>) savedObjects.poll()); infoListAdapter.getItemsList().addAll((List<InfoItem>) savedObjects.poll());
restoreFocus((Integer) savedObjects.poll());
}
private void restoreFocus(Integer position) {
if (position == null || position < 0) {
return;
}
itemsList.post(() -> {
RecyclerView.ViewHolder focusedHolder = itemsList.findViewHolderForAdapterPosition(position);
if (focusedHolder != null) {
focusedHolder.itemView.requestFocus();
}
});
} }
@Override @Override
@ -135,6 +164,18 @@ public abstract class BaseListFragment<I, N> extends BaseStateFragment<I> implem
savedState = StateSaver.tryToRestore(bundle, this); savedState = StateSaver.tryToRestore(bundle, this);
} }
@Override
public void onStop() {
focusedPosition = getFocusedPosition();
super.onStop();
}
@Override
public void onStart() {
super.onStart();
restoreFocus(focusedPosition);
}
/*////////////////////////////////////////////////////////////////////////// /*//////////////////////////////////////////////////////////////////////////
// Init // Init
//////////////////////////////////////////////////////////////////////////*/ //////////////////////////////////////////////////////////////////////////*/