Basic RecyclerView Custom Adapter in Java Android

RecyclerView is a UI component in Android to show lists & grid of items. we usually write the RecyclerView Custom Adapter to populate data to each item of the list. In the following code, I have shared a basic RecyclerView custom adapter in Java. You can just copy-paste this code in your project and modify it according to your needs.

Following is the Java code, If you are looking for a Kotlin code, please find it at the below link.

Basic RecyclerView Custom Adapter in Kotlin

Basic RecyclerView Custom Adapter in JAVA

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import androidx.recyclerview.widget.RecyclerView;

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

    private final Context context;
    private ArrayList<String> list;

    public CustomAdapter(Context context, ArrayList<String> list) {
        this.context = context;
        this.list = list;
    }

    private class ViewHolder extends RecyclerView.ViewHolder {

        TextView yourView;
        ViewHolder(final View itemView) {
            super(itemView);
            yourView = itemView.findViewById(R.id.your_textview_id); // Initialize your All views prensent in list items
        }
        void bind(int position) {
            // This method will be called anytime a list item is created or update its data
            //Do your stuff here
            yourView.setText(list.get(position));
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.your_layout_xml_file, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((ViewHolder) holder).bind(position);
    }

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

It’s a sample code in which you need to change your “your_layout_xml_file” with your layout XML file reference. You can update ViewHolder class and its methods according to your custom layout and data model.

This is how you can initialize RecyclerView and assign this CustomAdapter to it.

RecyclerView recyclerView = findViewById(R.id.recycler_view);
CustomAdapter adapter = new CustomAdapter(this, YOUR_ARRAY_LIST);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);

That’s it. You can have a look at our tutorials knowledgebase at the following link.
Handy Opinion Tutorials

Next:

1. Convert Bitmap to File in Android – JAVA & Kotlin

2. Get Screen Width & Height in Kotlin – Android

Please share this post:
Native Mobile Application Developer (Android + IOS) having experience in Java, Swift, Kotlin, Objective C, Unity, C#, C/C++, NODE JS & PHP.

One thought on “Basic RecyclerView Custom Adapter in Java Android

Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

%d bloggers like this: