I followed this answer:
https://stackoverflow.com/a/30046476/8793443
and got it to work: only one item is selected at a time (which is what I want).
However, when the same item is clicked for a second time, it remains selected. How can I deselect it so that it goes back to it's original color background?
Any help is appreciated.
Thanks!
The OnClick event must be this instead:
#Override
public void onClick(View view) {
if (selectedPos == getAdapterPosition()) {
selectedPos = RecyclerView.NO_POSITION;
notifyDataSetChanged();
return;
}
selectedPos = getAdapterPosition();
notifyDataSetChanged();
}
It works like a charm! Thanks to Quick learner's answer for inspiration.
Related
In my app I have a bottom navigation including home and favorites. in the recyclerview when I click on like button it'll be added to favourites. now when clicked again, I want to remove it from favorite at the moment.
In my code the item is deleted but it won't be removed until I go to home and return.
Note that I can't use arraylist.remove(position) because if so, even in home fragment the item is removed and I don't want that.
code in adapter:
itemHolder.like.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int rowId = databaseHelper.GetId((String) itemHolder.headline.getText().toString());
if (databaseHelper.IsLiked(rowId)){
databaseHelper.UpdateLiked(rowId, false);
notifyItemRemoved(position);
notifyItemChanged(position);
}
else{
databaseHelper.UpdateLiked(rowId, true);
notifyDataSetChanged();
notifyItemChanged(position);
}
}
yourarraylist.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, yourarraylist.size());
I'm having a problem when clicking on a CardView. I have a RecyclerView with CardViews where I want to do a single select. First click does it correctly, but when I click on another one the text color of the previous CardView changes to white.
Here is the code for my Adapter:
holder.setiRecyclerItemSelectedListener(new IRecyclerItemSelectedListener() {
#Override
public void onItemSelectedListener(View view, int pos) {
// Loop all cards in card list
for (CardView cardView: cardViewList) {
if (cardView.getTag() == null) // Only available card time slots
{
cardView.setCardBackgroundColor(context.getColor(R.color.colorWhite));
holder.txt_time_slot.setTextColor(context.getColor(android.R.color.tab_indicator_text));
holder.txt_time_slot_description.setTextColor(context.getColor(android.R.color.tab_indicator_text));
}
}
// Color of selected card time slot
holder.card_time_slot.setCardBackgroundColor(context.getColor(R.color.colorPrimaryLight));
holder.txt_time_slot.setTextColor(context.getColor(R.color.colorWhite));
holder.txt_time_slot_description.setTextColor(context.getColor(R.color.colorWhite));
// Send broadcast to enable button NEXT
Intent intent = new Intent(Common.KEY_ENABLE_BUTTON_NEXT);
intent.putExtra(Common.KEY_TIME_SLOT, position); // Put index of time slot we have selected
intent.putExtra(Common.KEY_STEP, 3);
localBroadcastManager.sendBroadcast(intent);
}
});
Pictures:
First one does it correctly
Second one doesn't
Define a selected index in adapter:
int selectedIndex = -1;
You can access child of recyclerView with this method:
findViewHolderForAdapterPosition(position);
change onClicklistener:
if (selectedIndex != -1)
{
YourHolder holderOld = (YourHolder) recycleView.findViewHolderForAdapterPosition(selectedIndex);
holderOld.cardView.setCardBackgroundColor(context.getColor(R.color.colorWhite));
holderOld.txt_time_slot.setTextColor(context.getColor(android.R.color.tab_indicator_text));
holderOld.txt_time_slot_description.setTextColor(context.getColor(android.R.color.tab_indicator_text));
}
selectedIndex = pos;
holder.card_time_slot.setCardBackgroundColor(context.getColor(R.color.colorPrimaryLight));
holder.txt_time_slot.setTextColor(context.getColor(R.color.colorWhite));
holder.txt_time_slot_description.setTextColor(context.getColor(R.color.colorWhite));
I guess you should invalidate your views in RecyclerView. So according to your question I think that you should call notifyDataSetChanged() function or you should invalidate specific item in RV by calling notifyItemChanged(position). Hope it'll help
I thought you implied colour of selected card time functions out of the } the machine using it and end the previous function without changing it that might be help
Good morning,
I have a ListView in a first activity (main_activity), when I click on a selected item, I have a new activity (details_activity) that opens with more informations and details, and the user had to check it, then the (details_activity) is closed and back to the main_activity with the same list.
I want to disable the click on listView's rows that are already checked by the user.
I tried to get the position from main_activity to details_activity to use it in SQL to get the informations, it's working.
Then I sent it back, in the main_activity I used this code:
mPrr = getSharedPreferences("reponse",Context.MODE_PRIVATE);
int IDs = mPrr.getInt("ID",-1);
String rep =mPrr.getString("reponse","");
if(IDs != -1) {
lst.getChildAt(IDs).setFocusable(false);
}
Is there any help please?
Create a ArrayList in adapter than whenever perform click on that row check that row position if position not in list than add position to the array list and perform click else nothing to do.
private ArrayList<String> positionClicked = new ArrayList<>();
public void onBindViewHolder(final MyViewHolder holder, final int position) {
if (!positionClicked.contains(String.valueOf(position))) {
positionClicked.add(String.valueOf(position));
//perform Click
}else {
//
}
}
Write setClickable(false) instead of setFocusable(false);
Create custom ArrayAdapter, find the position of item you want to disable click and #Overide isEnabled method
#Override
public boolean isEnabled(int position) {
if(position == positionYouWannaDisableClick)
return false;
return true;
}
I've got a card-view that each card has a button on it. I'm trying to get the position of the card that holds the button when the users clicks the button.
I have an adapter that extends from RecyclerView.Adapter and implements the following method...
public void onBindViewHolder(MovieImageViewHolder movieImageViewHolder, int i) {
Movie movieItem = mMovieList.get(i);
movieImageViewHolder.details.setTag(i);
movieImageViewHolder.details.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
position = (Integer) v.getTag();
}
});
}
This works sucessfully, when I press a button on card 1, position equals 1 and when I press a button on card 4, position equals 4.
The question is, how do I get this value (position) out of the adapter and back into the Activity so I can use it? or do I even need to do that? I'm trying to create a new intent but don't think that should be done from the adapter?
I attempted to create a getter for the variable position, and call the primary button action with
public void buttonClick(View view) {
int position = adapter.getPosition();
Log.d(TAG, "Button Clicked::- " + String.valueOf(position));
}
Using
android:onClick="buttonClick"
In the XML, but nothing happens when clicking the button, I believe this is because the OnClick method in the adapter is fired instead?
EDIT:
Seems I've made some progress. Worked out that I can set the tag in the adapter
movieImageViewHolder.btn_details.setTag(i);
Then get that tag back in my onClick method with .getTag();
Only thing now is the tag seems to be zero for the first element, then zero for the second element, then starts at one for the third... Any reason why this would be?
This was actually very simple and a very common "issue" people face. I was set down the path that I couldn't create an activity from an adapter where infact that is exactly what I should have been trying to do.
movieImageViewHolder.details.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
Integer taggedPosition = (Integer) v.getTag();
Intent intent = new Intent(mContext, ViewMovieDetailsActivity.class);
intent.putExtra(SearchMovie.MOVIE_TRANSFER, getMovie(taggedPosition));
v.getContext().startActivity(intent);
}
});
Correctly starts the correct activity.
I'm developing one android application in which I'm using one customized ListView which extends BaseAdapter, In which I have placed two TextView's and one button to delete the corresponding row from the list and also from the original data,
Whenever I call the notifyDataSetChanged() function the value of the position get reversed, am I doing anything wrong ?, Is there any other correct way to do this without the position get reversed ?
convertView.setOnClickListener(new OnClickListener() {
private int pos = position;
public void onClick(View v) {
Toast.makeText(context, "Click-" + String.valueOf(pos), Toast.LENGTH_SHORT).show();
}
});
holder.delete.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
adapter.notifyDataSetChanged();
}
});
Any help will be appreciated.
Instead of on click for button in the every row in the list I just used context menu and performed the actions needed and updated the list and now its working.
Actually this is not the solution for my question and its just an alternative.