Android list with 'grayed out' items - android

In Android, I want to present the user with a list. When an item on the list is selected some action is performed, and this list item is no longer selectable. It is also 'grayed out' or the like to indicate that it cannot be selected the next time the list is displayed. I have seen the isSelectable() override in Adapter, but I believe this causes the item to be treated as a separator, which causes visual problems. And I have not found a way to 'gray out' an item. Any ideas? Thanks...

As far as graying out an item. I'm not sure if this is the best way, but it's what I do:
view.setAlpha(75);
view.setBackgroundColor(Color.GRAY);
I'm basically making the item transparent and then setting the background color to gray. If you're reusing your list items you should also change them back to their original state if the condition is not met, i.e.:
view.setAlpha(255);
view.setBackgroundColor(Color.WHITE);
that is, if your original state was no transparency and the background color was white.

You need the view to be disabled. If you are creating the views just call .setDisabled(boolean) on the top view. Setting the list item to be disabled doesn't work very well in my experience.

Here is the solution I am using. I set up an OnItemClickListener for my ListView. When an item in the list is clicked, I take the passed in View and call setEnabled(false) on it. This will gray out the item. However, subsequent clicks on this item will still call the onItemClick method. So, you will need to check on each click if the item is enabled/disabled and act accordingly.

Related

How to get data from specific rows only in RecyclerView?

I'm stuck on a little problem.
When I click on a row in my RecyclerView, I'd like to change its color to some other color (i.e.: red).
Then, if I click this row again, I would like to set its color back to the normal one (i.e.: white).
Then, if I click on a second row or n-th row, I would like to do the same.
I've tried to work on the RecyclerView Adapter class, in my ViewHolder I tried some ideas counting user clicks, setting a boolean to check the colored rows, and getAdapterPosition to know what is the row's position... but actually all my tries failed!
Would you please help me with that problem?
It seems that I'm close to the solution, but need a little help
You're going to need to keep track of which items were selected on the list (otherwise any changes to the view will simply be recycled).
Keep a list of selected items within your adapter. In the adapter's onBindViewHolder you can check against the list and if the list contains the view/item, you can color the view accordingly.
Edit: Refer here for a working example
How to properly highlight selected item on RecyclerView?

Updating RecyclerView item ProgressBar without calling notifyItemChanged(int) on adapter

I am trying to update a progressBar which is the part of one of the RecyclerView items. I know I can do it in two ways,
Method 1: By refreshing the item row by calling notifyItemChanged(position)
Method 2: By accessing the view using the method findViewByPosition() on LayoutMananger object.
But unluckily none of them are working in my case.
If I use the first one, the whole item will get repainted, the background color of the recyclerview is gray and the item is white, so the user can notice the whole item refresh.
If I try the second one, I will end up with problems while scrolling.
I want to update progress just like WhatsApp does. Thanks in advance
If I use the first one, the whole item will get repainted, the background color of the recyclerview is gray and the item is white, so the user can notice the whole item refresh.
I ran into this issue as well. To fix it you need to turn off the default item change animations. Once you turn off the item change animations your recyclerView item will update without any flashing/jumping, allowing you to use notifyItemChanged(int) without any problems.
Turn off the default item change animations like so:
((SimpleItemAnimator) myRecyclerView.getItemAnimator()).setSupportsChangeAnimations(false);

Highlighting selected view in Listview Android

I have a listview and a corresponding listview adapter.
The views displayed in the Listview have I written myself and it's a framelayout with one button and one imagebutton. The imagebutton is a red cross that deletes the entry and the regular button selects the entry.
When the user presses the regular button, that item is selected and I want to show this to the user by setting the background of that item to green. The application stores which item the user selected and the next time the listview is rendered, THAT item should be selected and green.
Notice that there should be one and EXACTLY one selected item in my listview at all times.
In the getView(.. method in my adaper, it's very easy to change color of the button when the user clicks it. But the button that was green before the user clicked is impossible for me to reference.
I tried storing a reference the previously selected button, but it never repainted
I tried removing and adding the data item from the list to trigger notifyDataSetChanged but it never repainted
I tried setChoiceMode(ListView.CHOICE_MODE_SINGLE), but that led nowhere because I don't know how to catch the choice in my getView method and paint differently depending on wheter it's selected or not.
All guides I see suggest using notifyDataSetChanged. However, the underlying data is NOT changed in this case and it is not correct (or possible) solution.
So I think that my problem boils down to: HOW can I reference another view in my listview??
(And Yes, I have seen this post: Highlight selected item in ListView on Android . It describes my problem and is answered with "and then change the color of previous selected item's background back to normal" but I still can't refer to the PREVIOUS selected item.
Thanks!
Why don't you use Radio Buttons ? This is exactly the design you need. You can add a "red cross" on each line to delete the line. The selected line is unique and you can iterate on RadioButton.isChecked(); of your RadioGroup to set the background color accordingly.

Customize checked items in ListView

I have a list view with black text items with single choice mode.
I would like the checked item to be red.
Is there any straightforward way to do this, or do I need to listen for item click callbacks, change the colors manually, etc.?
Thanks
There's nothing that will "automatically" change the background of an entire listview item based on a checkbox, so you'll pretty much have to handle the event.

How to mark an element visually in ListView the user clicked on?

I have a ListView with a custom adapter. I'd like to visually mark the list element the user clicked on by changing it's background permanently (until the user clicks on another element that is). How do I achieve that? I believe that there's a built-in feature inside LisView for this, but I had no luck finding it yet.
By default ListView animates the background color of the list-element clicked, I just want that modified color to "stay". I've already set the ListView's ChoiceMode to single choice, but it doesn't affect it visually.
Have you tried ListView.setSelector?

Categories

Resources