Recycler View in Android Part-2 Coding and Explanation

android_learners_hub
android_learners_hub


Recycler View in Android Part-2 Coding and Explanation

In the previous post we discuss about recycler view, its advantage, key classes i.e Adapter,View Holder and Layout manager etc.. Now, we will do coding for making a recycler view. Here, in this App I will make 2 xml and 2 java files that are:-

Xml Files

1. activity_main.xml :- To make Recycler View. Go to Design then Palette then from Common drag and drop Recycler View.
<androidx.recyclerview.widget.RecyclerView android:id="@+id/recycle"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
/>
2. row_layout.xml :-To make layout for an individual row of Recycler View.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/android"
android:id="@+id/image_1"/>
<TextView
android:layout_toEndOf="@+id/image_1"
android:layout_marginStart="2dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:id="@+id/text"
android:text="Hello Android"
android:textColor="@color/black"
/>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignEnd="@+id/text"
android:id="@+id/image_2"
android:src="@drawable/download"/>
</RelativeLayout>



Java Files

1. MainActivity.java:- In this java file I will do 4 tasks that are 
                                     1. Casting of object of Recycler View
                                     2. Setting Layout Manager
                                     3. Making Array to store data 
                                     4. Set Adapter to the Recycler View
package com.example.recycler_view;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
RecyclerView obj_recycler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//casting of object of recycler view.
obj_recycler=findViewById(R.id.recycle);
//Set Layout manager.here, we will set the layout as linear
obj_recycler.setLayoutManager(new LinearLayoutManager(this));
// make data source(array) to pass in adapter class.
String array[]={"Article on Technology","Article on Business Management","Article on Fashion Designing","Article on Hobbies","Article on AI","Article on WWW","Article on Media","Article on network","Article on Fashion","Article on mobiles","Article on homework"};
// set Adapter to recyclerView.
obj_recycler.setAdapter(new MyAdapter(array));


}
}

2. MyAdapter.java:- This will act like a middle man between data and view. It contain the functions(oncreate, onbind,viewholder) that we discussed in detail in the previous post.

package com.example.recycler_view;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

class MyAdapter extends RecyclerView.Adapter<MyAdapter.myHolder> {
String data[];
// constructor of MyAdapter class
public MyAdapter(String[] data)
{
this.data=data;
}



@NonNull
@Override
public myHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
// to create view that we design row_layout with the help of layout inflater
LayoutInflater inflater=LayoutInflater.from(viewGroup.getContext());
View view=inflater.inflate(R.layout.row_layout,viewGroup,false);
// The references that are stored in myHolder class are passed to this method.
// Because this method is called first and this will first call myHolder class(seen in declaration)
// After that flow or call will be passed to onbind method,
// onbind will catch that with the help of holder.
return new myHolder(view);
}

@Override
public void onBindViewHolder(@NonNull myHolder myHolder, int i) {
//this will store the data in those references that are passed by the above method.
myHolder.text.setText(data[i]);
}

@Override
public int getItemCount() {
return data.length;
}

class myHolder extends RecyclerView.ViewHolder{
ImageView img_Start,img_end;
TextView text;
public myHolder(@NonNull View itemView) {
super(itemView);
img_Start=(ImageView)itemView.findViewById(R.id.image_1);
text=(TextView)itemView.findViewById(R.id.text);
img_end=(ImageView)itemView.findViewById(R.id.image_2);
}
}
}

Images used in this App :-

I use two images in this App one is JPG that you can easily find on internet ,then, copy that image and paste in drawable folder .The second image is designed by me with the help of vector asset.

Steps to create that image from vector asset:-


Step 1: Right click on drawable folder and click vector asset as shown in figure.

Step 1


Step 2 Choose that image from Clip Art and give it color. After that click on Finish Button.

 
step 2
Step 3  After Finishing this , the xml file that is created is shown as below.

step 3



 


Output

output




android_learners_hub


Comments

Post a Comment