Solution 1 :
Put your click Listner in viewholder
@Override
public void onBindViewHolder(ViewHolder holder, int position)
{
String item = itemList.get(position);
holder.textview.setText(item);
holder.textview.setTag(item);
GradientDrawable bgShape = (GradientDrawable) holder.button.getBackground();
if (ColorMap().containsKey(itemList.get(position)))
bgShape.setColor(Color.parseColor(ColorMap().get(itemList.get(position))));
holder.button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) //I've been debugging, this part does not work and just through.
{
int pos = getAdapterPosition();
if (pos != RecyclerView.NO_POSITION)
{
Intent intent = new Intent();
intent.putExtra("name", ColorMap().get(itemList.get((pos))));
colorSelectionActivity.setResult(Activity.RESULT_OK, intent);
notifyItemChanged(pos);
colorSelectionActivity.finish();
}
}
});
}
Previously you asked recycler view item to respond on your click listner. But now you asked
holder.button.setOnClickListener
So on click of button only your color will change.
Solution 2 :
You should change this:
itemView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
...
}
});
to this:
itemView.button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
...
}
});
Problem :
What I wanted was…
When click the button in Main Activity, Color Selection Activity will run, and click the button in the recycler view there, the button in Main Activity will change to the color of the button clicked in the recycler view.
But the recyler view works well, but the OnCilck inside the View Holder doesn’t work.
I searched for a really long time. But I don’t know the reason. I would be very grateful if you could help me.
Color Selection Activity
adapter = new Adapter(this, itemList, ColorSelectionActivity.this);
listview.setAdapter(adapter);
Main Activity
public void onColorButtonClick(View v)
{
Intent intent = new Intent(getApplicationContext(), ColorSelectionActivity.class);
startActivityForResult(intent, GET_COLOR_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent intent, View v)
{
super.onActivityResult(requestCode, resultCode, intent);
if(requestCode == GET_COLOR_REQUEST)
{
if(resultCode == RESULT_OK)
{
GradientDrawable bgShape = (GradientDrawable) v.getBackground();
bgShape.setColor(Color.parseColor(intent.getStringExtra("name")));
}
}
}
Adapter
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder>
{
private ArrayList<String> itemList;
private Context context;
private Activity colorSelectionActivity;
public Adapter(Context context, ArrayList<String> itemList, ColorSelectionActivity colorSelectionActivity)
{
this.context = context;
this.itemList = itemList;
this.colorSelectionActivity = colorSelectionActivity;
}
private Map<String, String> ColorMap()
{
Map<String, String> colorMap = new HashMap<>();
colorMap.put("Anger", "#ff4343");
colorMap.put("Confusion", "#ff8e43");
colorMap.put("Exciting", "#ffff4f");
colorMap.put("Normal", "#85e070");
colorMap.put("Joy", "#ffa8ab");
colorMap.put("Sad", "#80a9e0");
colorMap.put("Tired", "#6662bf");
colorMap.put("Melancholy", "#bd72db");
return colorMap;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View view = LayoutInflater.from(context).inflate(R.layout.color_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position)
{
String item = itemList.get(position);
holder.textview.setText(item);
holder.textview.setTag(item);
GradientDrawable bgShape = (GradientDrawable) holder.button.getBackground();
if (ColorMap().containsKey(itemList.get(position)))
bgShape.setColor(Color.parseColor(ColorMap().get(itemList.get(position))));
}
@Override
public int getItemCount()
{
return itemList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder
{
public TextView textview;
public Button button;
public ViewHolder(View itemView)
{
super(itemView);
itemView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) //I've been debugging, this part does not work and just through.
{
int pos = getAdapterPosition();
if (pos != RecyclerView.NO_POSITION)
{
Intent intent = new Intent();
intent.putExtra("name", ColorMap().get(itemList.get((pos))));
colorSelectionActivity.setResult(Activity.RESULT_OK, intent);
notifyItemChanged(pos);
colorSelectionActivity.finish();
}
}
});
textview = itemView.findViewById(R.id.item_text);
button = itemView.findViewById(R.id.item_button);
}
}