Android RecyclerView

Whenever you want to display a large number of items in a scrollable list, most items are not visible. For example, in a long list of words or many news headlines, list of products from the ecommerce website the user only sees a small number of list items at a time.

The RecyclerView class is a more advanced and flexible version of ListView. It is used for displaying large data sets that can be scrolled very efficiently to maintaining a limited number of views.

Use the RecyclerView widget when you need to display a large amount of scrollable data dynamically,means  the data is fetched dynamically  from the server at the runtime of the app or data collections whose elements change at runtime based on user action or network events.

RecyclerView components:

Data:

It is not important that  where the data comes from. You can create the data locally, or  get it from the  database or from the server dynamically on the device.

A RecyclerView:

It contains large amount of data in the scrolling view. means view the data by scrolling it. Exmaple to use it for ecommerce producrs, news feeds, blog posts etc.

Layout for one item of data;

In recycler view whatever data we are fetching is similar so that we are using same layout for all the items. so that one item view at a time created and filled with data. Example :ecommerce product list, employers data and students details etc.

A layout manager:

The layout manager handles the layout of user interface in a view. All view groups have layout managers. For example the LinearLayout, the Android system itself handles the layout for view. RecyclerView requires an explicit layout manager to manage the user interface in a view list items contained within it. This layout may be  vertical, horizontal, or a grid etc

 An adapter:

The adapter connects your data to the RecyclerView. It prepares the data and how will be displayed in a view holder.

The adapter updates the contents of the respective list item view when data changes are done in the RecyclerView.

the  Adapter class  extends RecyclerViewAdapter. The adapter class uses a ViewHolder to hold the views that contains each item in the RecyclerView, and to bind the data to be displayed into the views that displays to the user.

A view holder:

The view holder extends the ViewHolder class. It contains the view information for displaying one item from the item’s layout. A view holder used by the adapter to supply data, and extends RecyclerView.ViewHolder class.

Data:

Any displayable data can be shown in a RecyclerView. The data maybe any format Text ,Images, Icons etc. that will be present in the app or we are fetched dynamically  while running the application from database or from server.

Creating a  Recycler View:

Implementing a RecyclerView requires the following steps:

Add the RecyclerView dependency to the app’s app/build.gradle file first to make it available to the layout file and activity class.

Add the RecyclerView to the activity’s layout file.

Create a layout XML file for one item

Create any java  class and Extend RecyclerView.Adapter and implement  onCreateViewHolder and onBindViewHolder methods inside it.

Extend RecyclerView.ViewHolder to create a view holder for your item layout. Here we can implement  click behavior by overriding the onClick() method.

In your activity class, Inside onCreate method, create a RecyclerView and initialize it with the adapter and a  layout manager.

Add the dependency to app/build.gradle :

dependencies {
...
compile 'com.android.support:recyclerview-v7:24.1.1'
...
}

Note: In latest vrsion of the android in dependencies the complie is replaced with the implement.

Adding a RecyclerView to your activity’s layout:

Add the RecyclerView in your activity’s layout file.

<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>

Create the layout for one item:

Create an XML resource file and specify the layout of one item. This will be used by the adapter to create the view holder and will be used for all the items.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dp">

<TextView
android:id="@+id/word"
style="@style/word_title" />

</LinearLayout>

Create an adapter with a view holder:

Create a new Java class with the following signature:

public class Adapter extends RecyclerView.Adapter<MyWordListAdapter.MyWordViewHolder> {

}

In the constructor, get an inflater from the current contex, and your data.

public MyWordListAdapter(Context context, LinkedList<String> wordList) {
mInflater = LayoutInflater.from(context);
this.myWordList = wordList;
}

For this adapter, you have to implement 3 methods.

onCreateViewHolder() creates a view and returns it.

@Override
public MyWordViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
// Inflate an item view.
View myItemView = mInflater.inflate(R.layout.mywordlist_item, parent, false);
return new MyWordViewHolder(myItemView, this);
}

onBindViewHolder() associates the data with the view holder for a given position in the RecyclerView:

@Override
public void onBindViewHolder(MyWordViewHolder holder, int position) {
// Retrieve the data for that position
String myCurrent = myWordList.get(position);
// Add the data to the view
holder.mywordItemView.setText(myCurrent);
}

getItemCount() returns to number of data items available for displaying to the user:

@Override
public int getItemCount() {
return myWordList.size();
}

Creating the view holder class:

class MyWordViewHolder extends RecyclerView.ViewHolder {}

If we want to add onclick events to handle then we need to implement onClickListener() method inside  MyWordViewHolder class.

// Extend the signature of WordVewHolder to implement a click listener.

class MyWordViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {}

In its constructor, the view holder has to inflate its layout, associate with its adapter, and, if applicable, set a click listener.

public MyWordViewHolder(View itemView, MyWordListAdapter adapter) {
super(itemView);
MywordItemView = (TextView) itemView.findViewById(R.id.word);
this.mAdapter = adapter;
itemView.setOnClickListener(this);
}

And, if you implementing onClickListener, you also have to implement onClick().

@Override
public void onClick(View v) {
wordItemView.setText ("Clicked! "+ wordItemView.getText());
}

Create the RecyclerView :

In the mainActivity class create the instance of RecyclerView. and then create Adapetr class instance by supplying the data that will be dispalyed to the user and then connect the Adapter class to the RecyclerView to display the data. As shown in below example code.

Get a handle to the RecyclerView.

myRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);

Create an adapter and supply the data to be displayed.

myAdapter = new MyWordListAdapter(this, mWordList);

Connect the adapter with the recycler view.

mRecyclerView.setAdapter(myAdapter);

Give the recycler view a default layout manager.

myRecyclerView.setLayoutManager(new LinearLayoutManager(this));

 

RecyclerView is used to display large amount of that is fetched dynamically from the database or from the server or that will be available within the app.The fetched data will be displayed in the scrolable view to the user. The data format may be text, images,  or any files.

Congratulations You Have Learned About Recycler View 

Leave a Reply

Categories

Myles [CPS] IN
Shein WW