GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat: add a simple pagination example · alanwgeorge/Android-RxJava@c847664

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • app/src/main
    • java/com/morihacky/android/rxjava
      • fragments
        • MainFragment.java
      • pagination
        • PaginationFragment.java
    • res
      • layout
        • fragment_main.xml
        • fragment_pagination.xml
        • item_btn.xml
        • item_log.xml
      • values
        • strings.xml
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎app/src/main/java/com/morihacky/android/rxjava/fragments/MainFragment.java‎

Copy file name to clipboardExpand all lines: app/src/main/java/com/morihacky/android/rxjava/fragments/MainFragment.java
+8-2Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
import android.view.LayoutInflater;
88
import android.view.View;
99
import android.view.ViewGroup;
10+
1011
import com.morihacky.android.rxjava.R;
12+
import com.morihacky.android.rxjava.pagination.PaginationFragment;
1113
import com.morihacky.android.rxjava.rxbus.RxBusDemoFragment;
14+
import com.morihacky.android.rxjava.volley.VolleyDemoFragment;
1215

1316
import butterknife.ButterKnife;
1417
import butterknife.OnClick;
1518

16-
import com.morihacky.android.rxjava.volley.VolleyDemoFragment;
17-
1819
public class MainFragment
1920
extends BaseFragment {
2021

@@ -94,6 +95,11 @@ void demoRotationPersist() {
9495
//clickedOn(new RotationPersist1Fragment());
9596
}
9697

98+
@OnClick(R.id.btn_demo_pagination)
99+
void demoPaging() {
100+
clickedOn(new PaginationFragment());
101+
}
102+
97103
@OnClick(R.id.btn_demo_volley)
98104
void demoVolleyRequest() {
99105
clickedOn(new VolleyDemoFragment());
Collapse file

‎app/src/main/java/com/morihacky/android/rxjava/pagination/PaginationFragment.java‎

Copy file name to clipboard
+215Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
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+
}
Collapse file

‎app/src/main/res/layout/fragment_main.xml‎

Copy file name to clipboardExpand all lines: app/src/main/res/layout/fragment_main.xml
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,19 @@
9999
android:layout_width="match_parent"
100100
android:text="@string/btn_demo_rotation_persist"
101101
/>
102+
102103
<Button
103104
android:id="@+id/btn_demo_volley"
104105
android:layout_height="wrap_content"
105106
android:layout_width="match_parent"
106107
android:text="@string/btn_demo_volley"
107108
/>
109+
110+
<Button
111+
android:id="@+id/btn_demo_pagination"
112+
android:layout_height="wrap_content"
113+
android:layout_width="match_parent"
114+
android:text="@string/btn_demo_pagination"
115+
/>
108116
</LinearLayout>
109117
</ScrollView>
Collapse file

‎app/src/main/res/layout/fragment_pagination.xml‎

Copy file name to clipboard
+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<LinearLayout
4+
xmlns:android="http://schemas.android.com/apk/res/android"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
android:orientation="vertical"
8+
>
9+
10+
<TextView
11+
android:layout_width="match_parent"
12+
android:layout_height="wrap_content"
13+
android:padding="@dimen/activity_horizontal_margin"
14+
android:text="@string/msg_demo_pagination"
15+
/>
16+
17+
<ProgressBar
18+
android:id="@+id/progress_paging"
19+
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
20+
android:layout_width="match_parent"
21+
android:layout_height="wrap_content"
22+
android:indeterminate="true"
23+
android:visibility="invisible"
24+
/>
25+
26+
<android.support.v7.widget.RecyclerView
27+
android:id="@+id/list_paging"
28+
android:layout_width="match_parent"
29+
android:layout_height="match_parent"
30+
/>
31+
32+
</LinearLayout>
Collapse file

‎app/src/main/res/layout/item_btn.xml‎

Copy file name to clipboard
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<Button
4+
android:id="@+id/item_btn"
5+
xmlns:android="http://schemas.android.com/apk/res/android"
6+
7+
android:layout_width="match_parent"
8+
android:layout_height="wrap_content"
9+
android:layout_margin="@dimen/activity_horizontal_margin"
10+
11+
android:text="I AMZ BTN"
12+
/>
Collapse file

‎app/src/main/res/layout/item_log.xml‎

Copy file name to clipboardExpand all lines: app/src/main/res/layout/item_log.xml
+8-3Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22

33
<TextView
44
android:id="@+id/item_log"
5-
android:layout_height="match_parent"
5+
xmlns:android="http://schemas.android.com/apk/res/android"
6+
67
android:layout_width="match_parent"
7-
android:textSize="12sp"
8+
android:layout_height="wrap_content"
9+
android:paddingEnd="@dimen/activity_horizontal_margin"
10+
android:paddingLeft="@dimen/activity_horizontal_margin"
11+
android:paddingRight="@dimen/activity_horizontal_margin"
12+
android:paddingStart="@dimen/activity_horizontal_margin"
813
android:text="Item Log goes here"
9-
xmlns:android="http://schemas.android.com/apk/res/android"
14+
android:textSize="@dimen/text_medium"
1015
/>
Collapse file

‎app/src/main/res/values/strings.xml‎

Copy file name to clipboardExpand all lines: app/src/main/res/values/strings.xml
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
<string name="btn_demo_exponential_backoff">Exponential backoff</string>
2121
<string name="btn_demo_rotation_persist">Rotation persist</string>
2222
<string name="btn_demo_volley">Volley request demo</string>
23+
<string name="btn_demo_pagination">Paging example</string>
24+
<string name="btn_demo_pagination_more">MOAR</string>
25+
<string name="msg_demo_pagination">This is a demo of how you can do a list pagination with Rx. We page 10 items at a time and there are 55 items altogether</string>
2326
<string name="msg_demo_volley">This is a Volley request demo</string>
2427
<string name="msg_demo_concurrency_schedulers">This is a demo of how long running operations can be offloaded to a background thread. After the operation is done, we resume back on the main thread. All using RxJava! \n\n To really see this shine. Hit the button multiple times and see how the button click which is a ui operation is never blocked because the long operation only runs in the background</string>
2528
<string name="msg_demo_buffer">This is a demo of how events can be accumulated using the "buffer" operation. Tap the button below repetitively and you will notice in the logs that button taps are collected over a span of 2s and printed below.</string>

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.