GridView in Android is a group that displays items in a two-dimensional or divided into parts scrolling grid (rows & columns), the grid items are not necessary to present but are automatically inserted into the layout using a ListAdapter in the recycler view. The user can select any grid layout item by clicking on it. GridView defaults scrollable so we don’t need to use ScrollView or anything else with GridView. In GridView, each grid is the same size, that is, each grid has the same height and width. It shows symmetrical objects in thoughts. but in this post, we learn the automatic layout of the item is changed dynamically in the recycler view and it’s looking fantastic and attractive.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project, and fill in all required details to create a new project.
Step 2 − Implement one image roundedimageview Dependency in build.gradle in Module like this. and then click Sync Now
implementation 'com.makeramen:roundedimageview:2.3.0'
Step 3 – Now design the layout activity_mail.xml file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawrlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
Step 4 – You can create a recycler view static and dynamic it all depends on you but in this tutorial, I am using a static recycler view. Then you create one model class. right click your project name=>new=>java class
and name Homemodel.java
package com.example.staggered;
public class HomeModel {
private String image,name;
public HomeModel(String image, String name) {
this.image = image;
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Step 5 – Now this time you create one layout file to define the item layout in XML class like this click your res folder => then right-click layout folder click new=>then Layout Resource File =>then put name example_layout
<?xml version="1.0" encoding="utf-8"?>
<com.makeramen.roundedimageview.RoundedImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/images"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="10dp"
app:riv_corner_radius="12dp"
android:adjustViewBounds="true"/>
Step 5 – Now this time you create one Adaptor class like this right click your project name=>new=>java class
name HomeAdaptor
package com.example.staggered;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.recyclerview.widget.RecyclerView;
import com.allstar.shayarichat.R;
import com.makeramen.roundedimageview.RoundedImageView;
public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder>{
private HomeAdapter[] listdata;
// RecyclerView recyclerView;
public HomeAdapter(HomeAdapter[] listdata) {
this.listdata = listdata;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View listItem= layoutInflater.inflate(R.layout.example_layout, parent, false);
ViewHolder viewHolder = new ViewHolder(listItem);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.imageView.setImageResource(listdata[position].getImage());
}
@Override
public int getItemCount() {
return listdata.length;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public RoundedImageView imageView;
public ViewHolder(View itemView) {
super(itemView);
this.imageView = (RoundedImageView) itemView.findViewById(R.id.images);
}
}
}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
package example.example.staggered;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.allstar.shayarichat.R;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
HomeAdaptor[] myListData = new HomeAdaptor[] {
new HomeAdaptor(android.R.drawable.image1),
new HomeAdaptor(android.R.drawable.image2),
new HomeAdaptor(android.R.drawable.image3),
new HomeAdaptor(android.R.drawable.image4),
new HomeAdaptor(android.R.drawable.image5),
new HomeAdaptor(android.R.drawable.image6),
new HomeAdaptor(android.R.drawable.image7),
new HomeAdaptor(android.R.drawable.image8),
new HomeAdaptor(android.R.drawable.image9),
new HomeAdaptor(android.R.drawable.image10),
new HomeAdaptor(android.R.drawable.image11),
new HomeAdaptor(android.R.drawable.image12),
new HomeAdaptor(android.R.drawable.image13),
new HomeAdaptor(android.R.drawable.image14),
new HomeAdaptor(android.R.drawable.image15),
new HomeAdaptor(android.R.drawable.image16),
new HomeAdaptor(android.R.drawable.image17),
new HomeAdaptor(android.R.drawable.image18),
new HomeAdaptor(android.R.drawable.image19),
new HomeAdaptor(android.R.drawable.image20),
};
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
HomeAdaptor adapter = new HomeAdaptor(myListData);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
}