Escolar Documentos
Profissional Documentos
Cultura Documentos
Glossary of terms:
Index: The index of an attached child view as used in a call to getChildAt(int). Contrast
with Position.
Recycle (view): A view previously used to display data for a specific adapter position may
be placed in a cache for later reuse to display the same type of data again later. This can
drastically improve performance by skipping initial layout inflation or construction.
Scrap (view): A child view that has entered into a temporarily detached state during
layout. Scrap views may be reused without becoming fully detached from the parent
RecyclerView, either unmodified if no rebinding is required or modified by the adapter if the
view was considered dirty.
Dirty (view): A child view that must be rebound by the adapter before being displayed.
Positions in RecyclerView:
RecyclerView introduces an additional level of abstraction between
the RecyclerView.Adapter and RecyclerView.LayoutManager to be able to detect data set
changes in batches during a layout calculation. This saves LayoutManager from tracking adapter
changes to calculate animations. It also helps with performance because all view bindings
happen at the same time and unnecessary bindings are avoided.
For this reason, there are two types of position related methods in RecyclerView:
layout position: Position of an item in the latest layout calculation. This is the position
from the LayoutManager's perspective.
adapter position: Position of an item in the adapter. This is the position from the Adapter's
perspective.
These two positions are the same except the time between dispatching adapter.notify* events
and calculating the updated layout.
Methods that return or receive *LayoutPosition* use position as of the latest layout calculation
(e.g. getLayoutPosition(),findViewHolderForLayoutPosition(int)). These positions include all
changes until the last layout calculation. You can rely on these positions to be consistent with
what user is currently seeing on the screen. For example, if you have a list of items on the screen
and user asks for the 5th element, you should use these methods as they'll match what user is
seeing.
The other set of position related methods are in the form of *AdapterPosition*.
(e.g. getAdapterPosition(),findViewHolderForAdapterPosition(int)) You should use these
methods when you need to work with up-to-date adapter positions even if they may not have
been reflected to layout yet. For example, if you want to access the item in the adapter on a
ViewHolder click, you should use getAdapterPosition(). Beware that these methods may not be
able to calculate adapter positions if notifyDataSetChanged() has been called and new layout
has not yet been calculated. For this reasons, you should carefully
handle NO_POSITION or null results from these methods.
When writing a RecyclerView.LayoutManager you almost always want to use layout positions
whereas when writing an RecyclerView.Adapter, you probably want to use adapter positions.
Create Lists
The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a
container for displaying large data sets that can be scrolled very efficiently by maintaining a
limited number of views. Use the RecyclerViewwidget when you have data collections whose
elements change at runtime based on user action or network events.
The RecyclerView class simplifies the display and handling of large data sets by providing:
Default animations for common item operations, such as removal or addition of items
You also have the flexibility to define custom layout managers and animations
for RecyclerView widgets.
To use the RecyclerView widget, you have to specify an adapter and a layout manager. To
create an adapter, extend the RecyclerView.Adapter class. The details of the
implementation depend on the specifics of your dataset and the type of views. For more
information, see the examples below.
A layout manager positions item views inside a RecyclerView and determines when to reuse
item views that are no longer visible to the user. To reuse (or recycle) a view, a layout manager
may ask the adapter to replace the contents of the view with a different element from the dataset.
Recycling views in this manner improves performance by avoiding the creation of unnecessary
views or performing expensivefindViewById() lookups.
Animations
Animations for adding and removing items are enabled by default in RecyclerView. To
customize these animations, extend the RecyclerView.ItemAnimator class and use
theRecyclerView.setItemAnimator() method.
Examples
The following code example demonstrates how to add the RecyclerView to a layout:
Once you have added a RecyclerView widget to your layout, obtain a handle to the object,
connect it to a layout manager, and attach an adapter for the data to be displayed:
The adapter provides access to the items in your data set, creates views for items, and replaces
the content of some of the views with new data items when the original item is no longer visible.
The following code example shows a simple implementation for a data set that consists of an
array of strings displayed using TextViewwidgets:
Create Cards
CardView extends the FrameLayout class and lets you show information inside cards that have
a consistent look across the platform. CardView widgets can have shadows and rounded
corners.
To create a card with a shadow, use the card_view:cardElevationattribute. CardView uses real
elevation and dynamic shadows on Android 5.0 (API level 21) and above and falls back to a
programmatic shadow implementation on earlier versions. For more information, seeMaintaining
Compatibility.
Use these properties to customize the appearance of the CardViewwidget:
The following code example shows you how to include a CardView widget in your layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
... >
<!-- A CardView that contains a TextView -->
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_gravity="center"
android:layout_width="200dp"
android:layout_height="200dp"
card_view:cardCornerRadius="4dp">
<TextView
android:id="@+id/info_text"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v7.widget.CardView>
</LinearLayout>
Add Dependencies
The RecyclerView and CardView widgets are part of the v7 Support Libraries. To use these
widgets in your project, add these Gradle dependencies to your app's module:
dependencies {
...
compile 'com.android.support:cardview-v7:21.0.+'
compile 'com.android.support:recyclerview-v7:21.0.+'
}