RecyclerView with INTERFACE (Android)




Heres how you create a recyclerView with INTERFACE in JAVA.

With the help of interface you can transmit the data from your recycler adapter to your main activity or any Activity you want.

Firstly lets initiate our recycler layout in our main layout file:

activity_main.xml.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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>

Lets create our Recycler View Layout:

recycler_layout.xml.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/textView"
android:text="sample"
android:textColor="#ffffff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"/>

</RelativeLayout>


And lets create our adapter Like this:

RecyclerAdapter.java


package com.example.oddtocode;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

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

Context mContext;
List<String> listItems;
RecyclerClickListener listener;

public interface RecyclerClickListener{
void onClick(int position, String item);
}

public RecyclerAdapter(Context mContext, List<String> listItems, RecyclerClickListener listener) {
this.mContext = mContext;
this.listItems = listItems;
this.listener = listener;
}

@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflator = LayoutInflater.from(mContext);
View view = inflator.inflate(R.layout.recycler_layout, parent, false);
return new ViewHolder(view);
}

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

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

class ViewHolder extends RecyclerView.ViewHolder {

TextView textView;

public ViewHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.textView);
}

public void bind(List<String> listItems, int position) {
textView.setText(listItems.get(position));
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

listener.onClick(position,listItems.get(position));
}
});
}
}
}

and Finally Our MainActiivty.java look like this.


package com.example.oddtocode;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import android.os.Bundle;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

RecyclerView recyclerView;
RecyclerAdapter adapter;
List<String> listItems;

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

recyclerView = findViewById(R.id.recyclerView);

listItems = new ArrayList<>();
listItems.add("Item 1");
listItems.add("Item 2");
listItems.add("Item 3");
listItems.add("Item 4");
listItems.add("Item 5");
listItems.add("Item 6");
listItems.add("Item 7");
listItems.add("Item 8");
listItems.add("Item 9");
listItems.add("Item 10");
listItems.add("Item 11");
listItems.add("Item 12");
listItems.add("Item 13");
listItems.add("Item 14");
listItems.add("Item 15");
listItems.add("Item 16");
listItems.add("Item 17");
listItems.add("Item 18");
listItems.add("Item 19");
listItems.add("Item 20");

recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new RecyclerAdapter(getApplicationContext(), listItems,
 new RecyclerAdapter.RecyclerClickListener() {
@Override
public void onClick(int position, String item) {
Toast.makeText(getApplicationContext(),
       "Item Name: "+item+" Clicked on position: "+position,Toast.LENGTH_SHORT).show();
}
});
recyclerView.setAdapter(adapter);

}
}

and yes finally that's how our app looks like:










Comments