|
| 1 | +package com.firebase.uidemo.database.realtime; |
| 2 | + |
| 3 | +import android.arch.paging.PagedList; |
| 4 | +import android.os.Bundle; |
| 5 | +import android.support.annotation.NonNull; |
| 6 | +import android.support.annotation.Nullable; |
| 7 | +import android.support.v4.widget.SwipeRefreshLayout; |
| 8 | +import android.support.v7.app.AppCompatActivity; |
| 9 | +import android.support.v7.widget.LinearLayoutManager; |
| 10 | +import android.support.v7.widget.RecyclerView; |
| 11 | +import android.util.Log; |
| 12 | +import android.view.LayoutInflater; |
| 13 | +import android.view.View; |
| 14 | +import android.view.ViewGroup; |
| 15 | +import android.widget.TextView; |
| 16 | +import android.widget.Toast; |
| 17 | + |
| 18 | +import com.firebase.ui.database.paging.DatabasePagingOptions; |
| 19 | +import com.firebase.ui.database.paging.FirebaseRecyclerPagingAdapter; |
| 20 | +import com.firebase.ui.database.paging.LoadingState; |
| 21 | +import com.firebase.uidemo.R; |
| 22 | +import com.google.firebase.database.DatabaseError; |
| 23 | +import com.google.firebase.database.FirebaseDatabase; |
| 24 | +import com.google.firebase.database.Query; |
| 25 | + |
| 26 | +import butterknife.BindView; |
| 27 | +import butterknife.ButterKnife; |
| 28 | + |
| 29 | +public class FirebaseDbPagingActivity extends AppCompatActivity { |
| 30 | + |
| 31 | + private final String TAG = "PagingActivity"; |
| 32 | + |
| 33 | + @BindView(R.id.paging_recycler) |
| 34 | + RecyclerView mRecycler; |
| 35 | + |
| 36 | + @BindView(R.id.swipe_refresh_layout) |
| 37 | + SwipeRefreshLayout mSwipeRefreshLayout; |
| 38 | + |
| 39 | + private Query mQuery; |
| 40 | + |
| 41 | + @Override |
| 42 | + protected void onCreate(@Nullable Bundle savedInstanceState) { |
| 43 | + super.onCreate(savedInstanceState); |
| 44 | + setContentView(R.layout.activity_database_paging); |
| 45 | + ButterKnife.bind(this); |
| 46 | + |
| 47 | + mQuery = FirebaseDatabase.getInstance().getReference().child("posts"); |
| 48 | + |
| 49 | + setUpAdapter(); |
| 50 | + } |
| 51 | + |
| 52 | + private void setUpAdapter() { |
| 53 | + |
| 54 | + //Initialize Paging Configurations |
| 55 | + PagedList.Config config = new PagedList.Config.Builder() |
| 56 | + .setEnablePlaceholders(false) |
| 57 | + .setPrefetchDistance(5) |
| 58 | + .setPageSize(30) |
| 59 | + .build(); |
| 60 | + |
| 61 | + //Initialize Firebase Paging Options |
| 62 | + DatabasePagingOptions<Post> options = new DatabasePagingOptions.Builder<Post>() |
| 63 | + .setLifecycleOwner(this) |
| 64 | + .setQuery(mQuery, config, Post.class) |
| 65 | + .build(); |
| 66 | + |
| 67 | + //Initializing Adapter |
| 68 | + final FirebaseRecyclerPagingAdapter<Post, PostViewHolder> mAdapter = |
| 69 | + new FirebaseRecyclerPagingAdapter<Post, PostViewHolder>(options) { |
| 70 | + @NonNull |
| 71 | + @Override |
| 72 | + public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, |
| 73 | + int viewType) { |
| 74 | + View view = LayoutInflater.from(parent.getContext()) |
| 75 | + .inflate(R.layout.item_post, parent, false); |
| 76 | + return new PostViewHolder(view); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + protected void onBindViewHolder(@NonNull PostViewHolder holder, |
| 81 | + int position, |
| 82 | + @NonNull Post model) { |
| 83 | + holder.bind(model); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + protected void onLoadingStateChanged(@NonNull LoadingState state) { |
| 88 | + switch (state) { |
| 89 | + case LOADING_INITIAL: |
| 90 | + case LOADING_MORE: |
| 91 | + mSwipeRefreshLayout.setRefreshing(true); |
| 92 | + break; |
| 93 | + |
| 94 | + case LOADED: |
| 95 | + mSwipeRefreshLayout.setRefreshing(false); |
| 96 | + break; |
| 97 | + |
| 98 | + case FINISHED: |
| 99 | + mSwipeRefreshLayout.setRefreshing(false); |
| 100 | + Toast.makeText(getApplicationContext(), getString(R.string.paging_finished_message), Toast.LENGTH_SHORT).show(); |
| 101 | + break; |
| 102 | + |
| 103 | + case ERROR: |
| 104 | + retry(); |
| 105 | + break; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + protected void onError(DatabaseError databaseError) { |
| 111 | + mSwipeRefreshLayout.setRefreshing(false); |
| 112 | + Log.e(TAG, databaseError.getMessage()); |
| 113 | + } |
| 114 | + }; |
| 115 | + |
| 116 | + mRecycler.setLayoutManager(new LinearLayoutManager(this)); |
| 117 | + mRecycler.setAdapter(mAdapter); |
| 118 | + |
| 119 | + // Reload data on swipe |
| 120 | + mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { |
| 121 | + @Override |
| 122 | + public void onRefresh() { |
| 123 | + //Reload Data |
| 124 | + mAdapter.refresh(); |
| 125 | + } |
| 126 | + }); |
| 127 | + } |
| 128 | + |
| 129 | + public static class Post { |
| 130 | + |
| 131 | + @Nullable public String title; |
| 132 | + @Nullable public String body; |
| 133 | + |
| 134 | + public Post(){} |
| 135 | + |
| 136 | + public Post(@Nullable String title, @Nullable String body) { |
| 137 | + this.title = title; |
| 138 | + this.body = body; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + public static class PostViewHolder extends RecyclerView.ViewHolder { |
| 143 | + |
| 144 | + @BindView(R.id.textViewTitle) |
| 145 | + TextView mTitleView; |
| 146 | + |
| 147 | + @BindView(R.id.textViewBody) |
| 148 | + TextView mBodyView; |
| 149 | + |
| 150 | + PostViewHolder(@NonNull View itemView) { |
| 151 | + super(itemView); |
| 152 | + ButterKnife.bind(this, itemView); |
| 153 | + } |
| 154 | + |
| 155 | + void bind(@NonNull Post post) { |
| 156 | + mTitleView.setText(post.title); |
| 157 | + mBodyView.setText(post.body); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | +} |
0 commit comments