Heres how you create a recyclerView with OnClickListener in JAVA.
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
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
Context mContext;
List<String> listItems;
public RecyclerAdapter(Context mContext, List<String> listItems) {
this.mContext = mContext;
this.listItems = listItems;
}
@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) {
Toast.makeText(mContext, "item: " + listItems.get(position), Toast.LENGTH_SHORT).show();
}
});
}
}
}
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 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);
recyclerView.setAdapter(adapter);
}
}
and yes finally that's how our app looks like:
Comments
Post a Comment