Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 views
in Technique[技术] by (71.8m points)

java - Pagination of firestore data while scrolling in recyclerview

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...