use of androidx.recyclerview.widget.StaggeredGridLayoutManager in project nextcloud-notes by stefan-niedermann.
the class MainActivity method setupNotesList.
private void setupNotesList() {
adapter = new ItemAdapter(this, gridView);
listView.setAdapter(adapter);
listView.setItemAnimator(null);
if (gridView) {
final int spanCount = getResources().getInteger(R.integer.grid_view_span_count);
final var gridLayoutManager = new StaggeredGridLayoutManager(spanCount, StaggeredGridLayoutManager.VERTICAL);
listView.setLayoutManager(gridLayoutManager);
listView.addItemDecoration(new GridItemDecoration(adapter, spanCount, getResources().getDimensionPixelSize(R.dimen.spacer_3x), getResources().getDimensionPixelSize(R.dimen.spacer_5x), getResources().getDimensionPixelSize(R.dimen.spacer_3x), getResources().getDimensionPixelSize(R.dimen.spacer_1x), getResources().getDimensionPixelSize(R.dimen.spacer_activity_sides) + getResources().getDimensionPixelSize(R.dimen.spacer_1x)));
} else {
final var layoutManager = new LinearLayoutManager(this);
listView.setLayoutManager(layoutManager);
listView.addItemDecoration(new SectionItemDecoration(adapter, getResources().getDimensionPixelSize(R.dimen.spacer_activity_sides) + getResources().getDimensionPixelSize(R.dimen.spacer_1x) + getResources().getDimensionPixelSize(R.dimen.spacer_3x) + getResources().getDimensionPixelSize(R.dimen.spacer_2x), getResources().getDimensionPixelSize(R.dimen.spacer_5x), getResources().getDimensionPixelSize(R.dimen.spacer_1x), 0));
}
listView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
if (dy > 0)
fabCreate.hide();
else if (dy < 0)
fabCreate.show();
}
});
swipeRefreshLayout.setOnRefreshListener(() -> {
CustomAppGlideModule.clearCache(this);
final var syncLiveData = mainViewModel.getCurrentAccount();
final Observer<Account> syncObserver = currentAccount -> {
syncLiveData.removeObservers(this);
mainViewModel.synchronizeCapabilitiesAndNotes(currentAccount, new IResponseCallback<>() {
@Override
public void onSuccess(Void v) {
Log.d(TAG, "Successfully synchronized capabilities and notes for " + currentAccount.getAccountName());
}
@Override
public void onError(@NonNull Throwable t) {
runOnUiThread(() -> {
swipeRefreshLayout.setRefreshing(false);
if (t instanceof IntendedOfflineException) {
Log.i(TAG, "Capabilities and notes not updated because " + currentAccount.getAccountName() + " is offline by intention.");
} else if (t instanceof NextcloudHttpRequestFailedException && ((NextcloudHttpRequestFailedException) t).getStatusCode() == HttpURLConnection.HTTP_UNAVAILABLE) {
BrandedSnackbar.make(coordinatorLayout, R.string.error_maintenance_mode, Snackbar.LENGTH_LONG).show();
} else if (t instanceof NetworkErrorException) {
BrandedSnackbar.make(coordinatorLayout, getString(R.string.error_sync, getString(R.string.error_no_network)), Snackbar.LENGTH_LONG).show();
} else {
BrandedSnackbar.make(coordinatorLayout, R.string.error_synchronization, Snackbar.LENGTH_LONG).setAction(R.string.simple_more, v -> ExceptionDialogFragment.newInstance(t).show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName())).show();
}
});
}
});
};
syncLiveData.observe(this, syncObserver);
});
tracker = ItemSelectionTracker.build(listView, adapter);
adapter.setTracker(tracker);
tracker.addObserver(new SelectionTracker.SelectionObserver<Long>() {
@Override
public void onSelectionChanged() {
super.onSelectionChanged();
if (tracker.hasSelection() && mActionMode == null) {
mActionMode = startSupportActionMode(new MultiSelectedActionModeCallback(MainActivity.this, coordinatorLayout, mainViewModel, MainActivity.this, canMoveNoteToAnotherAccounts, tracker, getSupportFragmentManager()));
}
if (mActionMode != null) {
if (tracker.hasSelection()) {
int selected = tracker.getSelection().size();
mActionMode.setTitle(getResources().getQuantityString(R.plurals.ab_selected, selected, selected));
} else {
mActionMode.finish();
mActionMode = null;
}
}
}
});
itemTouchHelper = new NotesListViewItemTouchHelper(this, mainViewModel, this, tracker, adapter, swipeRefreshLayout, coordinatorLayout, gridView);
itemTouchHelper.attachToRecyclerView(listView);
}
Aggregations