I'm trying to set an onclick method in my RecyclerView. It worked on another project but not here. I searched on the web but can't figure it out. Not even the toast is popping up. Can someone, please, explain to me where I am making a mistake?
Here's my code:
private Context mContext;
private MenuImages[] mMenuImages;
public MenuAdapter(Context context, MenuImages[] menuImages){
mContext = context;
mMenuImages = menuImages;
}
#Override
public MenuViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.menu_list_item, parent, false);
MenuViewHolder viewHolder = new MenuViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(MenuViewHolder holder, int position) {
holder.bindMenu(mMenuImages[position]);
}
#Override
public int getItemCount() {
return mMenuImages.length;
}
public class MenuViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener{
public ImageView mImageView;
public TextView mTextView;
public MenuViewHolder(View itemView) {
super(itemView);
mImageView = (ImageView) itemView.findViewById(R.id.itemImageView);
mTextView = (TextView) itemView.findViewById(R.id.textView2);
itemView.setOnClickListener(this);
}
public void bindMenu(MenuImages menuImage){
mImageView.setImageBitmap(menuImage.getImageMenu());
mTextView.setText(menuImage.getTitleImageMenu());
}
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), getLayoutPosition(), Toast.LENGTH_LONG).show();
}
}
in my case I was using RecyclerView in InterceptorCardView of other class wich was preventing onClickListener of recyclerview,
when I changed it with CardView and it worked perfectly
Note: I removed clickable = "false" ,focusable = "false".
it may be late but in your layout xml file you should add attribute clickable = "false" and also focusable = "false" to the children view ,textview and imageview in your case
Related
so I have an android project that makes use of recyclerview and cardview, where I set a background imageview on click of a card. This is my code:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.Viewholder> {
private ArrayList <Integer> mImages = new ArrayList<>();
public RecyclerViewAdapter( Context mContext, ArrayList<Integer> mImages) {
this.mImages = mImages;
}
#NonNull
#Override
public Viewholder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.itemlist,viewGroup,false);
return new Viewholder(view);
}
#Override
public void onBindViewHolder(#NonNull final Viewholder viewholder, int i) {
viewholder.img.setImageResource(mImages.get(i));
}
#Override
public int getItemCount() {
return mImages.size();
}
public class Viewholder extends RecyclerView.ViewHolder implements View.OnClickListener{
ImageView img;
ImageView imv;
public Viewholder(#NonNull View itemView) {
super(itemView);
img = itemView.findViewById(R.id.imgview);
imv = itemView.findViewById(R.id.imageView4);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
Log.d("TEST", "Clicked");
int position = getLayoutPosition();
imv.setImageResource(R.drawable.vehicles);
}
}
}
But it doesn't work on click and the app just crashes. How do I resolve this issue?
You must define your itemView as clickable for receiving click events.
android:clickable="true"
please try move onClickListener to onBindViewHolder
#Override
public void onBindViewHolder(#NonNull final Viewholder viewholder, int i) {
viewholder.img.setImageResource(mImages.get(i));
viewholder.img.setOnClickListener(v -> {
// your code
});
}
Please try
itemView.setOnClickListener(new View.OnClickListener{
#Override
public void onClick(View view){
//your code
}
});
In my recyclerview has one imageview and textview. I'm changing image of ImageView onn onClickListener. Now the problem is if i click on image of position 3 and scroll down... image of position 8 is also changed and again if i scroll up...image of position 2 is changed.
public class PortraitListviewAdapter extends RecyclerView.Adapter<PortraitListviewAdapter.ViewHolder> {
Context context;
static List<PortraitParentListAdapterBean> list;
static List<String> selectedPosition ;
public PortraitListviewAdapter(Context context, List<PortraitParentListAdapterBean> list) {
this.context = context;
this.list = list;
selectedPosition = new ArrayList<>();
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.listview_parent_portrait, parent, false);
return new ViewHolder(itemView);
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
PortraitParentListAdapterBean portBean = list.get(position);
Log.i("pos",position+"");
holder.parentHeading.setText(portBean.getHeading());
if (selectedPosition.contains(list.get(position).getHeading())){
holder.parentImage.setImageResource(R.drawable.sad);
}
}
#Override
public int getItemCount() {
return list.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
protected TextView parentHeading;
protected ImageView parentImage;
public ViewHolder(View itemView) {
super(itemView);
parentHeading = (TextView)itemView.findViewById(R.id.parent_heading);
parentImage = (ImageView)itemView.findViewById(R.id.imageView);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
selectedPosition.add(list.get(getAdapterPosition()).getHeading());
parentImage.setImageResource(R.drawable.sad);
}
});
}
}
}
the above code is my implementation for recyclerview adapter. please help to understand the concept.
SOLUTION
change
if (selectedPosition.contains(list.get(position).getHeading())){
holder.parentImage.setImageResource(R.drawable.sad);
}
with
if (selectedPosition.contains(list.get(position).getHeading())){
holder.parentImage.setImageResource(R.drawable.sad);
} else {
holder.parentImage.setImageResource(R.drawable.your_default_drawable);
}
EXPLANATION
When you scroll your RecyclerView the system doesn't recreate always your ViewHolder but reuse one you previously scrolled that is no longer visible, so you need to reset your standard values in order to avoid showing wrong values.
Imagine a CardView with a button and text field. When I press on a button, text should change in the textfield. Code for my recyclerView is given below, where I am setting the text and a listener for a button:
#Override
public void onBindViewHolder(CategoriesDetailedViewHolder holder, int position) {
final CategoryDetailedModel model = categoriesDetailedModels.get(position);
holder.description.setText(model.getQuestion());
holder.btnPlus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
holder.description.setText("some text");
}
});
}
The main problem is with the string:
holder.description.setText("some text");
It cannot be accessed from an inner class.
Is anyone have any idea how to change text in card view from itself.
Update 1
public class CategoriesDetailedViewHolder extends RecyclerView.ViewHolder{
CardView cv;
TextView description;
TextView positiveCounter;
TextView negativeCounter;
TextView btnPlus;
View negativeCounterWrapper;
View positiveCounterWrapper;
CategoriesDetailedViewHolder(View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cv);
description = (TextView)itemView.findViewById(R.id.main_text);
positiveCounter = (TextView)itemView.findViewById(R.id.tv_positive_count_in_circle);
negativeCounter = (TextView)itemView.findViewById(R.id.tv_negative_count_in_circle);
btnPlus = (TextView)itemView.findViewById(R.id.plus_button);
positiveCounterWrapper = itemView.findViewById(R.id.positive_count_wrapper);
negativeCounterWrapper = itemView.findViewById(R.id.negative_count_wrapper);
cv.setPreventCornerOverlap(false);
}
}
#Override
public CategoriesDetailedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.categories_detailed_list_row_view, parent, false);
CategoriesDetailedViewHolder pvh = new CategoriesDetailedViewHolder(v);
context = parent.getContext();
return pvh;
}
Instead of,
TextView description;
Make it,
public TextView description;
EDIT:
Change the following,
#Override
public void onBindViewHolder(CategoriesDetailedViewHolder holder, int position) { ... }
to,
#Override
public void onBindViewHolder( final CategoriesDetailedViewHolder holder, int position) { .... }
I use this code and it works fine, you just need to use onBind method.
This is one example, I use this in my project and you can just change it to suit your needs:
public void onBindViewHolder(final TipsViewHolder TipsViewHolder, final int i) {
TipsViewHolder.cardText.setText("Old text");
TipsViewHolder.txtBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TipsViewHolder.cardText.setText("New Text");
}
});
}
I am working with RecyclerView and CardView. I want to attach OnClickListner to each card. I tried with many answers available on stackoverflow, but they are not working for me. So far I have tried -
public class SubjectAdapter extends RecyclerView.Adapter<SubjectAdapter.ViewHolder> implements View.OnClickListener,
View.OnLongClickListener{
private static final String LOGCAT = "SubjectAdapter";
private final Context mContext;
List<Subject> SubjectsList;
public SubjectAdapter(Context context) {
super();
this.mContext = context;
SQLiteDatabase.loadLibs(mContext);
DBHelper myDbHelper = new DBHelper(mContext);
SubjectsList = new ArrayList<Subject>();
SubjectsList = myDbHelper.getAllSubjects();
myDbHelper.close();
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.subject_cardview_row, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(v);
// Below two lines are NOT working
viewHolder.tvSubjectName.setOnClickListener(this);
//viewHolder.setOnClickListener(this);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
Subject subject = SubjectsList.get(i);
viewHolder.tvSubjectName.setText(subject.getSubject_Name());
viewHolder.tvCounts.setText(String.valueOf(subject.getSubject_Number_of_Questions()));
// Below two lines are NOT working
viewHolder.tvSubjectName.setOnClickListener(this);
//viewHolder.setOnClickListener(this);
}
#Override
public int getItemCount() {
return SubjectsList.size();
}
#Override
public void onClick(View v) {
// It's not working either
ViewHolder holder = (ViewHolder) v.getTag();
int position = holder.getPosition();
if (v.getId() == holder.tvSubjectName.getId()){
Log.d(LOGCAT, "tvSubjectName onClick at" + position);
//Toast.makeText(mContext, "tvSubjectName onClick at" + position, Toast.LENGTH_LONG).show();
} else {
Log.d(LOGCAT, "RecyclerView Item onClick at " + position);
//Toast.makeText(mContext, "RecyclerView Item onClick at " + position, Toast.LENGTH_LONG).show();
}
}
#Override
public boolean onLongClick(View v) {
return false;
}
class ViewHolder extends RecyclerView.ViewHolder {
public TextView tvSubjectName;
public TextView tvCounts;
public ViewHolder(View itemView) {
super(itemView);
tvSubjectName = (TextView) itemView.findViewById(R.id.tv_subject_name);
tvCounts = (TextView) itemView.findViewById(R.id.tv_text_counts);
}
}
}
As one can see, I have tried setOnClickListener with both onCreateViewHolder and onBindViewHolder, also as separate onClick, but none of them seems to be working for me. So, I want to know, How to add OnClickListner to CardView?
View returned by onClick does not necessarily correspond to View row hierarchy of the recycler view.
I think you should modify onBindViewHolder with
#Override
public void onBindViewHolder(final ViewHolder viewHolder, final int i) {
final Subject subject = SubjectsList.get(i);
viewHolder.tvSubjectName.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Log.d(LOGCAT, "tvSubjectName onClick at" + i);
// etc
}
});
...
}
If you use long click then replace View.OnClickListener with View.OnLongClickListener and onClick with onLongClick.
This worked for me !
Put the setOnClickListener-method inside the constructor of your ViewHolder class.
class ViewHolder extends RecyclerView.ViewHolder
{
public TextView tvSubjectName;
public TextView tvCounts;
public ViewHolder(View itemView)
{
super(itemView);
tvSubjectName = (TextView) itemView.findViewById(R.id.tv_subject_name);
tvCounts = (TextView) itemView.findViewById(R.id.tv_text_counts);
itemView.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//write here the code for wathever you want to do with a card
//...
}
});
}
}
Check out Lucas Rocha's new TwoWayView API.
TwoWayLayoutManager is a simple API on top of LayoutManager that does all the laborious work for you so that you can focus on how the child views are measured, placed, and detached from the RecyclerView.
Follow his site and implementation of the API is relatively simple and might help you through some of the difficult complexities RecyclerView and CardView pose.
http://lucasr.org/2014/07/31/the-new-twowayview/
Set OnClickListener to itemView in RecyclerView.ViewHolder constructor, also you can fetch position of cardView using getAdapterPosition() method which can help you to pass the data to new activity using putExtra method of intent. doc for getAdapterPosition method
`
class ViewHolder extends RecyclerView.ViewHolder
{
public TextView tvSubjectName;
public TextView tvCounts;
public ViewHolder(View itemView)
{
super(itemView);
tvSubjectName = (TextView) itemView.findViewById(R.id.tv_subject_name);
tvCounts = (TextView) itemView.findViewById(R.id.tv_text_counts);
itemView.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
int position = getAdapterPosition();
// use position to put extra data
}
});
}
`
If you really want to implement onclick listener then do the following
add a View object in your ViewHolderClass and initialize it to the itemView recieved in the constructor
2.setOnClickListener to the view object you declared(in ViewHolder) onBindViewHolder in the Adapter class
this will work for the entire cardView
Im trying show a toast msg when clicked on a item from my RecycleView, Ive tried many examples,
but its not giving me anything. Can somebody give me a different example that i can follow, at the end i wanna set the onClick to show a new fragment. If I can get an example on that, it will be great.
Im using this code:
public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.ViewHolder> {
private List<Movie> movies;
private int card_layout;
private Context mContext;
public MovieAdapter(List<Movie> movies, int card_layout, Context context) {
this.movies = movies;
this.card_layout = card_layout;
this.mContext = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
final View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(card_layout, viewGroup, false);
return new ViewHolder(itemView);
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
final Movie movie = movies.get(i);
viewHolder.movieImage.setImageDrawable(mContext.getDrawable(movie.getImageResourceId(mContext)));
viewHolder.movieName.setText(movie.mName);
viewHolder.currentMovie = movie;
}
#Override
public int getItemCount(){
return movies.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView movieName;
public ImageView movieImage;
public Movie currentMovie;
public ViewHolder( View itemView) {
super(itemView);
movieName = (TextView) itemView.findViewById(R.id.movieName);
movieImage = (ImageView)itemView.findViewById(R.id.movieImage);
itemView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View itemView){
Toast.makeText(itemView.getContext(),currentMovie.mName,Toast.LENGTH_SHORT ).show();
}
});
}
}
}
Do I have to implement something in my MainActivity as well?
and please dont get mad with me, Im just a starting with all this. All your help will be appriciated. thanks
Layout is not clickable by default. to make clickable add setClickable to true :
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(
card_layout, viewGroup, false);
itemView.setClickable(true);
itemView.setFocusableInTouchMode(true);
After wasting an hour of time, I found the most appropriate and simplest solution to this problem:
Try this, it will definitely work.No matters whether cards are used in grids or not.
RecyclerView Adapter:
ProductCardRecyclerViewAdapter.java
public class ProductCardRecyclerViewAdapter extends RecyclerView.Adapter<ProductCardViewHolder> {
public final String TAG=getClass().getSimpleName();
Context context;
private List<ProductEntry> productList;
private Integer[] cardImages;
String[] cardTitle;
String[] cardSubtitle;
public ProductCardRecyclerViewAdapter(Context context, Integer[] imageList, String[] cardTitle, String[] cardSubtitle) {
this.cardImages = imageList;
this.cardTitle = cardTitle;
this.cardSubtitle = cardSubtitle;
this.context = context;
}
#NonNull
#Override
public ProductCardViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_card, parent, false);
return new ProductCardViewHolder(layoutView);
}
#Override
public void onBindViewHolder(#NonNull ProductCardViewHolder holder, int position) {
// TODO: Put Recycler ViewHolder Cards binding code here in MDC-102
holder.imgCard.setImageResource(cardImages[position]);
holder.productTitle.setText(cardTitle[position]);
holder.productPrice.setText(cardSubtitle[position]);
holder.productCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: Material Card clicked "+cardTitle[position]+" : "+context.getClass());
//TODO: Perform card clicked working
Context c = v.getContext();
Toast.makeText(c, cardTitle[position], Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return cardImages.length;
}
}
RecyclerViewHolder
ProductCardViewHolder.java
public class ProductCardViewHolder extends RecyclerView.ViewHolder {
CardView productCard;
ImageView imgCard;
public TextView productTitle;
public TextView productPrice;
public ProductCardViewHolder(#NonNull View itemView) {
super(itemView);
imgCard = itemView.findViewById(R.id.product_image);
productTitle = itemView.findViewById(R.id.product_title);
productPrice = itemView.findViewById(R.id.product_price);
productCard=itemView.findViewById(R.id.cardofproducts);
// TODO: Find and store views from itemView
}
}
Hope, it will help a lot.