Merge "Ensure search highlight position when scheduled runnable starts"
This commit is contained in:
@@ -141,6 +141,8 @@ public class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Highlight request accepted
|
||||||
|
mHighlightRequested = true;
|
||||||
// Collapse app bar after 300 milliseconds.
|
// Collapse app bar after 300 milliseconds.
|
||||||
if (appBarLayout != null) {
|
if (appBarLayout != null) {
|
||||||
root.postDelayed(() -> {
|
root.postDelayed(() -> {
|
||||||
@@ -152,17 +154,37 @@ public class HighlightablePreferenceGroupAdapter extends PreferenceGroupAdapter
|
|||||||
recyclerView.setItemAnimator(null);
|
recyclerView.setItemAnimator(null);
|
||||||
// Scroll to correct position after 600 milliseconds.
|
// Scroll to correct position after 600 milliseconds.
|
||||||
root.postDelayed(() -> {
|
root.postDelayed(() -> {
|
||||||
mHighlightRequested = true;
|
if (ensureHighlightPosition()) {
|
||||||
recyclerView.smoothScrollToPosition(position);
|
recyclerView.smoothScrollToPosition(mHighlightPosition);
|
||||||
mHighlightPosition = position;
|
}
|
||||||
}, DELAY_HIGHLIGHT_DURATION_MILLIS);
|
}, DELAY_HIGHLIGHT_DURATION_MILLIS);
|
||||||
|
|
||||||
// Highlight preference after 900 milliseconds.
|
// Highlight preference after 900 milliseconds.
|
||||||
root.postDelayed(() -> {
|
root.postDelayed(() -> {
|
||||||
notifyItemChanged(position);
|
if (ensureHighlightPosition()) {
|
||||||
|
notifyItemChanged(mHighlightPosition);
|
||||||
|
}
|
||||||
}, DELAY_COLLAPSE_DURATION_MILLIS + DELAY_HIGHLIGHT_DURATION_MILLIS);
|
}, DELAY_COLLAPSE_DURATION_MILLIS + DELAY_HIGHLIGHT_DURATION_MILLIS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make sure we highlight the real-wanted position in case of preference position already
|
||||||
|
* changed when the delay time comes.
|
||||||
|
*/
|
||||||
|
private boolean ensureHighlightPosition() {
|
||||||
|
if (TextUtils.isEmpty(mHighlightKey)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
final int position = getPreferenceAdapterPosition(mHighlightKey);
|
||||||
|
final boolean allowHighlight = position >= 0;
|
||||||
|
if (allowHighlight && mHighlightPosition != position) {
|
||||||
|
Log.w(TAG, "EnsureHighlight: position has changed since last highlight request");
|
||||||
|
// Make sure RecyclerView always uses latest correct position to avoid exceptions.
|
||||||
|
mHighlightPosition = position;
|
||||||
|
}
|
||||||
|
return allowHighlight;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isHighlightRequested() {
|
public boolean isHighlightRequested() {
|
||||||
return mHighlightRequested;
|
return mHighlightRequested;
|
||||||
}
|
}
|
||||||
|
@@ -129,7 +129,7 @@ public class HighlightablePreferenceGroupAdapterTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void adjustInitialExpandedChildCount_hasHightlightKey_shouldExpandAllChildren() {
|
public void adjustInitialExpandedChildCount_hasHighlightKey_shouldExpandAllChildren() {
|
||||||
final Bundle args = new Bundle();
|
final Bundle args = new Bundle();
|
||||||
when(mFragment.getArguments()).thenReturn(args);
|
when(mFragment.getArguments()).thenReturn(args);
|
||||||
args.putString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY, "testkey");
|
args.putString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY, "testkey");
|
||||||
@@ -208,7 +208,7 @@ public class HighlightablePreferenceGroupAdapterTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void updateBackground_reuseHightlightedRowForNormalRow_shouldResetBackgroundAndTag() {
|
public void updateBackground_reuseHighlightedRowForNormalRow_shouldResetBackgroundAndTag() {
|
||||||
ReflectionHelpers.setField(mAdapter, "mHighlightPosition", 10);
|
ReflectionHelpers.setField(mAdapter, "mHighlightPosition", 10);
|
||||||
mViewHolder.itemView.setTag(R.id.preference_highlighted, true);
|
mViewHolder.itemView.setTag(R.id.preference_highlighted, true);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user