Overview :-
Reuses cells while scrolling up/down - this is possible with implementing View Holder in the listView adapter, but it was an optional thing, while in the RecycleView it's the default way of writing adapter.
Decouples list from its container - so you can put list items easily at run time in the different containers (linearLayout, gridLayout) with setting LayoutManager.
Animates common list actions - Animations are decoupled and delegated to ItemAnimator.
There is more about RecyclerView, but I think these points are the main ones.
So, to conclude, RecyclerView is a more flexible control for handling "list data" that follows patterns of delegation of concerns and leaves for itself only one task - recycling items.

RecyclerView was created as a ListView improvement, so yes, you can create an attached list with ListView control.The
RecyclerView
class supports the display of a collection of data.
It is a modernized version of the
ListView
and the GridView
classes provided by the Android framework. Recycler view addresses several issues that the existing widgets have. It enforced a programming style that results in good performance. It also comes with default animations for removing and adding elements.RecyclerView
allow to use different layout managers for positioning items.
Recycler view uses a
ViewHolder
to store references to the views for one entry in the recycler view. A ViewHolder
class is a static inner class in your adapter which holds references to the relevant views. With these references your code can avoid the time-consuming findViewById()
method to update the widgets with new data. but using RecyclerView is easier as it:Reuses cells while scrolling up/down - this is possible with implementing View Holder in the listView adapter, but it was an optional thing, while in the RecycleView it's the default way of writing adapter.
Decouples list from its container - so you can put list items easily at run time in the different containers (linearLayout, gridLayout) with setting LayoutManager.
Example:
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//or
mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
Animates common list actions - Animations are decoupled and delegated to ItemAnimator.
There is more about RecyclerView, but I think these points are the main ones.
So, to conclude, RecyclerView is a more flexible control for handling "list data" that follows patterns of delegation of concerns and leaves for itself only one task - recycling items.
The
RecyclerView
widget is a more advanced and flexible version of ListView
.
In the
RecyclerView
model, several different components work together to display your data. The overall container for your user interface is a RecyclerView
object that you add to your layout. The RecyclerView
fills itself with views provided by a layout manager that you provide. You can use one of our standard layout managers (such as LinearLayoutManager
or GridLayoutManager
), or implement your own.
The views in the list are represented by view holder objects. These objects are instances of a class you define by extending
RecyclerView.ViewHolder
. Each view holder is in charge of displaying a single item with a view. For example, if your list shows music collection, each view holder might represent a single album. The RecyclerView
creates only as many view holders as are needed to display the on-screen portion of the dynamic content, plus a few extra. As the user scrolls through the list, the RecyclerView
takes the off-screen views and rebinds them to the data which is scrolling onto the screen.
The view holder objects are managed by an adapter, which you create by extending
RecyclerView.Adapter
. The adapter creates view holders as needed. The adapter also binds the view holders to their data. It does this by assigning the view holder to a position, and calling the adapter's onBindViewHolder()
method. That method uses the view holder's position to determine what the contents should be, based on its list position.Add the support library
To access the RecyclerView widget, you need to add the v7 Support Libraries to your project as follows:
- Open the
build.gradle
file for your app module.
- Add the support library to the
dependencies
section.
dependencies { implementation 'com.android.support:recyclerview-v7:27.1.1' }
Add RecyclerView to your layout
Now you can add the
RecyclerView
to your layout file. For example, the following layout uses RecyclerView
as the only view for the whole layout:<?xml version="1.0" encoding="utf-8"?> <!-- A RecyclerView with some commonly used attributes --> <android.support.v7.widget.RecyclerView android:id="@+id/my_recycler_view" android:scrollbars="vertical" android:layout_width="match_parent" android:layout_height="match_parent"/>
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: public class MyActivity extends Activity { private RecyclerView mRecyclerView; private YourAdapter mAdapter; private RecyclerView.LayoutManager mLayoutManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_activity); mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view); // use this setting to improve performance if you know that changes // in content do not change the layout size of the RecyclerView mRecyclerView.setHasFixedSize(true); // use a linear layout manager mLayoutManager = new LinearLayoutManager(this); mRecyclerView.setLayoutManager(mLayoutManager); // specify an adapter (see also next example) mAdapter = new YourAdapter(myDataset); mRecyclerView.setAdapter(mAdapter); } // ... }
Add a list adapter
To feed all your data to the list, you must extend the
RecyclerView.Adapter
class. This object 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
TextView
widgets: public class YourAdapter extends RecyclerView.Adapter<YourAdapter.ViewHolder> { private String[] mDataset; // you provide access to all the views for a data item in a view holder public static class ViewHolder extends RecyclerView.ViewHolder {public TextView mTextView; public ViewHolder(TextView v) { super(v); mTextView = v; } } public YourAdapter(String[] myDataset) { mDataset = myDataset; } // Create new views (invoked by the layout manager) @Override public YourAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { // create a new view TextView v = (TextView) LayoutInflater.from(parent.getContext()) .inflate(R.layout.my_text_view, parent, false); ... ViewHolder vh = new ViewHolder(v); return vh; } // Replace the contents of a view (invoked by the layout manager) @Override public void onBindViewHolder(ViewHolder holder, int position) {holder.mTextView.setText(mDataset[position]); } // Return the size of your dataset (invoked by the layout manager) @Override public int getItemCount() { return mDataset.length; } }
The layout manager calls the adapter's
onCreateViewHolder()
method. That method needs to construct a RecyclerView.ViewHolder
and set the view it uses to display its contents. The type of the ViewHolder must match the type declared in the Adapter class signature. Typically, it would set the view by inflating an XML layout file. Because the view holder is not yet assigned to any particular data, the method does not actually set the view's contents.
The layout manager then binds the view holder to its data. It does this by calling the adapter's
onBindViewHolder()
method, and passing the view holder's position in the RecyclerView
. The onBindViewHolder()
method needs to fetch the appropriate data, and use it to fill in the view holder's layout. For example, if the RecyclerView
is displaying a list of names, the method might find the appropriate name in the list, and fill in the view holder's TextView
widget.
If the list needs an update, call a notification method on the
RecyclerView.Adapter
object, such asnotifyItemChanged()
. The layout manager then rebinds any affected view holders, allowing their data to be updated.Modifying the layout
The
RecyclerView
uses a layout manager to position the individual items on the screen and determine 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 expensive findViewById()
lookups. The Android Support Library includes three standard layout managers, each of which offers many customization options:LinearLayoutManager
arranges the items in a one-dimensional list. Using aRecyclerView
withLinearLayoutManager
provides functionality like the olderListView
layout.
GridLayoutManager
arranges the items in a two-dimensional grid, like the squares on a checkerboard. Using aRecyclerView
withGridLayoutManager
provides functionality like the olderGridView
layout.
StaggeredGridLayoutManager
arranges the items in a two-dimensional grid, with each column slightly offset from the one before, like the stars in an American flag.
If none of these layout managers suits your needs, you can create your own by extending the
RecyclerView.LayoutManager
abstract class.
Figure 1. A list using
RecyclerView
![]() |
A list With CardView |
No comments:
Post a Comment