Unable to get Click action on RecyclerView - android

I am using RecyclerView in BottomSheet.The data is fetched successfuly but unable to get click action on the RecyclerView items. This is my BottomSheetAdapter code :
public class BottomSheetAdapter extends RecyclerView.Adapter<BottomSheetAdapter.ViewHolder> {
List<UserDetailsModel> userDetailsModelList;
Context context;
private Onitemclicklistenersuggestion listener;
public BottomSheetAdapter(List<UserDetailsModel> userDetailsModelList, Context context) {
this.userDetailsModelList = userDetailsModelList;
this.context = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.bottom_sheet_recycler_items, parent, false);
return new BottomSheetAdapter.ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
UserDetailsModel model = userDetailsModelList.get(position);
holder.firstNameLabel.setText(model.getFirst_name().trim());
}
#Override
public int getItemCount() {
return userDetailsModelList.size();
}
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView firstNameLabel;
public ViewHolder(#NonNull View itemView) {
super(itemView);
firstNameLabel = itemView.findViewById(R.id.firstNameLabel);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (listener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
listener.clicktoupdate(position);
}
}
}
}
public interface Onitemclicklistenersuggestion {
void clicktoupdate(int poistion);
}
public void setOnitemclicklistener(Onitemclicklistenersuggestion mlistener) {
listener = mlistener;
}
}
This is my BottomSheetDialog code :
public class BottomSheetDialog extends BottomSheetDialogFragment
implements BottomSheetAdapter.Onitemclicklistenersuggestion {
bottomSheetAdapter = new BottomSheetAdapter(userDetailsModelList, getActivity());
bottomSheetRecycler.setAdapter(bottomSheetAdapter);
bottomSheetRecycler.setLayoutManager(new LinearLayoutManager(getActivity()));
bottomSheetAdapter.setOnitemclicklistener(new BottomSheetAdapter.Onitemclicklistenersuggestion() {
#Override
public void clicktoupdate(int poistion) {
Log.e("Pos", "Pos");
}
});
bottomSheetAdapter.notifyDataSetChanged();
}
#Override
public void clicktoupdate(int poistion) {
Toast.makeText(getContext(), ""+poistion, Toast.LENGTH_SHORT).show();
}
But after clicking the RecyclerView items neither the Log or the Toast message shows up. Please help me fix it. Thanks in Millions.

Try setting the onClick logic inside your ViewHolder class.
So something like,
class ViewHolder extends RecyclerView.ViewHolder {
TextView firstNameLabel;
public ViewHolder(#NonNull View itemView) {
super(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// show a toast msg or whatever action you need...
}
});
firstNameLabel = itemView.findViewById(R.id.firstNameLabel);
}
}

Related

Should I create adapter for every recyclerview?

I am using diffutil for my recycler adapters and I need many recyclerview for categorization. Should I create adapter for every recyclerview? Same view, same type data, same functions, different data btw
If you check your recycler view's onBindViewHolder method it has your holder parameter which provides you with the instance of different ViewHolders you have and accordingly set the layout for the recycler view.
For example, in my case I had to use a single recyclerView adapter to create an instance Income as well as expense in a list.
This is how my onBindViewHolder looks:
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
DialogDetails d=new DialogDetails();
if (holder instanceof IncomeListHolder){
((IncomeListHolder)holder).categoryText.setText(expensesList.get(position).getCategory());
((IncomeListHolder)holder).incomeText.setText("+"+expensesList.get(position).getAmount());
((IncomeListHolder)holder).incomeText.setTextColor(context.getResources().getColor(R.color.income_color));
}
if (holder instanceof ExpensesListHolder){
((ExpensesListHolder) holder).category.setText(list.get(position-expensesList.size()).getCategory());
((ExpensesListHolder) holder).expenseText.setText("-"+list.get(position- expensesList.size()).getExpense());
((ExpensesListHolder) holder).expenseText.setTextColor(context.getResources().getColor(R.color.expense_color));
}
//.setText(String.valueOf(balanceList.get(position).getIncome()));
}
You also have to create separate viewHolders for all the list data you wish to display,like:
public class IncomeListHolder extends RecyclerView.ViewHolder {
TextView categoryText;
TextView incomeText;
public IncomeListHolder(#NonNull View itemView) {
super(itemView);
categoryText=itemView.findViewById(R.id.main_category_text_view);
incomeText=itemView.findViewById(R.id.expenses_text_view);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int position=getAdapterPosition();
if (listener!=null && position!=RecyclerView.NO_POSITION){
listener.getIncomeData(expensesList.get(position),view);
details.setUpdate(true);
}
}
});
}
}
public class ExpensesListHolder extends RecyclerView.ViewHolder{
TextView category;
TextView expenseText;
public ExpensesListHolder(#NonNull View itemView) {
super(itemView);
category=itemView.findViewById(R.id.main_category_text_view_2);
expenseText=itemView.findViewById(R.id.expenses_text_view_2);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int position=getAdapterPosition();
if (listener!=null && position!=RecyclerView.NO_POSITION){
listener.getExpenseData(list.get(position-expensesList.size()),view);
details.setUpdate(true);
}
}
});
}
}
My whole recycler view adapter:
public class IncomeListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
final int VIEW_TYPE_INCOME=0;
final int VIEW_TYPE_EXPENSE=1;
View view;
List<DataBaseEntity> expensesList=new ArrayList<>();
List<ExpenseDataEntity> list=new ArrayList<>();
Context context;
OnItemClickListener listener;
DialogDetails details=new DialogDetails();
public IncomeListAdapter(Context context) {
this.context = context;
}
//
// View v=LayoutInflater.from(context).inflate(R.layout.fragment_expense_home,null);
//// t=v.findViewById(R.id.income_text_view);
// }
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
if (viewType==VIEW_TYPE_INCOME) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.expenses_list, parent, false);
return new IncomeListHolder(view);
}
if (viewType==VIEW_TYPE_EXPENSE){
view=LayoutInflater.from(parent.getContext()).inflate(R.layout.expense_layout_list,parent,false);
return new ExpensesListHolder(view);
}
return null;
}
public interface OnItemClickListener{
void getIncomeData(DataBaseEntity dataBaseEntity,View view);
void getExpenseData(ExpenseDataEntity expenseDataEntity,View view);
}
public void setOnItemClickListener(OnItemClickListener listener){
this.listener=listener;
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
DialogDetails d=new DialogDetails();
if (holder instanceof IncomeListHolder){
((IncomeListHolder)holder).categoryText.setText(expensesList.get(position).getCategory());
((IncomeListHolder)holder).incomeText.setText("+"+expensesList.get(position).getAmount());
((IncomeListHolder)holder).incomeText.setTextColor(context.getResources().getColor(R.color.income_color));
}
if (holder instanceof ExpensesListHolder){
((ExpensesListHolder) holder).category.setText(list.get(position-expensesList.size()).getCategory());
((ExpensesListHolder) holder).expenseText.setText("-"+list.get(position- expensesList.size()).getExpense());
((ExpensesListHolder) holder).expenseText.setTextColor(context.getResources().getColor(R.color.expense_color));
}
//.setText(String.valueOf(balanceList.get(position).getIncome()));
}
public DataBaseEntity getDataAt(int position){
return expensesList.get(position);
}
public ExpenseDataEntity getExpenseAt(int position){
return list.get(position- expensesList.size());
}
#Override
public int getItemCount() {
return expensesList.size()+list.size();
}
#Override
public int getItemViewType(int position) {
if (position< expensesList.size()){
return VIEW_TYPE_INCOME;
}
if (position-expensesList.size()< list.size()){
return VIEW_TYPE_EXPENSE;
}
return -1;
}
public void setExpensesList(List<DataBaseEntity> expensesList){
this.expensesList=expensesList;
notifyDataSetChanged();
}
public void setList(List<ExpenseDataEntity> list){
this.list=list;
notifyDataSetChanged();
}
// public void setBalanceList(List<BalanceSheetEntity> balanceList){
// this.balanceList=balanceList;
// notifyDataSetChanged();
// }
public class IncomeListHolder extends RecyclerView.ViewHolder {
TextView categoryText;
TextView incomeText;
public IncomeListHolder(#NonNull View itemView) {
super(itemView);
categoryText=itemView.findViewById(R.id.main_category_text_view);
incomeText=itemView.findViewById(R.id.expenses_text_view);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int position=getAdapterPosition();
if (listener!=null && position!=RecyclerView.NO_POSITION){
listener.getIncomeData(expensesList.get(position),view);
details.setUpdate(true);
}
}
});
}
}
public class ExpensesListHolder extends RecyclerView.ViewHolder{
TextView category;
TextView expenseText;
public ExpensesListHolder(#NonNull View itemView) {
super(itemView);
category=itemView.findViewById(R.id.main_category_text_view_2);
expenseText=itemView.findViewById(R.id.expenses_text_view_2);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int position=getAdapterPosition();
if (listener!=null && position!=RecyclerView.NO_POSITION){
listener.getExpenseData(list.get(position-expensesList.size()),view);
details.setUpdate(true);
}
}
});
}
}
}

How to implement multiple callback in RecyclerAdapter?

I have custom ServerRecyclerAdapter in which I implemented onLongClickListener, but now I am having problem where to implement OnClickListener
public class ServerRecyclerAdapter extends RecyclerView.Adapter<ServerRecyclerAdapter.ViewHolder> {
//Interface for callbacks
interface ActionCallback {
void onLongClickListener(Server server);
}
private Context context;
private List<Server> serverList;
private ActionCallback mActionCallbacks;
ServerRecyclerAdapter(Context context, List<Server> serverList) {
this.context = context;
this.serverList = serverList;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_recycler_server, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.bindData(position);
}
#Override
public int getItemCount() {
return serverList.size();
}
void updateData(List<Server> servers) {
this.serverList = servers;
notifyDataSetChanged();
}
//View Holder
class ViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener {
private TextView mNameTextView;
ViewHolder(View itemView) {
super(itemView);
itemView.setOnLongClickListener(this);
mNameTextView = itemView.findViewById(R.id.nameTextView);
}
void bindData(int position) {
Server server = serverList.get(position);
String servername = server.getServerName();
mNameTextView.setText(servername);
}
#Override
public boolean onLongClick(View v) {
if (mActionCallbacks != null) {
mActionCallbacks.onLongClickListener(serverList.get(getAdapterPosition()));
}
return true;
}
}
void addActionCallback(ActionCallback actionCallbacks) {
mActionCallbacks = actionCallbacks;
}
Try this
ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
itemView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
if (mActionCallbacks != null) {
mActionCallbacks.onLongClickListener(serverList.get(getAdapterPosition()));
}
return true;
}
});
}
how about ...
extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener
then it will suggest you where to insert the override for that interface method.
For others how to solve this simple question :)
I've added in ActionCallback
void onClickListener(Server server);
and in ViewHolder
class ViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener, View.OnClickListener {
private TextView mNameTextView;
ViewHolder(View itemView) {
super(itemView);
itemView.setOnLongClickListener(this);
itemView.setOnClickListener(this);
mNameTextView = itemView.findViewById(R.id.nameTextView);
}
.
.
.
#Override
public void onClick(View view) {
if (mActionCallbacks != null) {
mActionCallbacks.onClickListener(serverList.get(getAdapterPosition()));
}
}
Thx to #MikeM. for helping me solve it

Recycler View item on Click not working even after following the listener procedure

I am Trying to start an activity from a recycler view item and followed the procedures yet the onClick is not calling in my app. I have checked the hasOnClickListeners() and that returned true. please help me.
public class ShopAdapter extends
RecyclerView.Adapter<ShopAdapter.ViewHolder> {
public List<String> shopDetails;
public View view;
public Context context;
public ShopAdapter(List<String> shopDetails){
this.shopDetails = shopDetails;
}
#Override
public ShopAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int
viewType) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_single,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ShopAdapter.ViewHolder holder, int position) {
final String shop_description_data = "ONE";
final String shop_name_data = "TWO";
holder.setShopNameView(shop_name_data);
holder.setShopInfoView(shop_description_data);
Here I have set the item click listener from view holder
holder.setItemClickListener(new ItemClickListener() {
#Override
public void onItemClicked(View view, int position) {
Log.d("GOUSE","on Item Click");
}
});
}
private void startShopActivity(String shop_name_data, String shop_description_data,View view) {
}
#Override
public int getItemCount() {
return shopDetails.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView shopDescView;
private TextView shopNameView;
private View mView;
private ItemClickListener onItemClickListener;
private CardView shopItem;
public ViewHolder(View itemView) {
super(itemView);
mView = itemView;
Here I set the on click listener to my listener interface
mView.setOnClickListener(this);
Log.d("Listener",mView.hasOnClickListeners()+"");
}
public void setShopInfoView(String shopDescText){
shopDescView = mView.findViewById(R.id.shop_info);
shopDescView.setText(shopDescText);
}
public void setShopNameView(String shopNameText){
shopNameView = mView.findViewById(R.id.shop_name);
shopNameView.setText(shopNameText);
}
public void startShop(String shopName,String shopDesc){
Intent shopActivityMainIntent = new Intent(mView.getContext(),MainActivity.class);
shopActivityMainIntent.putExtra("shopName",shopName);
shopActivityMainIntent.putExtra("shopDescription",shopDesc);
mView.getContext().startActivity(shopActivityMainIntent);
}
public void setItemClickListener(ItemClickListener itemClickListener){
Log.d("GOUSE","set item listener");
this.onItemClickListener = itemClickListener;
}
#Override
public void onClick(View view) {
Log.d("GOUSE","set item clicked");
onItemClickListener.onItemClicked(view,getAdapterPosition());
}
}
}
make some changes in adapter used interface to handle recycler view click event like below code ...
private onItemClickListener onItemClickListener;
/**
* this interface is used to handle recycler view click event.
*/
public interface onItemClickListener {
void onItemClick(Cricketer cricketer);
}
public void setOnItemClickListener(onItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
#Override
public void onBindViewHolder(#NonNull ItemViewHolder holder, final int position) {
final Cricketer cricketer = mCricketerList.get(position);
holder.mTvTitle.setText(cricketer.getName());
holder.mTvDescription.setText(cricketer.getCountry());
holder.mTvDate.setText(cricketer.getDateOfBirth());
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onItemClickListener.onItemClick(cricketer);
}
});
}
when you bind adapter into recycler view that time get click event like below code..
/**
* this method used to bind adapter into recycler view.
*/
private void setAdapter() {
recyclerViewAdapter = new RecyclerViewAdapter(this, mCricketerList);
mRvData.setAdapter(recyclerViewAdapter);
recyclerViewAdapter.notifyDataSetChanged();
recyclerViewAdapter.setOnItemClickListener(new RecyclerViewAdapter.onItemClickListener() {
#Override
public void onItemClick(Cricketer cricketer) {
Toast.makeText(getApplicationContext(), "Cricketer Name::" + cricketer.getName(), Toast.LENGTH_SHORT).show();
}
});
}

How to get image from recyclerview position clicked data and set it into another imageview?

I have an app which contains recyclerview with imageview and text.When I click on item based on position that is imageview i want image of that image view and set it to another Imageview.To do this I have made interface and implements its method in Activity but need to know that how do i do that
Adapter code:
public interface OnItemClicked {
void onItemClicked(int position);
}
public AvatarListAdapter(Context context, int[] arrayList) {
this.mContext = context;
this.dataModel = arrayList;
}
#Override
public AvatarListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.avatar_items, parent, false);
return new AvatarListAdapter.ViewHolder(view);
}
#Override
public void onBindViewHolder(AvatarListAdapter.ViewHolder holder, final int position) {
holder.mAvatarImage.setImageResource(dataModel[position]);
holder.mAvatarImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onClick.onItemClicked(position);
}
});
}
public void setOnClick(OnItemClicked onClick) {
this.onClick = onClick;
}
#Override
public int getItemCount() {
return dataModel.length;
}
public class ViewHolder extends RecyclerView.ViewHolder {
private CircleImageView mAvatarImage;
public ViewHolder(View itemView) {
super(itemView);
mAvatarImage = itemView.findViewById(R.id.img_avatar);
}
}
}
Activity code:
private void initializeActions() {
int numberOfColumns = 3;
mAvatarImages.setLayoutManager(new GridLayoutManager(AvatarProfileImage.this, numberOfColumns));
mAdapter = new AvatarListAdapter(AvatarProfileImage.this, avatar);
mAvatarImages.setAdapter(mAdapter);
mAdapter.setOnClick(this);
}
#Override
public void onItemClicked(int position) {
String pos = "You clicked at position " + position;
Toast.makeText(getApplicationContext(), "You clicked position" + pos, Toast.LENGTH_LONG).show();
mAvatarImages.setBackgroundResource(avatar[position]);
}
You can get data by position on OnClick().As you are using local drawable for image resource, One way to do is Modify your viewHolder as follows.This is just a one way you can also use ViewTag.
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private Circle mAvatarImage;
public ViewHolder(View itemView) {
super(itemView);
mAvatarImage = itemView.findViewById(R.id.img_avatar);
mAvatarImage.setOnClickListener(this);
}
#Override
public void onClick(View v) {
int postion =getAdapterPosition();
int image =arrayList[postion];
// Use this image to forward
}
}
Remove OnClick from onBindViewHolder.
Implement RecycleView onClick() method and get specified data while onclick position.have look
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private Circle mAvatarImage;
public ViewHolder(View itemView) {
super(itemView);
mAvatarImage = itemView.findViewById(R.id.img_avatar);
mAvatarImage.setOnClickListener(this);
}
#Override
public void onClick(View v) {
int position=arrayList[getAdapterPosition()];
String imagepath = arraylist.get(position).getImagePath
//you can use imagepath anywhere you want either be put in bundle or intent call
}
}
Adapter code should look like this
private final OnItemClickListener listenerOnItemClick;
public interface OnItemClickListener {
void onItemClick(int position); }
public AvatarListAdapter(Context context, int[] arrayList, OnItemClickListener listenerOnItemClick) {
this.mContext = context;
this.dataModel = arrayList;
this.listenerOnItemClick=listenerOnItemClick;
}
#Override
public AvatarListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.avatar_items, parent, false);
return new AvatarListAdapter.ViewHolder(view); }
#Override
public void onBindViewHolder(AvatarListAdapter.ViewHolder holder, final int position) {
holder.mAvatarImage.setImageResource(dataModel[position]);
holder.mAvatarImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onClick.onItemClicked(position);
}
}); }
public void setOnClick(OnItemClicked onClick) {
this.onClick = onClick; }
#Override
public int getItemCount() {
return dataModel.length; }
public class ViewHolder extends RecyclerView.ViewHolder {
private CircleImageView mAvatarImage;
public ViewHolder(View itemView) {
super(itemView);
mAvatarImage = itemView.findViewById(R.id.img_avatar);
} }
Activity Code
AvatarListAdapter.OnItemClickListener onItemClickListener = new AvatarListAdapter.OnItemClickListener() {
#Override
public void onItemClick(int position) {
//change your Image here
String pos = "You clicked at position " + position;
Toast.makeText(getApplicationContext(), "You clicked position" + pos, Toast.LENGTH_LONG).show();
mAvatarImages.setBackgroundResource(avatar[position]);
}
};
private void initializeActions() {
int numberOfColumns = 3;
mAvatarImages.setLayoutManager(new GridLayoutManager(AvatarProfileImage.this, numberOfColumns));
mAdapter = new AvatarListAdapter(AvatarProfileImage.this, avatar,onItemClickListener );
mAvatarImages.setAdapter(mAdapter);
mAdapter.setOnClick(this);
}

Android RecyclerView onClick in different Activity

Everything works well and my onClick in my recyclerView is working in getting the positions of my items, but what my design calls for is to be able to click an item of the recyclerView and open up a new activity (as a popover or pop up). I can achieve this but my problems comes with the information I need to display on the popover. The information comes like this inside the activity (inside a Firebase value call)
attributeList.removeAll(attributeList);
for (DataSnapshot child : dataSnapshot.child("Attribute").getChildren()){
Attribute attribute = child.getValue(Attribute.class);
attribute_list newAttributeList = new attribute_list( attribute.Name + ": " + attribute.Value);
attributeList.add(newAttributeList);
}
attributeAdapter = new attribute_list_adapter(attributeList, getContext());
recyclerAttribute.setAdapter(attributeAdapter);
This works perfectly for displaying the information, but there's more then just a "value" and a "name" associated with the click.
Basically when I select an item, I need to get the position of the item clicked (which I have) and compare it to the position inside attributeList so I can call a Firebase call (or pass the data somehow) to the popover to display values from the "Attribute" class (such as Name, Value, Description, and another list (recyclerView).
My recyclerView:
public class attribute_list_adapter extends RecyclerView.Adapter<attribute_list_adapter.ViewHolder> {
private List<attribute_list> listItems;
private Context context;
public attribute_list_adapter(List<attribute_list> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.attribute_list, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
attribute_list listItem = listItems.get(position);
holder.txtTitle.setText(listItem.getTxtTitle());
}
#Override
public int getItemCount() {
return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView txtTitle;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
txtTitle = (TextView) itemView.findViewById(R.id.txtTitle);
}
#Override
public void onClick(View v) {
}
}
}
This is example:
public class attribute_list_adapter extends RecyclerView.Adapter<attribute_list_adapter.ViewHolder> {
private List<attribute_list> listItems;
private Context context;
public attribute_list_adapter(List<attribute_list> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.attribute_list, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.itemView.setOnClickListener(new View.onClickListener() {
#Override
public void onClick(View v) {
onItemClickListener.onItemClick(position);
}
});
}
#Override
public int getItemCount() {
return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView txtberita;
ImageView imgberita;
TextView txtnama;
public ViewHolder(View itemView) {
super(itemView);
txtnama = (TextView) itemView.findViewById(R.id.txtnama);
txtberita = (TextView) itemView.findViewById(R.id.txtberita);
imgberita = (ImageView) itemView.findViewById(R.id.imgberita);
}
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener){
this.onItemClickListener = onItemClickListener;
}
OnItemClickListener onItemClickListener;
public interface OnItemClickListener{
void onItemClick(int position);
}
}
your Activity. in Oncreate()
public class TestActivity extends AppCompatActivity implements attribute_list_adapter.OnItemClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
attribute_list_adapter adapter = new attribute_list_adapter(listItems, this);
adapter.setOnItemClickListener(this);
}
#Override
public void onItemClick(int position) {
// code here
}
}
Create an interface something like
public interface OnSingleItemClickListener{
void onSingleItemClick(int position);
}
Then implement it on your ViewHolder like this
public class ViewHolder extends RecyclerView.ViewHolder implements OnSingleItemClickListener {
public ViewHolder(View itemView) {
super(itemView);
}
#Override
void onSingleItemClick(int position){
if(listItems.get(position) == listItems.get(getAdapterPosition)){
// TODO do something here
}
}
now on your OnBindViewHolder inside your adapter you must do this.
holder.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
holder.onSingleItemClick(position);
}
}):

Categories

Resources