Skip to content

Commit 3aed368

Browse files
changed sample to show how to hide the fast scroller in case the recyclerView already shows all items.
1 parent b120518 commit 3aed368

File tree

5 files changed

+42
-16
lines changed

5 files changed

+42
-16
lines changed

app/app.iml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@
9292
</content>
9393
<orderEntry type="jdk" jdkName="Android API 23 Platform" jdkType="Android SDK" />
9494
<orderEntry type="sourceFolder" forTests="false" />
95+
<orderEntry type="library" exported="" name="recyclerview-v7-23.0.1" level="project" />
96+
<orderEntry type="library" exported="" name="support-v4-23.0.1" level="project" />
9597
<orderEntry type="library" exported="" name="design-23.0.1" level="project" />
9698
<orderEntry type="library" exported="" name="appcompat-v7-23.0.1" level="project" />
97-
<orderEntry type="library" exported="" name="support-v4-23.0.1" level="project" />
98-
<orderEntry type="library" exported="" name="recyclerview-v7-23.0.1" level="project" />
9999
<orderEntry type="library" exported="" name="support-annotations-23.0.1" level="project" />
100100
<orderEntry type="module" module-name="library" exported="" />
101101
</component>

app/src/main/java/com/lb/recyclerview_fast_scroller/LargeAdapter.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@
1212
import java.util.List;
1313

1414
public final class LargeAdapter extends RecyclerView.Adapter<LargeAdapter.ViewHolder> implements BubbleTextGetter {
15-
private static final int SIZE = 1000;
1615
private final List<String> items;
1716

18-
public LargeAdapter() {
17+
public LargeAdapter(int numberOfItems) {
1918
List<String> items = new ArrayList<>();
2019
java.util.Random r = new java.util.Random();
21-
for (int i = 0; i < SIZE; i++)
20+
for (int i = 0; i < numberOfItems; i++)
2221
items.add(((char) ('A' + r.nextInt('Z' - 'A'))) + " " + Integer.toString(i));
2322
java.util.Collections.sort(items);
2423
this.items = items;

app/src/main/java/com/lb/recyclerview_fast_scroller/MainActivity.java

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,30 +91,57 @@ public DesignDemoPagerAdapter(FragmentManager fm) {
9191

9292
@Override
9393
public Fragment getItem(int position) {
94-
return new RecyclerViewFragment();
94+
final RecyclerViewFragment recyclerViewFragment = new RecyclerViewFragment();
95+
recyclerViewFragment.numberOfItems = getFragmentItemsCount(position);
96+
return recyclerViewFragment;
97+
}
98+
99+
private int getFragmentItemsCount(int pos) {
100+
return (int) Math.pow(4, (getCount() - pos));
95101
}
96102

97103
@Override
98104
public int getCount() {
99-
return 3;
105+
return 5;
100106
}
101107

102108
@Override
103109
public CharSequence getPageTitle(int position) {
104-
return "Tab " + position;
110+
return "itemsCount: " + getFragmentItemsCount(position);
105111
}
106112
}
107113

108114

109115
public static class RecyclerViewFragment extends Fragment {
116+
public int numberOfItems;
117+
110118
@Nullable
111119
@Override
112120
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
113121
View rootView = inflater.inflate(R.layout.fragment_recycler_view, container, false);
114122
RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview);
115-
recyclerView.setAdapter(new LargeAdapter());
116-
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
117-
RecyclerViewFastScroller fastScroller = (RecyclerViewFastScroller) rootView.findViewById(R.id.fastscroller);
123+
final LargeAdapter adapter = new LargeAdapter(numberOfItems);
124+
recyclerView.setAdapter(adapter);
125+
final RecyclerViewFastScroller fastScroller = (RecyclerViewFastScroller) rootView.findViewById(R.id.fastscroller);
126+
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false) {
127+
@Override
128+
public void onLayoutChildren(final RecyclerView.Recycler recycler, final RecyclerView.State state) {
129+
super.onLayoutChildren(recycler, state);
130+
//TODO if the items are filtered, considered hiding the fast scroller here
131+
final int firstVisibleItemPosition = findFirstVisibleItemPosition();
132+
if (firstVisibleItemPosition != 0) {
133+
// this avoids trying to handle un-needed calls
134+
if (firstVisibleItemPosition == -1)
135+
//not initialized, or no items shown, so hide fast-scroller
136+
fastScroller.setVisibility(View.GONE);
137+
return;
138+
}
139+
final int lastVisibleItemPosition = findLastVisibleItemPosition();
140+
int itemsShown = lastVisibleItemPosition - firstVisibleItemPosition + 1;
141+
//if all items are shown, hide the fast-scroller
142+
fastScroller.setVisibility(adapter.getItemCount() > itemsShown ? View.VISIBLE : View.GONE);
143+
}
144+
});
118145
fastScroller.setRecyclerView(recyclerView);
119146
fastScroller.setViewsToUse(R.layout.recycler_view_fast_scroller__fast_scroller, R.id.fastscroller_bubble, R.id.fastscroller_handle);
120147
return rootView;

app/src/main/res/layout/activity_main.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
app:layout_scrollFlags="scroll|enterAlways"/>
1919

2020
<android.support.design.widget.TabLayout
21-
android:id="@+id/tablayout"
21+
android:id="@+id/tablayout" app:tabMode="scrollable"
2222
android:layout_width="match_parent"
23-
android:layout_height="wrap_content"
23+
android:layout_height="wrap_content" app:tabGravity="center"
2424
android:background="?attr/colorPrimary"
25-
app:tabGravity="fill"/>
25+
/>
2626

2727
</android.support.design.widget.AppBarLayout>
2828

library/library.iml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<option name="ALLOW_USER_CONFIGURATION" value="false" />
2222
<option name="MANIFEST_FILE_RELATIVE_PATH" value="/src/main/AndroidManifest.xml" />
2323
<option name="RES_FOLDER_RELATIVE_PATH" value="/src/main/res" />
24-
<option name="RES_FOLDERS_RELATIVE_PATH" value="file://$MODULE_DIR$/src/main/res" />
24+
<option name="RES_FOLDERS_RELATIVE_PATH" value="" />
2525
<option name="ASSETS_FOLDER_RELATIVE_PATH" value="/src/main/assets" />
2626
<option name="LIBRARY_PROJECT" value="true" />
2727
</configuration>
@@ -89,9 +89,9 @@
8989
</content>
9090
<orderEntry type="jdk" jdkName="Android API 23 Platform" jdkType="Android SDK" />
9191
<orderEntry type="sourceFolder" forTests="false" />
92-
<orderEntry type="library" exported="" name="appcompat-v7-23.0.1" level="project" />
9392
<orderEntry type="library" exported="" name="recyclerview-v7-23.0.1" level="project" />
9493
<orderEntry type="library" exported="" name="support-v4-23.0.1" level="project" />
94+
<orderEntry type="library" exported="" name="appcompat-v7-23.0.1" level="project" />
9595
<orderEntry type="library" exported="" name="support-annotations-23.0.1" level="project" />
9696
</component>
9797
</module>

0 commit comments

Comments
 (0)