This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies { | |
//RecyclerView | |
implementation 'com.android.support:recyclerview-v7:28.0.0' | |
//Firebase Database | |
implementation 'com.google.firebase:firebase-database:16.1.0' | |
implementation 'com.google.firebase:firebase-core:16.0.7' | |
//Firebase-UI Library |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Post { | |
public String title; | |
public String body; | |
public Post(){} | |
public Post(String title, String body) { | |
this.title = title; | |
this.body = body; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity { | |
private RecyclerView mRecyclerView; | |
private DatabaseReference mDatabase; | |
private SwipeRefreshLayout mSwipeRefreshLayout; | |
FirebaseRecyclerPagingAdapter<Post, PostViewHolder> mAdapter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mSwipeRefreshLayout = findViewById(R.id.swipe_refresh_layout); | |
//Initialize RecyclerView | |
mRecyclerView = findViewById(R.id.recycler_view); | |
mRecyclerView.setHasFixedSize(true); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PagedList.Config config = new PagedList.Config.Builder() | |
.setEnablePlaceholders(false) | |
.setPrefetchDistance(5) | |
.setPageSize(10) | |
.build(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DatabasePagingOptions<Post> options = new DatabasePagingOptions.Builder<Post>() | |
.setLifecycleOwner(this) | |
.setQuery(mDatabase, config, Post.class) | |
.build(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mAdapter = new FirebaseRecyclerPagingAdapter<Post, PostViewHolder>(options) { | |
@NonNull | |
@Override | |
public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { | |
return new PostViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, parent, false)); | |
} | |
@Override | |
protected void onBindViewHolder(@NonNull PostViewHolder holder, | |
int position, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
protected void onBindViewHolder(@NonNull PostViewHolder holder, | |
int position, | |
@NonNull Post model) { | |
DatabaseReference reference = getRef(position); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
protected void onError(@NonNull DatabaseError databaseError) { | |
mSwipeRefreshLayout.setRefreshing(false); | |
databaseError.toException().printStackTrace(); | |
// Handle Error | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
protected void onError(@NonNull DatabaseError databaseError) { | |
retry(); | |
} |
OlderNewer