Search in sources :

Example 1 with FirestorePagingAdapter

use of com.firebase.ui.firestore.paging.FirestorePagingAdapter 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());
}
Also used : FirestorePagingAdapter(com.firebase.ui.firestore.paging.FirestorePagingAdapter) Query(com.google.firebase.firestore.Query) ViewGroup(android.view.ViewGroup) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) TextView(android.widget.TextView) MenuItem(android.view.MenuItem) FirestorePagingOptions(com.firebase.ui.firestore.paging.FirestorePagingOptions) NonNull(androidx.annotation.NonNull) LoadState(androidx.paging.LoadState) PagingConfig(androidx.paging.PagingConfig)

Aggregations

MenuItem (android.view.MenuItem)1 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1 TextView (android.widget.TextView)1 NonNull (androidx.annotation.NonNull)1 LoadState (androidx.paging.LoadState)1 PagingConfig (androidx.paging.PagingConfig)1 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 FirestorePagingAdapter (com.firebase.ui.firestore.paging.FirestorePagingAdapter)1 FirestorePagingOptions (com.firebase.ui.firestore.paging.FirestorePagingOptions)1 Query (com.google.firebase.firestore.Query)1