info@androidpaper.co.in

How to Use Android RecyclerView to Display Data

RecyclerView is a powerful and flexible UI component in Android Studio that allows you to efficiently display large sets of data in a scrollable list or grid. It is often used in conjunction with a custom adapter to bind data to individual items within the RecyclerView. When working with RecyclerView, it is common to use a model class to represent the data for each item. The model class encapsulates the properties and behaviors of an item, allowing you to organize and manipulate your data more effectively.

Step 1: Set up your project
Create a new project in Android Studio or open an existing one. Make sure you have the necessary dependencies in your project by adding the following line to the dependencies section of your app-level build.gradle file:

implementation 'androidx.recyclerview:recyclerview:1.2.0'

Sync your project to download the RecyclerView library.

Step 2: Create the model class
Create a Java class that represents your data model. This class will hold the data for each item in the RecyclerView. For example, create a file named MyModelRecycler.java and define the properties and methods according to your needs. Here's an example:

public class MyModelRecycler {

    String name;
    int gifId;

    public MyModelRecycler(String name,int gifId) {
        this.gifId = gifId;
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getGifId() {
        return gifId;
    }

    public void setGifId(int gifId) {
        this.gifId = gifId;
    }
}

Step 3: Create the RecyclerView adapter
Create a new Java class for your RecyclerView adapter. This adapter will be responsible for binding the data to each item in the RecyclerView. Extend the RecyclerView.Adapter class and implement the necessary methods. The adapter class should contain an inner class that extends RecyclerView.ViewHolder to hold references to the views in the item layout. Here's an example:

public class ImRAdapter extends RecyclerView.Adapter<ImRAdapter.ViewHolder> {

    private MyModelRecycler[] listdata;
    Context context;

    public ImRAdapter(MyModelRecycler[] listdata,Context mContext) {
        this.listdata = listdata;
        this.context=mContext;

    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View listItem= layoutInflater.inflate(R.layout.item_animation, parent, false);
        ViewHolder viewHolder = new ViewHolder(listItem);
        return viewHolder;
    }

    @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
        final MyModelRecycler myListData = listdata[position];
        // holder.textView.setText(listdata[position].getDescription());
        holder.imageView.setImageResource(listdata[position].getGifId());
        
    }


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

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public GifImageView imageView;
        public TextView textView;

        public ViewHolder(View itemView) {
            super(itemView);
            this.imageView = (GifImageView) itemView.findViewById(R.id.ivThumb);

        }
    }
}


Step 4: Set up the RecyclerView in your activity/fragment layout
In your activity/fragment layout XML file, add a RecyclerView element. For example:

<androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:id="@+id/recylerview"/>

Step 5: Initialize the RecyclerView in your activity/fragment
In your activity/fragment class, find the RecyclerView by its ID and initialize it with the adapter and a layout manager. Here's an example:

Create a list of data models that you want to display in the RecyclerView. Pass this list to the adapter when initializing it. For example:

RecyclerView recylerview;
MyModelRecycler[] myListData;


recylerview = findViewById(R.id.recylerview);
myListData = new MyModelRecycler[] {
                new MyModelRecycler("Email", R.drawable.gif0),
                new MyModelRecycler("Info", R.drawable.gif1),
                new MyModelRecycler("Info", R.drawable.gif0)};

        ImRAdapter adapter = new ImRAdapter(myListData,ImRecyclerActivity.this);
        recylerview.setHasFixedSize(true);
        recylerview.setLayoutManager(new LinearLayoutManager(this));
        recylerview.setAdapter(adapter);

That's it for Recyceview.
Step 7: Now, need to know, if click on an item of recyclerview.

In adapter class, onBindViewHolder

 holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String pos = String.valueOf(position);
                Toast.makeText(view.getContext(),"click on item: "+position,Toast.LENGTH_LONG).show();
                Intent newInt = new Intent(context, DetailsActivity.class);
                newInt.putExtra("key1",pos);
                context.startActivity(newInt);

            }
        });


Step 8: Write DetailsActivity class

 GifImageView ivThumb;
    String draw_string ="gif";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);
        ivThumb = findViewById(R.id.ivThumb);

        String value=getIntent().getStringExtra("key1");
        draw_string = "gif"+value;

        int resourceId = getResources().getIdentifier(draw_string, "drawable", getPackageName() );

        ivThumb.setImageResource(resourceId);
    }

Step 9: In your activity/fragment layout XML file, add a gif element. For example:

 <pl.droidsonroids.gif.GifImageView
        android:id="@+id/ivThumb"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitXY"/>

That's it! You have successfully set up and used a customized RecyclerView with a model class in your Android Studio project. Make sure to customize the adapter, model class, and layout according to your specific needs.