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);
Related
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?
How can I stop RecyclerView from scrolling to the bottom of the list whenever I add items?
Even when I insert something at the top of the list and call notifyDataSetChanged or notifyItemInserted the list scrolls to the bottom.
The one feature that works as expected is notifyItemRemoved(index)
I think I was calling notifyRangeInserted() with the entire list and that was scrolling to the end of that range.
If you're having similar issues, just cut the Adapter functionality back to its bare essentials and build up from there.
The problem was I had an item in the list, a ProgressBar to indicate that data was loading, but when the new items loaded I added them to the list, this moved the "focused item," the ProgressBar down and if there were enough items to move it off the screen then the RecyclerView would scroll down to keep it on screen. The problem being 1. I didn't want it to maintain "focus" and 2. The very next thing I did was remove the ProgressBar from the `Adapter.
So the solution is to remove the ProgressBar or other items before adding items earlier positions in the Adapter.
I need all of the other views in my arrayadapter to check their attributes and return to a default attribute at certain times
use case: listview items (a)(b)(c)(d) , when you touch (a) it's background turns black. This is done in the ontouchListener. But when you touch view (b) it's background turns black BUT (a)'s needs to turn back to the default which is not black
I dabbled around with recalling the arrayadapter at the end of the view.OnClickListener but the problem with this is that it also resets the scroll position of the view, so if this list was much longer - which it is - and user touched item (r) , then it would reset the list and put the user back at item (a) at the top
I was looking at notifydatasetchangedbut I am not sure how to use that and where it should work
insight appreciated
notifyDataSetChanged will do the job. You can call that at any time in your code and this will trigger the adapter to call getView for the visible list items again and so update their content. So basically the only thing you have to do is to update the list item states or information before calling that method.
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.
I am trying to update color of listview from async task postexecute method.
I am doing ((View) lstChoices.getAdapter().getView(0,null, lstChoices)).setBackgroundColor(Color.RED);
But this is not doing anything, but I tried the same in getView method of my custom adapter then it worked, row.setBackgroundColor(Color.RED);
Any ideas what I am doing wrong?
Thanks
You should not change this that way. All what is related to the row should be handled in adapter. It means that if you changed anything that could influence background color, then all you should do is notifyDatsetChanged() which would trigger list redraw. And b/g color should be then changed by adapter.
A very clearly presented approach to Listview Item Background color changing based on the item's State is at Programmatically select item ListView in Android
Since you are looking for 'postexecute' perhaps changing the Item's State and using that approach can help you get what you are after.