Skip to content

Instantly share code, notes, and snippets.

View PatilShreyas's full-sized avatar
👨‍💻
Might be writing code at the moment

Shreyas Patil PatilShreyas

👨‍💻
Might be writing code at the moment
View GitHub Profile
public class Post {
public String authorName;
public String message;
public Post() {
}
public Post(String authorName, String message) {
this.authorName = authorName;
this.message = message;
// Init Paging Configuration
PagedList.Config config = new PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setPrefetchDistance(2)
.setPageSize(10)
.build();
// Init Adapter Configuration
FirestorePagingOptions options = new FirestorePagingOptions.Builder<Post>()
.setLifecycleOwner(this)
.setQuery(mQuery, config, Post.class)
.build();
// Instantiate Paging Adapter
mAdapter = new FirestorePagingAdapter<Post, PostViewHolder>(options) {
@NonNull
@Override
public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = getLayoutInflater().inflate(R.layout.item_post, parent, false);
return new PostViewHolder(view);
}
@Override
// Instantiate Paging Adapter
mAdapter = object : FirestorePagingAdapter<Post, PostViewHolder>(options) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostViewHolder {
val view = layoutInflater.inflate(R.layout.item_post, parent, false)
return PostViewHolder(view)
}
override fun onBindViewHolder(viewHolder: PostViewHolder, position: Int, post: Post) {
// Bind to ViewHolder
viewHolder.bind(post)
@Override
protected void onError(@NonNull Exception e) {
super.onError(e);
Log.e("MainActivity", e.getMessage());
// Handle the error.
}
// Refresh Action on Swipe Refresh Layout
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
mAdapter.refresh();
}
});
@Override
protected void onStart() {
super.onStart();
mAdapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
mAdapter.stopListening();
}
public class PostViewHolder extends RecyclerView.ViewHolder {
private TextView authorView;
private TextView messageView;
public PostViewHolder(@NonNull View itemView) {
super(itemView);
authorView = itemView.findViewById(R.id.post_AuthorName);
messageView = itemView.findViewById(R.id.post_Message);
}