Android-RecyclerView
In Android, RecyclerView is the improved version of ListView. The data is shown using RecyclerView as a scrollable list. A ViewGroup is used to display a lot of data. It presents a View for each item in a big dataset. A scrollable list can be implemented effectively using RecyclerView.
1.First user creates a project with any project name, and there are two default files already created, MainActivity.java and activity_main.xml.
2. User should have to add dependency in our build.gradle code file.
1 2 3 |
implementation "androidx.recyclerview:recyclerview:1.3.0" //RecyclerView depedency implementation "androidx.recyclerview:recyclerview-selection:1.2.0-alpha01" |
3. Now user adds the RecyclerView layoutactivity_main.xml file,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout> |
4. Now user creates a custom layout as row_layout.xml which is used to display the element of RecyclerView.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="horizontal" android:layout_height="wrap_content"> <ImageView android:id="@+id/image" android:layout_width="300px" android:layout_height="300px" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:src="@color/teal_200" /> <TextView android:id="@+id/name" android:layout_width="250dp" android:layout_height="55dp" android:layout_toRightOf="@id/image" android:layout_marginLeft="15dp" android:layout_marginTop="35dp" android:text="abcd" android:textColor="#000" android:textSize="30sp" /> </RelativeLayout> |
5. Now create a model class for showing the list of RecyclerView which is named Data.java, here user creates a public constructor for the image and name and getter setter method for variables.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package com.example.layouts; public class Data { public String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getImageId() { return imageId; } public void setImageId(int imageId) { this.imageId = imageId; } public int imageId; Data(String name, int imageId) { this.name = name; this.imageId = imageId; } } |
6. Here user creates an adapter class for RecyclerView names as RecyclerView_Adapter.java, here defined the ViewHolder for the list, RecyclerView.The adapter extends and creates its constructor.
- onCreateViewHolder()- It initializes the view holder and expands the row arrangement. In order to prevent repeated calls, it manages findViewById methods, finds views only once, and recycles them.
- onBindViewHolder()- The current RecyclerView row is filled with data using the onCreateViewHolder() view holder.
- getItemCount()- This function returns the size of the collection containing the items that we want to display.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
package com.example.layouts; import android.app.Application; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import java.util.Collections; import java.util.List; public class RecyclerView_Adapter extends RecyclerView.Adapter<View_Holder> { List<Data> list = Collections.emptyList(); Context context; public RecyclerView_Adapter(List<Data> data, Application application) { this.list = data; this.context = application; } @NonNull @Override public View_Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { //Inflate the layout, initialize the View Holder View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout, parent, false); View_Holder holder = new View_Holder(v); return holder; } @Override public void onBindViewHolder(@NonNull View_Holder holder, int position) { //Use the provided View Holder on the onCreateViewHolder method to populate the current row on the RecyclerView holder.name.setText(list.get(position).name); holder.imageView.setImageResource(list.get(position).imageId); } @Override public int getItemCount() { return list.size(); } } |
7. Now create a ViewHolder Java class named View_Holder.java, A ViewHolder is used by the RecyclerView to store references to each view. This approach avoids all findViewById() method calls in the adapter to determine which views should contain data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.example.layouts; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import androidx.recyclerview.widget.RecyclerView; public class View_Holder extends RecyclerView.ViewHolder { TextView name; ImageView imageView; View_Holder(View itemView) { super(itemView); name = (TextView) itemView.findViewById(R.id.name); imageView = (ImageView) itemView.findViewById(R.id.image); } } |
8. Create a Recycler View Adapter instance in the MainActivity class’s onCreate() function of the MainActivity.java file, and send the adapter the list of data and background information. The context for an application will be provided via the getApplication() method.Code for MainActivity.java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package com.example.layouts; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import android.os.Bundle; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); List<Data> data = fill_with_data(); RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView); RecyclerView_Adapter adapter = new RecyclerView_Adapter(data, getApplication()); recyclerView.setAdapter(adapter); recyclerView.setLayoutManager(new LinearLayoutManager(this)); } public List<Data> fill_with_data() { List<Data> data = new ArrayList<>(); data.add(new Data("Apple", R.drawable.fruit)); data.add(new Data("DraganFruit", R.drawable.fruit)); data.add(new Data("Kiwi", R.drawable.fruit)); data.add(new Data("Grapes", R.drawable.fruit)); data.add(new Data("papaya", R.drawable.fruit)); data.add(new Data("mango", R.drawable.fruit)); data.add(new Data("guava", R.drawable.fruit)); data.add(new Data("Banana", R.drawable.fruit)); data.add(new Data("Berries", R.drawable.fruit)); data.add(new Data("SQL Server", R.drawable.fruit)); return data; } } |