Focusing on item highlighted coming from search result
There are two cases that require different approach: 1. No scrolling or only a bit of scrolling is required to reach desired item - then we can focus the item straight away. 2. We're still scrolling when highlighting is started - after DELAY_COLLAPSE_DURATION_MILLIS + DELAY_HIGHLIGHT_DURATION_MILLIS. Then we need to wait till we reach the element/stop scrolling and focus afterwards. Test: case 1: search for any setting, press enter and see if keyboard is focused on search result Test: case 2: search for setting that requires a lot of scrolling, e.g. "display cutout" and see if it's get the focus after selecting Fixes: 307902050 Change-Id: Ifa0738748184e78074099e33e09e5d8df198177e
This commit is contained in:
@@ -479,6 +479,7 @@ public abstract class SettingsPreferenceFragment extends InstrumentedPreferenceF
|
|||||||
mDialogFragment.dismiss();
|
mDialogFragment.dismiss();
|
||||||
mDialogFragment = null;
|
mDialogFragment = null;
|
||||||
}
|
}
|
||||||
|
getListView().clearOnScrollListeners();
|
||||||
}
|
}
|
||||||
super.onDetach();
|
super.onDetach();
|
||||||
}
|
}
|
||||||
|
@@ -29,12 +29,14 @@ import android.util.Log;
|
|||||||
import android.util.TypedValue;
|
import android.util.TypedValue;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.VisibleForTesting;
|
import androidx.annotation.VisibleForTesting;
|
||||||
import androidx.preference.PreferenceGroup;
|
import androidx.preference.PreferenceGroup;
|
||||||
import androidx.preference.PreferenceGroupAdapter;
|
import androidx.preference.PreferenceGroupAdapter;
|
||||||
import androidx.preference.PreferenceScreen;
|
import androidx.preference.PreferenceScreen;
|
||||||
import androidx.preference.PreferenceViewHolder;
|
import androidx.preference.PreferenceViewHolder;
|
||||||
import androidx.recyclerview.widget.RecyclerView;
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
|
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
|
||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settings.SettingsPreferenceFragment;
|
import com.android.settings.SettingsPreferenceFragment;
|
||||||
@@ -159,15 +161,32 @@ public class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter
|
|||||||
root.postDelayed(() -> {
|
root.postDelayed(() -> {
|
||||||
if (ensureHighlightPosition()) {
|
if (ensureHighlightPosition()) {
|
||||||
recyclerView.smoothScrollToPosition(mHighlightPosition);
|
recyclerView.smoothScrollToPosition(mHighlightPosition);
|
||||||
|
highlightAndFocusTargetItem(recyclerView, mHighlightPosition);
|
||||||
}
|
}
|
||||||
}, DELAY_HIGHLIGHT_DURATION_MILLIS);
|
}, DELAY_HIGHLIGHT_DURATION_MILLIS);
|
||||||
|
}
|
||||||
|
|
||||||
// Highlight preference after 900 milliseconds.
|
private void highlightAndFocusTargetItem(RecyclerView recyclerView, int highlightPosition) {
|
||||||
root.postDelayed(() -> {
|
ViewHolder target = recyclerView.findViewHolderForAdapterPosition(highlightPosition);
|
||||||
if (ensureHighlightPosition()) {
|
if (target != null) { // view already visible
|
||||||
notifyItemChanged(mHighlightPosition);
|
notifyItemChanged(mHighlightPosition);
|
||||||
}
|
target.itemView.requestFocus();
|
||||||
}, DELAY_COLLAPSE_DURATION_MILLIS + DELAY_HIGHLIGHT_DURATION_MILLIS);
|
} else { // otherwise we're about to scroll to that view (but we might not be scrolling yet)
|
||||||
|
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
||||||
|
@Override
|
||||||
|
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
|
||||||
|
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
|
||||||
|
notifyItemChanged(mHighlightPosition);
|
||||||
|
ViewHolder target = recyclerView
|
||||||
|
.findViewHolderForAdapterPosition(highlightPosition);
|
||||||
|
if (target != null) {
|
||||||
|
target.itemView.requestFocus();
|
||||||
|
}
|
||||||
|
recyclerView.removeOnScrollListener(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user