0% found this document useful (0 votes)
10 views

Steps RecyclerView

Uploaded by

Good
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Steps RecyclerView

Uploaded by

Good
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Creating a RecyclerView in a Java Android app involves several steps.

Below is a detailed guide


to help you set up a RecyclerView from scratch:

Step 1: Set Up Your Project

1. Create a new Android project in Android Studio:


○ Open Android Studio.
○ Click on "Start a new Android Studio project."
○ Choose "Empty Activity" and proceed with the default configurations.

Step 2: Create the RecyclerView Layout

1. Open activity_main.xml and add the RecyclerView widget:

<LinearLayout
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"
android:orientation="vertical"
tools:context=".MainActivity">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>

Step 3: Create the Layout for Each Item

1. Create a new layout resource file named item_layout.xml in the res/layout


directory:

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp">

<TextView
android:id="@+id/itemTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item" />
</LinearLayout>

Step 4: Create a Model Class

1. Create a new Java class named Item.java:

public class Item {


private String text;

public Item(String text) {


this.text = text;
}

public String getText() {


return text;
}

public void setText(String text) {


this.text = text;
}
}
Step 5: Create the Adapter

1. Create a new Java class named ItemAdapter.java:

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;

public class ItemAdapter extends


RecyclerView.Adapter<ItemAdapter.ItemViewHolder> {
private List<Item> itemList;

public static class ItemViewHolder extends RecyclerView.ViewHolder


{
public TextView textView;

public ItemViewHolder(View itemView) {


super(itemView);
textView = itemView.findViewById(R.id.itemTextView);
}
}

public ItemAdapter(List<Item> itemList) {


this.itemList = itemList;
}

@NonNull
@Override
public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup
parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_layout, parent, false);
return new ItemViewHolder(itemView);
}

@Override
public void onBindViewHolder(@NonNull ItemViewHolder holder, int
position) {
Item currentItem = itemList.get(position);
holder.textView.setText(currentItem.getText());
}

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

Step 6: Set Up the RecyclerView in the MainActivity

1. Open MainActivity.java and set up the RecyclerView:

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {


private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
private List<Item> itemList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

itemList = new ArrayList<>();


for (int i = 1; i <= 20; i++) {
itemList.add(new Item("Item " + i));
}
recyclerView = findViewById(R.id.recyclerView);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
adapter = new ItemAdapter(itemList);

recyclerView.setAdapter(adapter);
}
}

Step 8: Run Your Application

1. Build and run your application on an emulator or a physical device.


2. You should see a list of items displayed in the RecyclerView.

By following these steps, you have created a basic RecyclerView implementation in an Android
app using Java. The RecyclerView displays a list of items defined in the Item model class and
rendered using the ItemAdapter adapter.

You might also like