Search in sources :

Example 1 with DatabasePagingOptions

use of com.firebase.ui.database.paging.DatabasePagingOptions 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();
    });
}
Also used : ViewGroup(android.view.ViewGroup) DatabasePagingOptions(com.firebase.ui.database.paging.DatabasePagingOptions) FirebaseRecyclerPagingAdapter(com.firebase.ui.database.paging.FirebaseRecyclerPagingAdapter) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) TextView(android.widget.TextView) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) MenuItem(android.view.MenuItem) 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 DatabasePagingOptions (com.firebase.ui.database.paging.DatabasePagingOptions)1 FirebaseRecyclerPagingAdapter (com.firebase.ui.database.paging.FirebaseRecyclerPagingAdapter)1