Fix behavior of the new entry highlight in case no scroll is needed

This commit is contained in:
Alexander Bakker 2021-01-27 20:42:39 +01:00
parent f4bdf4645b
commit ad138e4a4c

View file

@ -287,7 +287,6 @@ public class EntryListView extends Fragment implements EntryAdapter.Listener {
public void addEntry(VaultEntry entry) { public void addEntry(VaultEntry entry) {
addEntry(entry, false); addEntry(entry, false);
} }
@SuppressLint("ClickableViewAccessibility") @SuppressLint("ClickableViewAccessibility")
@ -295,33 +294,46 @@ public class EntryListView extends Fragment implements EntryAdapter.Listener {
int position = _adapter.addEntry(entry); int position = _adapter.addEntry(entry);
updateEmptyState(); updateEmptyState();
LinearLayoutManager layoutManager = (LinearLayoutManager) _recyclerView.getLayoutManager();
if (focusEntry && position >= 0) { if (focusEntry && position >= 0) {
RecyclerView.OnScrollListener scrollListener = new RecyclerView.OnScrollListener() { int last = layoutManager.findLastVisibleItemPosition();
@Override int first = layoutManager.findFirstVisibleItemPosition();
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) { if ((_recyclerView.canScrollVertically(1) && position > layoutManager.findLastCompletelyVisibleItemPosition())
if (newState == RecyclerView.SCROLL_STATE_IDLE) { || (_recyclerView.canScrollVertically(-1) && position < layoutManager.findFirstCompletelyVisibleItemPosition())) {
_recyclerView.removeOnScrollListener(this); RecyclerView.OnScrollListener scrollListener = new RecyclerView.OnScrollListener() {
_adapter.setTempHighlightEntry(true); @Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
final int secondsToFocus = 3; if (newState == RecyclerView.SCROLL_STATE_IDLE) {
_adapter.focusEntry(entry, secondsToFocus); _recyclerView.removeOnScrollListener(this);
_recyclerView.setOnTouchListener(null);
tempHighlightEntry(entry);
}
}
};
_recyclerView.addOnScrollListener(scrollListener);
_recyclerView.setOnTouchListener((v, event) -> {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
_recyclerView.removeOnScrollListener(scrollListener);
_recyclerView.stopScroll();
_recyclerView.setOnTouchListener(null);
} }
}
};
_recyclerView.addOnScrollListener(scrollListener);
_recyclerView.setOnTouchListener((v, event) -> {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
_recyclerView.removeOnScrollListener(scrollListener);
_recyclerView.stopScroll();
_recyclerView.setOnTouchListener(null);
}
return false; return false;
}); });
_recyclerView.smoothScrollToPosition(position); _recyclerView.smoothScrollToPosition(position);
} else {
tempHighlightEntry(entry);
}
} }
} }
public void tempHighlightEntry(VaultEntry entry) {
_adapter.setTempHighlightEntry(true);
final int secondsToFocus = 3;
_adapter.focusEntry(entry, secondsToFocus);
}
public void addEntries(Collection<VaultEntry> entries) { public void addEntries(Collection<VaultEntry> entries) {
_adapter.addEntries(entries); _adapter.addEntries(entries);
updateEmptyState(); updateEmptyState();