Skip to content
Davide Steduto edited this page Jun 26, 2016 · 15 revisions

In this page

  • XML Configuration
  • Java initialization
  • Callbacks
  • Customization

XML Configuration

FastScroller needs an xml layout and Java initialization. The layout has to be placed at the end of the ViewHierarchy where the RecyclerView is also declared:

	...
	<android.support.v7.widget.RecyclerView
		.../>

	...
	<!-- FastScroller Layout must be at the end of ViewHierarchy
		 in order to be displayed at the top of every views -->
	<include layout="@layout/fast_scroller"/>

</FrameLayout>

layout/fast_scroller.xml

<eu.davidea.fastscroller.FastScroller
	xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:tools="http://schemas.android.com/tools"
	android:id="@+id/fast_scroller"
	android:layout_width="wrap_content"
	android:layout_height="match_parent"
	android:layout_centerHorizontal="true"
	android:layout_alignTop="@+id/swipeRefreshLayout"
	android:layout_alignBottom="@+id/swipeRefreshLayout"
	android:layout_alignParentEnd="true"
	android:layout_alignParentRight="true"
	android:visibility="gone"
	tools:visibility="visible"/>

Java initialization

In the creation of the Activity/Fragment call setFastScroller() after the Adapter has been attached to the RV:

//First, assign the Adapter to the RV
mRecyclerView.setAdapter(mAdapter);
//Add FastScroll to the RecyclerView
//stateChangeListener is optional and might be the Activity
mAdapter.setFastScroller((FastScroller) findViewById(R.id.fast_scroller),
		Utils.getColorAccent(context)[, stateChangeListener]);

Callbacks

A useful callback is triggered when the handle is dragged, the method onFastScrollerStateChange() is called. From the Activity, implement the callback method:

@Override
public void onFastScrollerStateChange(boolean scrolling) {
	//Example
	if (scrolling) hideFab();
	else showFab();
}

Instead, the text is built from the second callback. It has to be implemented/overridden from the inside of a CustomAdapter that extends FlexibleAdapter:

@Override
public String onCreateBubbleText(int position) {
	IFlexible iFlexible = getItem(position);
	return iFlexible.toString().substring(0,1).toUpperCase();
}

Customization

The following 3 methods give full control on the FastScroller instance:

/**
 * Change the visibility of the View
 */
public void toggleFastScroller();

/**
 * Returns true, if FastScroller is configured and shown, false otherwise
 */
public boolean isFastScrollerEnabled();

/**
 * Returns the FastScroller instance for more control on the settings.
 */
public FastScroller getFastScroller();

FastScroller is composed by:

  • drawable/fast_scroller_bubble.xml
  • drawable/fast_scroller_handle.xml
  • layout/library_fast_scroller_layout.xml

The Resources can be overridden by assigning the same @+id to the views of the base layout:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:tools="http://schemas.android.com/tools"
	android:layout_width="wrap_content"
	android:layout_height="match_parent">

	<View
		android:id="@+id/fast_scroller_bar"
		android:layout_width="7dp"
		android:layout_height="wrap_content"
		android:layout_gravity="end"
		android:background="@android:color/darker_gray"/>

	<RelativeLayout
		android:layout_width="wrap_content"
		android:layout_height="wrap_content">

		<TextView
			android:id="@+id/fast_scroller_bubble"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:layout_gravity="end"
			android:layout_marginRight="0dp"
			android:layout_marginEnd="0dp"
			android:paddingLeft="16dp"
			android:paddingRight="16dp"
			android:layout_toLeftOf="@+id/fast_scroller_handle"
			android:background="@drawable/fast_scroller_bubble"
			android:gravity="center"
			android:textColor="?android:attr/textColorPrimaryInverse"
			android:textSize="38sp"
			android:visibility="gone"
			tools:visibility="visible"
			tools:text="A" />

		<ImageView
			android:id="@+id/fast_scroller_handle"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:layout_alignParentRight="true"
			android:paddingLeft="6dp"
			android:paddingStart="6dp"
			android:contentDescription="@null"
			android:src="@drawable/fast_scroller_handle"/>

	</RelativeLayout>
	
</merge>

The previous Drawables and XML create the following preview:
FastScroller

To be continued...

Clone this wiki locally