recyclerfragment
RecyclerFragment is a small Android library that allow you to display RecyclerView data in a Fragment. Easy to use, it is similar to the framework's ListFragment with additional features.
Download
The library is available through jCenter.
You just have to include the dependency in your build.gradle file :
Features
- Fragment with a
RecyclerView
and a hideableProgressBar
out of the box - Ability to show the
ProgressBar
when waiting for asynchronous data - Customizable layout
- Support for an "empty view" to be displayed automatically in place
of RecyclerView when the adapter contains no data.
How to use
The preferred way to use RecyclerFragment is to extend it to add
your behavior in onActivityCreated(Bundle)
.
The following example show you how to use RecyclerFragment when loading
data asynchronously :
Note that the progress indicator won't we shown if your data is loaded
in less than 500 ms. This is an expected behavior: similarly to
ContentLoadingProgressBar
, the progress indicator is only shown if it
will be displayed a sufficient amount of time to avoid UI "flashes".
Using a custom layout
You may need to customize the layout of RecyclerFragment.
All you have to do is to override onCreateView
and inflate
your custom view hierarchy. Howether, your layout has to meet
the following criterias:
- It must contain a
RecyclerView
with id@id/recycler
- It must contain any
View
with id@id/progress
to be displayed
when theRecyclerView
is hidden bysetRecyclerShown(false)
.
You may optionally specify a View
to be automatically displayed
in place of the RecyclerView
when the adapter is empty:
just mark it with the id @id/empty
.
The following is an example of custom layout for RecyclerFragment:
fragment_custom.xml
MyFragment.java
Don't forget to set your LayoutManager in XML or via
setLayoutManager(RecyclerView.LayoutManager)
,
as only the default implementation uses a LinearLayoutManager
when
no other is provided.