Reputation: 56
I currently use RecyclerView
but i can't fix the issue;
If user selected a answer, answer in change textview
color and background.
If user selected different answer first answer old textview
color and background.
Codes;
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
_mContext = holder._mAnswersContainer.getContext();
_mPosition = position;
holder._mImageAnswer.setImageDrawable(Utils.stringToResource(_mContext,
_mAnswerList.get(_mPosition).mAnswerImage));
holder._mImageTextAnswer.setText(_mAnswerList.get(_mPosition).mAnswerText);
holder._mAnswersContainer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder._mImageTextAnswer.setTextColor(_mContext.getResources()
.getColor(R.color.white));
holder._mImageTextAnswer.setBackgroundColor(_mContext.getResources()
.getColor(R.color.red));
Log.d(TAG, "Values : " + QuestionsHelper.getInstance(_mContext)
.getValues(_mAnswerList.get(_mPosition).mAnswerText));
}
});
}
Upvotes: 0
Views: 39
Reputation: 1924
Inside your adapter, make a member variable to keep track of which position is selected:
private int mSelected = -1;
Inside your onBindViewHolder
(although it might work inside onCreateViewHolder
as well):
int color;
if(position == mSelected){
color = ContextCompat.getColor(context, R.color.selectedColor);
}else{
color = ContextCompat.getColor(context, R.color.regularColor);
}
// Set the color
viewHolder.yourView.setBackgroundColor(color);
Create some helper functions for your RecyclerView adapter to handle the selection:
public void selectPosition(int selected){
mSelected = selected;
notifyDataSetChanged();
}
public void resetSelected(){
mSelected = -1;
notifyDataSetChanged();
}
Wherever you want to set the selected item just call adapter.selectPosition()
. And clear the selection with adapter.resetSelected()
Upvotes: 1