I have got a RecyclerView item looking like this
I want to achieve that when I click on item the ImageView will get overlay over it and TextView will become bold. I know how to use adapter and where to handle item clicks. I also know how to make overlay or bold text. I only want to know how to make this item selectable to get the behavior I described above. Because I found only tutorials to change background of item when clicked.
Based on this
I only want to know how to make this item selectable to get the behavior I described above.
So basically you need a way to tell the ViewHolder that the current item is selected, such that in onBindViewHolder() the items are rendered as per need.
I can think of this: Make a model of the item youre adding to the RecyclerView. Add a key as boolean isSelected = false in it.
And inside your onBindViewHolder where youre implementing the onClick()interface. do this:
... new OnClickListener({
... onClick(){
// take the item and set the isSelected flag
list.get(position).setIsSelected(true):
notifyDataSetChanged();
// alternatively you can also toggle this flag.
}
});
and while loading inside onBindViewHolder to this:
if (list.get(position).isSelected()) {
// highlight aka set overlay and bold text to view
} else {
// as per recyclerview doc, reset the views.
}
All you need is having a variable to hold the selected index. Then decorating the selected item in onBindViewHolder() method.
int selectedIndex = 0;
...
public void onBindViewHolder(ViewHolder viewHolder, int position) {
if (selectedIndex == position) {
// Do things you want
}
}
Related
I am working with recyclerview and i know how to change the color of selected item...
I am using SparseBooleanArray to change change the color of multiple items but don't know how to change the color of all items when user touch on selectAll button
remaining things working fine like get all the items in arraylist but don't know how to change the color of background at the same time....
Please anyone can suggest me...and comment for code if you want if unable to comment put it in answer which class you need for giving me suggestion
I found an answer for my question and i think it is the easiest way to do this:
Create an arraylist of view type...
Arraylist<View> view=new ArrayList();
class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener, View.OnClickListener {
private ImageView thumbnail_img;private TextView SongName;LinearLayout layout;
private ItemViewHolder(View itemView) {
super(itemView);
itemView.setOnLongClickListener(this);
itemView.setOnClickListener(this);
view.add(itemView);
thumbnail_img=itemView.findViewById(R.id.album_art);
SongName=itemView.findViewById(R.id.song_test);
}
}
After that when you want to use it
for(View v:view){
v.setBackground(context.getResources().
getDrawable(android.R.color.transparent));
}
You can use it also by position....
View v= view.get(index); //index is the int value for which you want to get the view.
Try and enjoy this simple code....
You just have to keep the track of the colors of each item in a separate array and then in your onBindViewHolder while populating each item in your RecyclerView, get the status of the background colors from that array.
You have a click listener for each item I assume. When you are about to change the background color, just keep an array of the items and update the value of the item accordingly when clicked. For example, you might consider having the following array.
int[] selectedItems = new int[yourArrayList.size()]; // Initially all items are initialized with 0
Then in your onBindViewHolder you need to do assign 1 when an item is selected.
public void onClick(int position) {
// Change the background here
// Keep track of the items selected in the array
if (selectedItems[position] == 0)
selectedItems[position] = 1; // The item is selected
else selectedItems[position] = 0; // The item is unselcted
}
if(selectedItems[position] == 1) itemView.setBackgroundColor(andriod.R.color.gray);
else itemView.setBackgroundColor(andriod.R.color.white);
Hope that helps you to understand.
I have a recyclerView that each item of it contain a question title and also a recyclerView inside it. I want to change the color of items inside the second recyclerView when clicking on a send button. I do this with these lines of code:
(questionListView is the RecyclerView and ansRecycle is second RecyclerView)
for (int i=0;i<adapter.getItemCount();i++){
questionListView.scrollToPosition(i);
questionListView.
adapter.notifyItemChanged(i);
View view=questionListView.findViewHolderForAdapterPosition(i).itemView;
RecyclerView ansRecycle=view.findViewById(R.id.checkedAnswerLayout);
for(int j=0;j<ansRecycle.getAdapter().getItemCount();j++) {
View temp=ansRecycle.getChildAt(j);
CheckBox ch=temp.findViewById(R.id.checkedChoice);
if(ch.getText().toString().equals(questions.get(i).getCorrectAnswers())){
ch.setTextColor(Color.RED);
}
}
}
For items that are shown on screen all things are fine but for others, there is no existence ViewHolder and I can not change the color of them.
How can I do this?
The problem is that items that are not on screen doesn't exist. The whole point of recyclerview is that it uses small amount of views, and recycle them when they leave the screen.
What you should do is do the same "if" when you bind the data.:
#Override
public void onBindViewHolder(MyDataViewHolder holder, int position) {
if(ch.getText().toString().equals(questions.get(i).getCorrectAnswers())){
ch.setTextColor(Color.RED);
}
}
Work around solution would be:
add a field to the Question - like int color = your color with setters/getters
on click get the questionItem and set the color of it to a new color
onBind of the recyclerView's find the textView and set the color based on your queston
same with answers.
for example
holder.textView.setColor(question.getColor());
I use FirestoreRecyclerAdapter in which i want change selected item color. because in firestoreAdapter it comes to onBindViewHolder() only item data change.for single item replace it ,but i want to change all item text color to which except selected item.
how can i do ?
thanks.
Reverse your logic then. Declare a list that keeps the clicked item (store the index, id or whatever you prefer), then in your onBindViewHolder(), do nothing if the clicked item is in that list, else change whatever you want.
Of course, you must update this list if you allow insertion/removal/change in your list.
EDIT
To answer your questions in your comments, first you must design the logic to change the color accordingly in onBindViewHolder(). Then, set onClickListener to the responding view. Lastly, to refresh your displaying list, call notifyDataSetChanged() or similar methods.
#Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
// declaration and setup here
final index = position;
// Define the logic to change the color if clicked item is/not in the list
if (yourClickedItemList.contain(position)) {
// Change what you want
}
else {
// Change what you want if the item is not in the list
}
// Then set the click listener
viewHolder.yourItemLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!yourClickedItemList.contain(index)) {
yourClickedItemList.add(index);
}
else {
// the list already contain that item
// do whatever you want here, like toggle off selection and remove from the list
}
// call to refresh your view
notifyDataSetChanged(); // or use notifyItemChanged() etc
}
}
}
I have a list view with multiple items, where i need to select and deselect the list items, and also delete the selected items.
So i have looked into the example in the below link but its for android:minSdkVersion="11"
but i am working on minSdkVersion="10".
Link : http://www.androidbegin.com/tutorial/android-delete-multiple-selected-items-listview-tutorial/
And yes we can do with checked text view, check box and radio button, but the requirement is like that i cannot use that.
Is there any other way that we can acheive this?
Make custom list adapter and get the click of that each view and maintain flag in adapter. If flag is true that means item selected otherwise item deselected, according to that you can change item view like disable that particular item or show some check box.
What I did was created an ArrayList that stores all the position of selected items, and toggle the background colors on clicks.
In my Adapter I define:
public ArrayList<Integer> selectedIds = new ArrayList<Integer>();
With the following method :
public void toggleSelected(Integer position)
{
if(selectedIds.contains(position))
{
selectedIds.remove(position);
}
else
{
selectedIds.add(position);
}
}
which addes\removes items from the ArrayList
In my getView method :
if (selectedIds.contains(position)) {
convertView.setSelected(true);
convertView.setPressed(true);
convertView.setBackgroundColor(Color.parseColor("#FF9912"));
}
else
{
convertView.setSelected(false);
convertView.setPressed(false);
convertView.setBackgroundColor(Color.parseColor("#000000"));
}
This checks if the position is storred in the ArrayList. if it does, paint it as selected. if not, the opposite.
all is left is the OnItemClick listener, i added :
((YourAdapter)list.getAdapter()).toggleSelected(new Integer(position));
When YourAdapter is the adapter of your ListView
Hope this helps anyone, as it's a generic answer :)
Thanks to eric.itzhak here : How to change background color of selected items in ListView?
I am implementing an endless ListView (like in the Twitter app). I want to make the last item not selecteble. So that if the penultimate item is selected and I scroll down with my trackball, nothing happens. I tried setting android:focusable="false" and android:cickable="false" but I didn't notice any chnage.
It's pretty easy, in your adapter you can override the method isEnabled(int position) and return false for this item.
if you're using custom array adapter just override this method.
#Override
public boolean isEnabled(int position) {
return false;
}
If you want to get the same effect without having to have a custom adapter, you make the OnClickListener ignore that item when tapped and then set a solid background color for the item's view, so it doesn't highlight when tapped.