use of androidx.paging.PagingConfig in project FirebaseUI-Android by firebase.
the class FirebaseDbPagingActivity method setUpAdapter.
private void setUpAdapter() {
// Initialize Paging Configurations
PagingConfig config = new PagingConfig(30, 5, false);
// Initialize Firebase Paging Options
DatabasePagingOptions<Item> options = new DatabasePagingOptions.Builder<Item>().setLifecycleOwner(this).setQuery(mQuery, config, Item.class).build();
// Initializing Adapter
final FirebaseRecyclerPagingAdapter<Item, ItemViewHolder> mAdapter = new FirebaseRecyclerPagingAdapter<Item, ItemViewHolder>(options) {
@NonNull
@Override
public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_item, parent, false);
return new ItemViewHolder(view);
}
@Override
protected void onBindViewHolder(@NonNull ItemViewHolder holder, int position, @NonNull Item model) {
holder.bind(model);
}
};
mAdapter.addLoadStateListener(states -> {
LoadState refresh = states.getRefresh();
LoadState append = states.getAppend();
if (refresh instanceof LoadState.Error) {
LoadState.Error loadStateError = (LoadState.Error) refresh;
mBinding.swipeRefreshLayout.setRefreshing(false);
Log.e(TAG, loadStateError.getError().getLocalizedMessage());
}
if (append instanceof LoadState.Error) {
LoadState.Error loadStateError = (LoadState.Error) append;
mBinding.swipeRefreshLayout.setRefreshing(false);
Log.e(TAG, loadStateError.getError().getLocalizedMessage());
}
if (append instanceof LoadState.Loading) {
mBinding.swipeRefreshLayout.setRefreshing(true);
}
if (append instanceof LoadState.NotLoading) {
LoadState.NotLoading notLoading = (LoadState.NotLoading) append;
if (notLoading.getEndOfPaginationReached()) {
// This indicates that the user has scrolled
// until the end of the data set.
mBinding.swipeRefreshLayout.setRefreshing(false);
showToast("Reached end of data set.");
return null;
}
if (refresh instanceof LoadState.NotLoading) {
// This indicates the most recent load
// has finished.
mBinding.swipeRefreshLayout.setRefreshing(false);
return null;
}
}
return null;
});
mBinding.pagingRecycler.setLayoutManager(new LinearLayoutManager(this));
mBinding.pagingRecycler.setAdapter(mAdapter);
// Reload data on swipe
mBinding.swipeRefreshLayout.setOnRefreshListener(() -> {
// Reload Data
mAdapter.refresh();
});
}
use of androidx.paging.PagingConfig in project FirebaseUI-Android by firebase.
the class FirestorePagingActivity method setUpAdapter.
private void setUpAdapter() {
Query baseQuery = mItemsCollection.orderBy("value", Query.Direction.ASCENDING);
PagingConfig config = new PagingConfig(20, 10, false);
FirestorePagingOptions<Item> options = new FirestorePagingOptions.Builder<Item>().setLifecycleOwner(this).setQuery(baseQuery, config, Item.class).build();
final FirestorePagingAdapter<Item, ItemViewHolder> adapter = new FirestorePagingAdapter<Item, ItemViewHolder>(options) {
@NonNull
@Override
public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_item, parent, false);
return new ItemViewHolder(view);
}
@Override
protected void onBindViewHolder(@NonNull ItemViewHolder holder, int position, @NonNull Item model) {
holder.bind(model);
}
};
adapter.addLoadStateListener(states -> {
LoadState refresh = states.getRefresh();
LoadState append = states.getAppend();
if (refresh instanceof LoadState.Error || append instanceof LoadState.Error) {
showToast("An error occurred.");
adapter.retry();
}
if (append instanceof LoadState.Loading) {
mBinding.swipeRefreshLayout.setRefreshing(true);
}
if (append instanceof LoadState.NotLoading) {
LoadState.NotLoading notLoading = (LoadState.NotLoading) append;
if (notLoading.getEndOfPaginationReached()) {
// This indicates that the user has scrolled
// until the end of the data set.
mBinding.swipeRefreshLayout.setRefreshing(false);
showToast("Reached end of data set.");
return null;
}
if (refresh instanceof LoadState.NotLoading) {
// This indicates the most recent load
// has finished.
mBinding.swipeRefreshLayout.setRefreshing(false);
return null;
}
}
return null;
});
mBinding.pagingRecycler.setLayoutManager(new LinearLayoutManager(this));
mBinding.pagingRecycler.setAdapter(adapter);
mBinding.swipeRefreshLayout.setOnRefreshListener(() -> adapter.refresh());
}
Aggregations