0

I'm trying to implement pagination in a recyclerview. My code works fine without pagination but I can't seem to figure out how to start from here.. Below is my current code:

AuthorRepository

public interface AuthorRepository {
    LiveData<List<Author>> findAll();
}

IAuthorRepository

public class IAuthorRepository implements AuthorRepository {

    private final FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
    private final CollectionReference collectionReference = firebaseFirestore.collection("authors");

    private final MutableLiveData<List<Author>> listMutableLiveData = new MutableLiveData<>();

    @Override
    public LiveData<List<Author>> findAll() {
        collectionReference.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                listMutableLiveData.setValue(task.getResult().toObjects(Author.class));
            }
        });
        return listMutableLiveData;
    }
}

AuthorViewModel

public interface AuthorViewModel extends LifecycleObserver {
    LiveData<List<Author>> findAll();
}

IAuthorViewModel

public class IAuthorViewModel extends ViewModel implements AuthorViewModel {

    private final LiveData<List<Author>> authorListModelData;

    public IAuthorViewModel() {
        IAuthorRepository iAuthorRepository = new IAuthorRepository();
        authorListModelData = iAuthorRepository.findAll();
    }

    @Override
    public LiveData<List<Author>> findAll() {
        return authorListModelData;
    }
}

MainActivity

public class MainActivity extends AppCompatActivity {

    private IAuthorViewModel viewModel;
    private AuthorAdapter adapter;

    private final List<Author> authors = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
        getAuthors();
    }

    private void init() {
        ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        activityMainBinding.authorRecyclerview.setHasFixedSize(true);
        adapter = new AuthorAdapter(authors);
        activityMainBinding.authorRecyclerview.setAdapter(adapter);
        activityMainBinding.authorRecyclerview.setLayoutManager(new LinearLayoutManager(this));

        viewModel = ViewModelProviders.of(this).get(IAuthorViewModel.class);
    }

    private void getAuthors() {
        viewModel.findAll().observe(this, new Observer<List<Author>>() {
            @Override
            public void onChanged(List<Author> authorList) {
                authors.clear();
                authors.addAll(authorList);
                adapter.notifyDataSetChanged();
            }
        });
    }
}

This is just a small test project to try and get the hang of the MVVM principles. There's also not that much data that's being loaded but I prefer getting it working for this before I move on to bigger data sets.

Any push in the right direction would be greatly appreciated. I've looked at countless lines of code but nothing really explains it clearly..

Kind regards.

  • are you looking for https://firebaseopensource.com/projects/firebase/firebaseui-android/firestore/readme/ – Abhinav Chauhan Jan 11 '21 at 03:59
  • **[This](https://stackoverflow.com/questions/50741958/how-to-paginate-firestore-with-android)** is a recommended way in which you can paginate queries by combining query cursors with the limit() method. I also recommend you take a look at this **[video](https://www.youtube.com/watch?v=KdgKvLll07s)** for a better understanding. – Alex Mamo Jan 11 '21 at 07:10
  • Did you find the links shared to be useful? – Daniel Ocando Jan 11 '21 at 17:09

0 Answers0