Communication between adapter and fragment in android - android

I am stuck need your kind help, i have an activity in android user sign up, and apart from general information i have a list of existing users/entities shown in recyclerview and each item of recyclerview list contains a follow button, i want to captures the position/ buttons that were clicked to check which users/entities the new user has followed. Any suggest how to do it?
I had customer adapter and recycler view, Check the attached image for clarity. I want to create an array list that will contain ids of each list item whose button is clicked.
RecyclerView containing list item and follow button
//this adapter use on admin
public class FollowLocalBusinessAdapter extends RecyclerView.Adapter {
AlertDialog.Builder alertDialogBuilder;
private Context context;
private List<FollowLocalBusinessMC> list;
private LayoutInflater layoutInflater;
private DatabaseReference mDatabase;//database reference
private FollowLocalBusinessMC localBusinessMC;
private ListAdapterListener mListener;
public FollowLocalBusinessAdapter(Context context, List<FollowLocalBusinessMC> list) {
this.context = context;
this.list = list;
layoutInflater = LayoutInflater.from(context);
alertDialogBuilder = new AlertDialog.Builder(context);
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int position) {
View view = layoutInflater.inflate(R.layout.list_item_local_business, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(final FollowLocalBusinessAdapter.MyViewHolder holder, final int position)//is called
{
localBusinessMC = list.get(position);
if (holder != null) {
if (holder.followBtn.getText().equals("Follow")) {
holder.followBtn.setBackgroundResource(R.drawable.follow_btn_bg);
holder.followBtn.setTextColor(context.getResources().getColor(R.color.colorBlueButton));
} else {
holder.followBtn.setBackgroundResource(R.drawable.following_btn_bg);
holder.followBtn.setTextColor(context.getResources().getColor(R.color.colorWhite));
}
holder.mainHeading.setText(localBusinessMC.getMainHeading());
holder.subHeading.setText(localBusinessMC.getSubHeading());
Glide.with(context).load(localBusinessMC.getIcon()).into(holder.icon);
holder.followBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context, "" + list.get(position).getId(), Toast.LENGTH_SHORT).show();
if (holder.followBtn.getText().equals("Follow")) {
holder.followBtn.setText("Following");
holder.followBtn.setTextColor(context.getResources().getColor(R.color.colorWhite));
holder.followBtn.setBackgroundResource(R.drawable.following_btn_bg);
} else {
holder.followBtn.setText("Follow");
holder.followBtn.setBackgroundResource(R.drawable.follow_btn_bg);
holder.followBtn.setTextColor(context.getResources().getColor(R.color.colorBlueButton));
}
}
});
}
}
#Override
public int getItemCount() {
return list.size();
}
public interface ListAdapterListener { // create an interface
void onClickAtOKButton(int position); // create callback function
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView mainHeading, subHeading;
ImageView icon;
Button followBtn;
MyViewHolder(View v) {
super(v);
mainHeading = v.findViewById(R.id.main_lbsr_id);
subHeading = v.findViewById(R.id.sub_lbsr_id);
icon = v.findViewById(R.id.icon_lbsr_id);
followBtn = v.findViewById(R.id.followBtn_lbsr_id);
}
}
}

create an interface in your Adapter with one method :
public interface CallBack{
void onItemClicked(int position , /*anyThing else*/);
}
then get an instance of interface in your Adapter :
private YourAdapter.CallBack;
in your Adpater constructor get object of CallBack :
public YourAdapter(Context context, List</* */> list, YourAdapter.CallBackcallback callBack) {
this.context = context;
this.list= list;
this.callback = callback;
}
#Override
public void onBindViewHolder(#NonNull ChoosenExerciseViewHolder holder, final int position) {
holder.View.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
callback.onItemClicked(
//pass any parameter youe defined
//for ex : position , ///);
}
});
}
then in your Activity/Fragment :
private YourAdapter.CallBack callback= (position, /* */ ) -> {
// do what you want
};
YourAdapter adapter = new YourAdapter(getContext(), calback, list);
receyclerview.setAdapter(adapter )

Related

Recyclerview single item selection not working correctly

I've implemented a recyclerview with staggered gridLayout containing about 31 items in the arrayList, recyclerview is working correctly, but I faced issue relating to single item selection.
When I select the value till "26" as shown in figure, its working fine
But, when I select the value after "26", the values from top most item are also selected, as shown in this next figure.
I require to only select one item at a time.
I've implemented the following code in my adapter class
public class DialogAdapter extends
RecyclerView.Adapter<DialogAdapter.DialogHolder>
{
// components
public Context context;
public ArrayList<AlertDialogModel> dialogArrayList = new
ArrayList<AlertDialogModel>();
private final ArrayList<Integer> selected = new ArrayList<>();
private int lastCheckedPosition = -1;
public Interface interface;
// parameterized constructor
public DialogAdapter(Context context, ArrayList<AlertDialogModel>
dialogArrayList,Interface interface)
{
this.context = context;
this.dialogArrayList = dialogArrayList;
this.interface = interface;
}
#NonNull
#Override
public DialogHolder onCreateViewHolder(#NonNull ViewGroup parent, int
viewType)
{
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_cardview,parent,false);
DialogHolder dialogHolder = new DialogHolder(view);
return dialogHolder;
}
#Override
public void onBindViewHolder(#NonNull final DialogHolder holder, final int position)
{
final AlertDialogModel alertDialogModel = dialogArrayList.get(position);
holder.textView.setText(alertDialogModel.getDisplayValue());
if(lastCheckedPosition == position)
{
holder.textView.setTextColor(context.getResources().getColor(R.color.white));
holder.textView.setBackground(context.getResources().getDrawable(R.drawable.circular_shape_selection));
}
else
{
}
holder.textView.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
lastCheckedPosition = position;
notifyDataSetChanged();
holder.textView.setTextColor(context.getResources().getColor(R.color.white));
holder.textView.setBackground(context.getResources().getDrawable(R.drawable.circular_shape_selection));
interface.getSelectedValue(alertDialogModel.getDisplayValue());
}
});
}
#Override
public int getItemCount()
{
return dialogArrayList.size();
}
public static class DialogHolder extends RecyclerView.ViewHolder
{
public TextView textView;
public DialogHolder(View itemView)
{
super(itemView);
textView = (TextView)itemView.findViewById(R.id.textView);
}
}
}
Can anyone relate my code and identify the issue ?
holder.textView.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
lastCheckedPosition = position;
notifyDataSetChanged();
holder.textView.setTextColor(context.getResources().getColor(R.color.white));
holder.textView.setBackground(context.getResources().getDrawable(R.drawable.circular_shape_selection));
interface.getSelectedValue(alertDialogModel.getDisplayValue());
//below line is important to remove previous selected position from the variable
lastCheckedPosition = -1;
}
});
you should put the text view to the original state:
if(lastCheckedPosition == position)
{
holder.textView.setTextColor(context.getResources().getColor(R.color.white));
holder.textView.setBackground(context.getResources().getDrawable(R.drawable.circular_shape_selection));
}
else
{
holder.textView.setTextColor(context.getResources().getColor(R.color.transparent));
holder.textView.setBackground(null));
}

Update ArrayList for RecyclerView.Adapter from RecyclerView.ViewHolder?

In my MainActivity I have something like this:
recyclerAdapter = new RecyclerAdapter(this, arrayList);
recyclerView.setAdapter(recyclerAdapter);
To update my RecyclerView, I do: (in MainActivity, too)
Item item = new Item();
item.setId(id);
item.setMessage(message);
arrayList.set(position, item);
recyclerAdapter.notifyItemChanged(position);
I want to have a button within every RecyclerView list item which can enable/disable the current entry. Therefore I need to get the current toggle state.
My problem is, how can I update the RecyclerView in MainActivity from within my ViewHolder?
RecyclerView.ViewHolder:
public class SetViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{//, View.OnLongClickListener{
public ImageView toggle;
public SetViewHolder(View itemView){
super(itemView);
toggle = (ImageView) itemView.findViewById(R.id.img_main_card_toggle);
toggle.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v.getId() == toggle.getId()) {
int clicked_position = getAdapterPosition();
//TODO update Item of RecyclerView of MainActivity
}
}
My Recycler.Adapter:
public class RulesAdapter extends RecyclerView.Adapter<SetViewHolder>{
private Activity activity;
List<Item> items = Collections.emptyList();
private OnTapListener onTapListener;
public RulesAdapter(Activity activity, List<Item> items){
this.activity = activity;
this.items = items;
}
#Override
public SetViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rules_list_item,parent,false);
return new SetViewHolder(view);
}
#Override
public void onBindViewHolder(SetViewHolder holder, final int position) {
holder.recevied_message.setText(items.get(position).getReceived_message());
holder.reply_message.setText(items.get(position).getReply_message());
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(onTapListener != null){
onTapListener.OnTapView(position);
}
}
});
}
#Override
public int getItemCount() {
return items.size();
}
public void setOnTapListener(OnTapListener onTapListener){
this.onTapListener = onTapListener;
}
}
Define an Interface.
public interface OnRecyclerItemToggleClickListener {
void onToggleClick(int adapterPosition);
}
Implement this in your adapter. And pass the adapter instance to the ViewHolder through a setter method in the onCreateViewHolder callback.
public class SetViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{//, View.OnLongClickListener{
public ImageView toggle;
private OnRecyclerItemToggleClickListener listener;
public SetViewHolder(View itemView){
super(itemView);
toggle = (ImageView)
itemView.findViewById(R.id.img_main_card_toggle);
toggle.setOnClickListener(this);
}
public void setRecyclerItemToggleClickListener( OnRecyclerItemToggleClickListener listener) {
this.listener = listener;
}
#Override
public void onClick(View v) {
if (v.getId() == toggle.getId()) {
int clicked_position = getAdapterPosition();
//TODO update Item of RecyclerView of MainActivity
listener.onToggleClick(clicked_position);
}
}
In your adapter:
#Override
public SetViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rules_list_item,parent,false);
SetViewHolder holder = new SetViewHolder(view);
holder.setOnRecyclerItemToggleClickListener(this);
return holder;
}
#Override public void onToggleClick(int adapterPosition) {
// you will get the callback here when the toggle button is clicked.
// you can update the ArrayList here.
// Or similarly, call an interface method to Activity.
}
And call this onToggleClick method from ViewHolder when the toggle is clicked. You will get a callback to adapter, where you can update the ArrayList. Otherwise you can have another similar Interface to give callback to Activity from the adapter.
You just have to add a static function and make items static too:
public static ArrayList<Items> getAreayList()
{
return items;
}
And get this function in MainActivity:
onStart()

Get the internal details of the items in recycler view if the items are shuffeled

I have a recycler view, which consists of list. The items in the list have their own details like this (each item will have image, id, name, description), and the items are shuffeled, not in order.
Example : recyclerview item position = 0, list item id= 7.
recyclerview item position = 1, list item id= 5.
recyclerview item position = 2, list item id= 12.
So i need to get the list item id on recycler on click. If i click on recyclerview position = 0, If i have to get the item id as 7. So that i can work on that furthur based on that id.
My example code is
recommendedrecyclerView=(RecyclerView)view.findViewById(R.id.recommended_recycler_view);
recommendedrecyclerView.setNestedScrollingEnabled(false);
recommendedrecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),OrientationHelper.VERTICAL,true));
recommendedrecyclerView.setItemAnimator(new DefaultItemAnimator());
I am getting data from server and setting it to adapter
setrecommendedAppsContent(response.body().getData());
public void setrecommendedAppsContent(List<FeaturedAppsData> data){
if (data!=null&&data.size()>0) {
recommendedAdapter = new RecommendedAdapter(mCurrentContext, data);
recommendedrecyclerView.setAdapter(recommendedAdapter);
recommendedAdapter.setClickListener(this);
}
}
This is my adapter class
public class RecommendedAdapter extends RecyclerView.Adapter<RecommendedAdapter.ItemViewHolder> {
private LayoutInflater inflater = null;
private Context context;
private List<FeaturedAppsData> itemsData;
private ClickListener clicklistener = null;
public TextView recommendedAppTitle,recommendedAppCategory,recommendedAppDescription;
public ImageView recommendedAppIcon;
Button recommendedBtn;
String appId;
public void setClickListener(ClickListener clickListener) {
this.clicklistener = clickListener;
}
public RecommendedAdapter(Context context, List<FeaturedAppsData> itemsData) {
this.context = context;
this.itemsData = itemsData;
}
#Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
holder.recommendedAppTitle.setText(itemsData.get(position).getName());
holder.recommendedAppCategory.setText(itemsData.get(position).getApp_description());
holder.recommendedAppDescription.setText(itemsData.get(position).getOffer_description());
holder.recommendedAppBtn.setText(itemsData.get(position).getButton_text());
String imageUrl = String.valueOf(itemsData.get(position).getImage().getUrl());
Glide.with(context).load(ApiConstant.ApiBaseUrl + imageUrl).into(recommendedAppIcon);
appId=itemsData.get(position).getId();
}
#Override
public int getItemCount() {
int size = 0;
if (itemsData != null && itemsData.size() > 0) {
size = itemsData.size();
}
return size;
}
public class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView recommendedAppTitle,recommendedAppCategory,recommendedAppDescription,offerDescription,offerDetailDescription,rewardDetail;
public ImageView recommendedAppIcon;
public Button recommendedAppBtn;
public ArrayList<FeaturedAppsData> dataItems;
private Context context;
public ItemViewHolder(Context context, View itemView, int viewType) {
super(itemView);
this.context = context;
itemView.setOnClickListener(this);
recommendedAppTitle = (TextView) itemView.findViewById(R.id.recommended_app_title);
recommendedAppCategory = (TextView) itemView.findViewById(R.id.recommended_app_category);
recommendedAppDescription = (TextView) itemView.findViewById(R.id.recommended_app_description);
recommendedAppIcon = (ImageView) itemView.findViewById(R.id.recommended_app_icon);
recommendedAppBtn=(Button)itemView.findViewById(R.id.recommended_app_card_btn);
}
#Override
public void onClick(View v) {
if (clicklistener != null) {
clicklistener.itemClicked(v, getAdapterPosition());
}
}
}
#Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = null;
inflater = LayoutInflater.from(context);
view = inflater.inflate(R.layout.recommended_item_layout, parent, false);
recommendedAppIcon=(ImageView)view.findViewById(R.id.recommended_app_icon);
return new ItemViewHolder(context, view, viewType);
}
}
I am not sure of the Onclick method. So please suggest me as required along with Onclick event.
The setOnClickListener inside the ViewHolder is a good way.
Considering your current solution, the easiest way is to change the ClickListener interface, put the data inside the Holder during onBindViewHolder and then pass it to the listener during onClick. Like this:
Change the view holder fields to:
public class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView recommendedAppTitle,recommendedAppCategory,recommendedAppDescription,offerDescription,offerDetailDescription,rewardDetail;
public ImageView recommendedAppIcon;
public Button recommendedAppBtn;
public FeaturedAppsData data; // <<< ADD THIS
// remove that u don't need public ArrayList<FeaturedAppsData> dataItems;
// remove that u don't need private Context context;
... the rest of your holder
then inside onBindViewHolder
#Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
holder.data = itemsData.get(position);
... the rest on bind
then inside onClick, inside the holder:
#Override
public void onClick(View v) {
if (clicklistener != null) {
clicklistener.itemClicked(v, data.getId());
}
}

Save state of the item selected in RecyclerView when view is reused while scrolling

I have created an Adapter and ViewHolder for the recyclerView. I bind the imageButton of the itemView inside the Viewholder. And have set an onClickListener inside the onBindViewHolder.
Everything works as expected but the issue is now when i scroll down the list the selected state of the imageButton changes for the selected item and some of the items on the bottom of the list already appear
selected.
Here is the some code
public class ListViewHolder extends RecyclerView.ViewHolder {
TextView textViewUserName;
ImageButton imageButtonCheckMark;
public ListViewHolder(View itemView) {
super(itemView);
textViewUserName = (TextView) itemView.findViewById(R.id.textView_user_name);
imageButtonCheckMark = (ImageButton) itemView.findViewById(R.id.imageButton_add_user);
}
}
Adapter class
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListViewHolder> {
private LayoutInflater inflater;
private Context context;
private List<Metadata> list;
public static boolean isUserSelected = false;
ListInterface listener;
public ListAdapter(Context context, List<Metadata> data, ListInterface listener) {
Log.d(TAG, "Passed list to adapter : " + data.size());
inflater = LayoutInflater.from(context);
this.context = context;
this.list = data;
this.listener = listener;
}
#Override
public ListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.item_following_people, parent, false);
return new ListViewHolder(view);
}
#Override
public void onBindViewHolder(ListViewHolder holder, int position) {
holder.textViewUserName.setText(list.get(position).name);
holder.imageButtonCheckMark.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!isUserSelected) {
holder.imageButtonCheckMark.setImageResource(R.drawable.checkmark_green);
isUserSelected = true;
} else {
holder.imageButtonCheckMark.setImageResource(R.drawable.checkmark_grey);
isUserSelected = false;
}
listener.onUserSelected(isUserSelected, holder.getLayoutPosition());
}
});
}
#Override
public int getItemCount() {
return list.size();
}
}
Inteface class
public interface ListInterface {
public void onUserSelected(boolean userStatus, int listPosition);
}
Interface callback listener inside activity
// Interface callback listener implemented in the activity class
private ArrayList<Metadata> metadataList = new ArrayList<>();
ArrayList<String> selectedUserIdList = new ArrayList<>();
#Override
public void onUserSelected(boolean isUserSelected, int pos) {
if (isUserSelected) {
Log.d(TAG, "Selected user Id: " + metadataList.get(pos).userID);
followersSelectedIdList.add(metadataList.get(pos).userID);
} else {
Log.d(TAG, "Removed user Id: " + metadataList.get(pos).userID);
followersSelectedIdList.remove(metadataList.get(pos).userID);
}
}
Add one attribute in your model class that will save the state as your button is selected or not, write code in onBindViewHolder() to manage button view like this,
if(yourModel.getIsSelected()){
// positive view
} else {
//negative view
}
You see this behavior because views are reused (recycled) as you scroll. You need to save the state for all buttons in the list and set the state on each button in onBindViewHolder() as it becomes visible.

How to get selected item from RecyclerView list?

I have created a interface for extending functionality of Recycler View and making possible to listen click on every item of list, but i can't make it to work. Nothing happens when i click on any item. Here is my code:
// METHOD FOR INITIALIZATION OF RECYCLER VIEW
public void initRecyclerView() {
// SETTING ARRAY FOR CATEGORIES
final CategoryItem categoryItem[] = { new CategoryItem("Birthday", R.drawable.birthday_image),
new CategoryItem("Anniversary", R.drawable.anniversary_image),
new CategoryItem("Anniversary", R.drawable.anniversary_image),
new CategoryItem("Anniversary", R.drawable.anniversary_image),
new CategoryItem("Anniversary", R.drawable.anniversary_image),
new CategoryItem("Anniversary", R.drawable.anniversary_image),
new CategoryItem("Anniversary", R.drawable.anniversary_image),
new CategoryItem("Anniversary", R.drawable.anniversary_image)};
// FINDING RECYCLER VIEW IN LAYOUT
recyclerView = (RecyclerView) findViewById(R.id.list);
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
adapter = new CategoriesListAdapter(categoryItem);
recyclerView.setAdapter(adapter);
// SETTING ON CLICK LISTENER ON ADAPTER
adapter.setOnItemClickListener(new OnItemClickListener() {
#Override
public void setOnItemClick(View view, int position) {
String clickedItem = String.valueOf(position);
if ( clickedItem.equals("Birthday") ) {
Toast.makeText(MainActivity.this, "Clicked on Birthday!", Toast.LENGTH_SHORT).show();
}
}
});
recyclerView.setItemAnimator(new DefaultItemAnimator());
}
UPDATE:
adapter
public class CategoriesListAdapter extends RecyclerView.Adapter<CategoriesListAdapter.ViewHolder> {
private CategoryItem[] categoryItems;
private static OnItemClickListener onItemClickListener;
public CategoriesListAdapter(CategoryItem[] categoryItems) {
this.categoryItems = categoryItems;
}
#Override
public CategoriesListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_row, parent, false);
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
#Override
public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
viewHolder.txtViewTitle.setText(categoryItems[position].getTitle());
viewHolder.imgViewIcon.setImageResource(categoryItems[position].getImage());
}
// inner class to hold a reference to each item of RecyclerView
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView txtViewTitle;
public ImageView imgViewIcon;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.title);
imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.image);
itemLayoutView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
onItemClickListener.setOnItemClick(v, getAdapterPosition());
}
}
public void setOnItemClickListener (final OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
// Return the size of your itemsData (invoked by the layout manager)
#Override
public int getItemCount() {
return categoryItems.length;
}
}
INTERFACE:
public interface OnItemClickListener {
public void setOnItemClick(View view, int position);
}
It should display toast message if birthday is selected, but it's not displaying anything. I'm instancing array of class and i don't know how to listen clicked item. The answers there didn't help me solving my problem.
Currently you are checking your position against string "Birthday" in OnItemClickListener method setOnItemClick
String clickedItem = String.valueOf(position);
if ( clickedItem.equals("Birthday") ) {
...
}
which never gonna be true. Try getting item for position and checking its first parameter against "Birthday" or bring your Toast out from that if statement and just print out position number for beginning to understand what's going on in there
#Override
public void setOnItemClick(View view, int position) {
Toast.makeText(MainActivity.this, "Clicked item on position: " + position, Toast.LENGTH_SHORT).show();
}
My Solution:
Steps Involved
Created an interface RecyclerviewClickListener
Implemented interface on an activity/fragment
Passed the interface to the adapter
Called an interface method on an item clicked onBindViewHolder.
RecyclerviewClickListener interface
//Created an interface RecyclerviewClickListener
public interface RecyclerviewClickListener
{
void recyclerviewClickListener(int position);
}
NewFragment
//Implemented interface on an activity/fragment
public class NewFragment extends Fragment implements RecyclerviewClickListener
{
//Passed the interface to the adapter
adapter = new Adapter(getContext(), dataList, this);
#Override
public void recyclerviewClickListener(int position)
{
//Do something here with the position from a recyclerview
//e.g dataList.get(position);
}
}
Adapter
public class UniversityAdapter extends RecyclerView.Adapter<UniversityAdapter.ViewHolder>
{
private final ArrayList<User> dataList;
private final Context context;
private RecyclerviewClickListener listener;
public Adapter(Context context, ArrayList<User> dataList, RecyclerviewClickListener itemListener)
{
context = context;
dataList = universityArrayList;
listener = itemListener;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position)
{
holder.userName.setOnclickListener(view -> listener.recyclerviewClickListener(position));
}
}

Categories

Resources