|
| 1 | +package com.morihacky.android.rxjava.pagination; |
| 2 | + |
| 3 | +import android.support.v7.widget.RecyclerView; |
| 4 | +import android.view.LayoutInflater; |
| 5 | +import android.view.View; |
| 6 | +import android.view.ViewGroup; |
| 7 | +import android.widget.Button; |
| 8 | +import android.widget.TextView; |
| 9 | +import com.morihacky.android.rxjava.R; |
| 10 | +import com.morihacky.android.rxjava.rxbus.RxBus; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +class PaginationAutoAdapter |
| 15 | + extends RecyclerView.Adapter<RecyclerView.ViewHolder> { |
| 16 | + |
| 17 | + private static final int ITEM_LOG = 0; |
| 18 | + private static final int ITEM_BTN = 1; |
| 19 | + |
| 20 | + private final List<String> _items = new ArrayList<>(); |
| 21 | + private final RxBus _bus; |
| 22 | + |
| 23 | + PaginationAutoAdapter(RxBus bus) { |
| 24 | + _bus = bus; |
| 25 | + } |
| 26 | + |
| 27 | + void addItems(List<String> items) { |
| 28 | + _items.addAll(items); |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public int getItemViewType(int position) { |
| 33 | + if (position == _items.size()) { |
| 34 | + return ITEM_BTN; |
| 35 | + } |
| 36 | + |
| 37 | + return ITEM_LOG; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
| 42 | + switch (viewType) { |
| 43 | + case ITEM_BTN: |
| 44 | + return ItemBtnViewHolder.create(parent); |
| 45 | + default: |
| 46 | + return ItemLogViewHolder.create(parent); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { |
| 52 | + switch (getItemViewType(position)) { |
| 53 | + case ITEM_LOG: |
| 54 | + ((ItemLogViewHolder) holder).bindContent(_items.get(position)); |
| 55 | + return; |
| 56 | + case ITEM_BTN: |
| 57 | + ((ItemBtnViewHolder) holder).bindContent(_bus); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public int getItemCount() { |
| 63 | + return _items.size() + 1; // add 1 for paging button |
| 64 | + } |
| 65 | + |
| 66 | + private static class ItemLogViewHolder extends RecyclerView.ViewHolder { |
| 67 | + ItemLogViewHolder(View itemView) { |
| 68 | + super(itemView); |
| 69 | + } |
| 70 | + |
| 71 | + static ItemLogViewHolder create(ViewGroup parent) { |
| 72 | + return new ItemLogViewHolder(LayoutInflater.from(parent.getContext()) |
| 73 | + .inflate(R.layout.item_log, parent, false)); |
| 74 | + } |
| 75 | + |
| 76 | + void bindContent(String content) { |
| 77 | + ((TextView) itemView).setText(content); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + static class ItemBtnViewHolder extends RecyclerView.ViewHolder { |
| 82 | + ItemBtnViewHolder(View itemView) { |
| 83 | + super(itemView); |
| 84 | + } |
| 85 | + |
| 86 | + static ItemBtnViewHolder create(ViewGroup parent) { |
| 87 | + return new ItemBtnViewHolder(LayoutInflater.from(parent.getContext()) |
| 88 | + .inflate(R.layout.item_btn, parent, false)); |
| 89 | + } |
| 90 | + |
| 91 | + void bindContent(RxBus bus) { |
| 92 | + ((Button) itemView).setText(R.string.btn_demo_pagination_more); |
| 93 | + itemView.setOnClickListener(v -> bus.send(new ItemBtnViewHolder.PageEvent())); |
| 94 | + } |
| 95 | + |
| 96 | + static class PageEvent { |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments