|
| 1 | +package com.morihacky.android.rxjava.pagination; |
| 2 | + |
| 3 | +import android.os.Bundle; |
| 4 | +import android.support.annotation.Nullable; |
| 5 | +import android.support.v7.widget.LinearLayoutManager; |
| 6 | +import android.support.v7.widget.RecyclerView; |
| 7 | +import android.view.LayoutInflater; |
| 8 | +import android.view.View; |
| 9 | +import android.view.ViewGroup; |
| 10 | +import android.widget.Button; |
| 11 | +import android.widget.ProgressBar; |
| 12 | +import android.widget.TextView; |
| 13 | + |
| 14 | +import com.morihacky.android.rxjava.MainActivity; |
| 15 | +import com.morihacky.android.rxjava.R; |
| 16 | +import com.morihacky.android.rxjava.fragments.BaseFragment; |
| 17 | +import com.morihacky.android.rxjava.rxbus.RxBus; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | +import java.util.concurrent.TimeUnit; |
| 22 | + |
| 23 | +import butterknife.Bind; |
| 24 | +import butterknife.ButterKnife; |
| 25 | +import rx.Observable; |
| 26 | +import rx.Subscription; |
| 27 | +import rx.android.schedulers.AndroidSchedulers; |
| 28 | +import rx.subjects.PublishSubject; |
| 29 | +import rx.subscriptions.CompositeSubscription; |
| 30 | + |
| 31 | +public class PaginationFragment extends BaseFragment { |
| 32 | + |
| 33 | + @Bind(R.id.list_paging) RecyclerView _pagingList; |
| 34 | + @Bind(R.id.progress_paging) ProgressBar _progressBar; |
| 35 | + |
| 36 | + private CompositeSubscription _subscriptions; |
| 37 | + private TestAdapter _adapter; |
| 38 | + private RxBus _bus; |
| 39 | + private PublishSubject<Integer> _paginator; |
| 40 | + |
| 41 | + @Override |
| 42 | + public void onActivityCreated(@Nullable Bundle savedInstanceState) { |
| 43 | + super.onActivityCreated(savedInstanceState); |
| 44 | + |
| 45 | + _bus = ((MainActivity) getActivity()).getRxBusSingleton(); |
| 46 | + |
| 47 | + LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); |
| 48 | + layoutManager.setOrientation(LinearLayoutManager.VERTICAL); |
| 49 | + _pagingList.setLayoutManager(layoutManager); |
| 50 | + |
| 51 | + _adapter = new TestAdapter(_bus); |
| 52 | + _pagingList.setAdapter(_adapter); |
| 53 | + |
| 54 | + _paginator = PublishSubject.create(); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void onStart() { |
| 59 | + super.onStart(); |
| 60 | + _subscriptions = new CompositeSubscription(); |
| 61 | + |
| 62 | + Subscription s2 =// |
| 63 | + _paginator// |
| 64 | + .flatMap(nextPage -> _itemsFromNetworkCall(nextPage + 1, 10))// |
| 65 | + .observeOn(AndroidSchedulers.mainThread()) |
| 66 | + .map(items -> { |
| 67 | + int start = _adapter.getItemCount() - 1; |
| 68 | + |
| 69 | + _adapter.addItems(items); |
| 70 | + _adapter.notifyItemRangeInserted(start, 10); |
| 71 | + |
| 72 | + _progressBar.setVisibility(View.INVISIBLE); |
| 73 | + return null; |
| 74 | + })// |
| 75 | + .subscribe(); |
| 76 | + |
| 77 | + // I'm using an Rxbus purely to hear from a nested button click |
| 78 | + // we don't really need Rx for this part. it's just easy ¯\_(ツ)_/¯ |
| 79 | + Subscription s1 = _bus.toObserverable().subscribe(event -> { |
| 80 | + if (event instanceof ItemBtnViewHolder.PageEvent) { |
| 81 | + |
| 82 | + // trigger the paginator for the next event |
| 83 | + int nextPage = _adapter.getItemCount() - 1; |
| 84 | + _paginator.onNext(nextPage); |
| 85 | + |
| 86 | + } |
| 87 | + }); |
| 88 | + |
| 89 | + _subscriptions.add(s1); |
| 90 | + _subscriptions.add(s2); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public void onStop() { |
| 95 | + super.onStop(); |
| 96 | + _subscriptions.clear(); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Fake Observable that simulates a network call and then sends down a list of items |
| 101 | + */ |
| 102 | + private Observable<List<String>> _itemsFromNetworkCall(int start, int count) { |
| 103 | + return Observable.just(true) |
| 104 | + .observeOn(AndroidSchedulers.mainThread()) |
| 105 | + .doOnNext(dummy -> _progressBar.setVisibility(View.VISIBLE)) |
| 106 | + .delay(2, TimeUnit.SECONDS) |
| 107 | + .map(dummy -> { |
| 108 | + List<String> items = new ArrayList<>(); |
| 109 | + for (int i = 0; i < count; i++) { |
| 110 | + items.add("Item " + (start + i)); |
| 111 | + } |
| 112 | + return items; |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | + // ----------------------------------------------------------------------------------- |
| 118 | + // WIRING up the views required for this example |
| 119 | + |
| 120 | + @Override |
| 121 | + public View onCreateView(LayoutInflater inflater, |
| 122 | + @Nullable ViewGroup container, |
| 123 | + @Nullable Bundle savedInstanceState) { |
| 124 | + View layout = inflater.inflate(R.layout.fragment_pagination, container, false); |
| 125 | + ButterKnife.bind(this, layout); |
| 126 | + return layout; |
| 127 | + } |
| 128 | + |
| 129 | + |
| 130 | + private static class TestAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { |
| 131 | + |
| 132 | + static final int ITEM_LOG = 0; |
| 133 | + static final int ITEM_BTN = 1; |
| 134 | + |
| 135 | + final List<String> _items = new ArrayList<>(); |
| 136 | + final RxBus _bus; |
| 137 | + |
| 138 | + TestAdapter(RxBus bus) { |
| 139 | + _bus = bus; |
| 140 | + } |
| 141 | + |
| 142 | + void addItems(List<String> items) { |
| 143 | + _items.addAll(items); |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public int getItemViewType(int position) { |
| 148 | + if (position == _items.size()) { |
| 149 | + return ITEM_BTN; |
| 150 | + } |
| 151 | + |
| 152 | + return ITEM_LOG; |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
| 157 | + switch (viewType) { |
| 158 | + case ITEM_BTN: |
| 159 | + return ItemBtnViewHolder.create(parent); |
| 160 | + default: |
| 161 | + return ItemLogViewHolder.create(parent); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { |
| 167 | + switch (getItemViewType(position)) { |
| 168 | + case ITEM_LOG: |
| 169 | + ((ItemLogViewHolder) holder).bindContent(_items.get(position)); |
| 170 | + return; |
| 171 | + case ITEM_BTN: |
| 172 | + ((ItemBtnViewHolder) holder).bindContent(_bus); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + public int getItemCount() { |
| 178 | + return _items.size() + 1; // add 1 for paging button |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + private static class ItemLogViewHolder extends RecyclerView.ViewHolder { |
| 183 | + ItemLogViewHolder(View itemView) { |
| 184 | + super(itemView); |
| 185 | + } |
| 186 | + |
| 187 | + static ItemLogViewHolder create(ViewGroup parent) { |
| 188 | + return new ItemLogViewHolder(LayoutInflater.from(parent.getContext()) |
| 189 | + .inflate(R.layout.item_log, parent, false)); |
| 190 | + } |
| 191 | + |
| 192 | + void bindContent(String content) { |
| 193 | + ((TextView) itemView).setText(content); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + private static class ItemBtnViewHolder extends RecyclerView.ViewHolder { |
| 198 | + ItemBtnViewHolder(View itemView) { |
| 199 | + super(itemView); |
| 200 | + } |
| 201 | + |
| 202 | + static ItemBtnViewHolder create(ViewGroup parent) { |
| 203 | + return new ItemBtnViewHolder(LayoutInflater.from(parent.getContext()) |
| 204 | + .inflate(R.layout.item_btn, parent, false)); |
| 205 | + } |
| 206 | + |
| 207 | + void bindContent(RxBus bus) { |
| 208 | + ((Button) itemView).setText(R.string.btn_demo_pagination_more); |
| 209 | + itemView.setOnClickListener(v -> bus.send(new PageEvent())); |
| 210 | + } |
| 211 | + |
| 212 | + static class PageEvent { |
| 213 | + } |
| 214 | + } |
| 215 | +} |