RecyclerViewItemListenerActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.bhaumik.officetask.RecyclerViewItemListenerActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_item"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
listener_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_listener_item"
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="Hello World"
android:textSize="@dimen/_30dp"
android:gravity="center"
/>
</LinearLayout>
RecyclerViewItemListenerActivity.java
package com.example.bhaumik.officetask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;
import com.example.bhaumik.officetask.adapter.ListenerAdapter;
import com.example.bhaumik.officetask.interfaces.ItemListener;
import java.util.ArrayList;
import java.util.List;
public class RecyclerViewItemListenerActivity extends AppCompatActivity {
String[] items= {"Aestro","Blender","Cupcake","Donut","Eclair","Froyo","GingerBread"
,"HoneyComb", "IceCream Sendwich","JellyBean","Kitkat","Lolipop","Marshmallow","Noght"};
List<String> list = new ArrayList<>();
ListenerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler_view_item_listener);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_item);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
int i=0;
for (String data : items){
list.add(data);
i++;
}
adapter = new ListenerAdapter(list);
recyclerView.setAdapter(adapter);
adapter.setOnItemClickListener(new ItemListener() {
@Override
public void onItemClick(int pos) {
Toast.makeText(RecyclerViewItemListenerActivity.this,"Clicked Item Is : " + list.get(pos),
Toast.LENGTH_SHORT).show();
}
});
}
}
ListenerAdapter.java
package com.example.bhaumik.officetask.adapter;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.bhaumik.officetask.R;
import com.example.bhaumik.officetask.interfaces.ItemListener;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Bhaumik on 8/28/2017.
*/
public class ListenerAdapter extends RecyclerView.Adapter<ListenerAdapter.ListenerHolder>{
List<String> list = new ArrayList<>();
ItemListener itemListener;
public ListenerAdapter(List<String> list) {
this.list = list;
}
@Override
public ListenerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listener_item,parent,false);
return new ListenerHolder(view);
}
@Override
public void onBindViewHolder(ListenerHolder holder, final int position) {
holder.textView.setText(list.get(position));
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
itemListener.onItemClick(position);
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public void setOnItemClickListener(ItemListener itemListener){
this.itemListener = itemListener;
}
public static class ListenerHolder extends RecyclerView.ViewHolder{
TextView textView;
public ListenerHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.tv_listener_item);
}
}
}
0 comments:
Post a Comment