Recyclerview manipulate items that are not selected - android

i am trying to add some animations to my Recyclerview. So far everything works great, but i have reached on problem i don't have an answer to.
I have a List where the user should be able to select only one Item. When an item is selected an animation starts and a checked box appears. Now if another item is selected it should also play this animation (This Part works) but it should also remove the checkbox from previous selected one with an animation (This part does not work).
My Problem is that i do not have a clue how to get the adapterPosition of the previous selected item.
I am thankful for any help

So i solved it, maybe someday someone will stumble upon this question, so here is the answer.
The Recyclerview adapter method getAdapterPosition() returns the sam value as the index in the ArrayList given to the Adapter.
All you have to do is fetch the index of the item you want to manipulate and then you can work with that.
If some one needs more details, then post a comment.

Related

Android RecyclerView best practice for displaying items

I have a reyclerview in which every time I add an item I add it at index 0, Usually when you add an item a given index you can do it like so
mAdapter.add(0, item)
mAdapter.notifyItemChanged(0)
the problem is the item will be added to index 0 but the user will have to scroll to view it, of course, there is a solution which is by simply doing the following
messageAdapter.add(0, item)
messageAdapter.notifyDataSetChanged()
the problem here is that by calling "notifyDataSetChanged()" the item will appear all of sudden inside the list which I don't like I want to have a nice and smooth animation for when the item is added, another solution is to manually smooth scroll the recyclerView whenever an item is added using our first code, this solution is the best but it has some problems.
when the user is viewing item at index 999 and a new item is added the view will be scrolled to index 0 which can be kind of annoying for the user, the solution is simple for this problem, we can check if the item at index 0 is visible if it is, then scroll to index 0, this is the perfect solution for my problem.
So my question is, is there any other way to implement the functionality that I want to achieve without having to do that work? I would like to know what the simplest way is, thank you in advance!
Try using diff util it will be better for performance for updating recyclerview
https://blog.mindorks.com/the-powerful-tool-diff-util-in-recyclerview-android-tutorial

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?

ListView updating Items without being recreated

I want my ListView to work something like the following:
When I press a button (probably from context-menu), I want the user to be able to select more then one item from ListView (probably using check-boxes), but those check-boxes should not be visible before that.
So, the point is, after the user presses a button (let's say "Delete more items"), the listview, should update itself, and appear on every row of the list, a checkbox should appear (allowing me to select the items ID to pass those to server).
How can I achieve that, without having to recreate the list from zero? (how to setVisibility ON, keeping the other content of the ListView as it is, and not doing another request to server).
PS. If you guys, have another better idea, on achieving the Delete More Items, would be much appreciated!
This is just an idea, haven't tried it myself: you build in a checkbox in your listitem layout. Normally, in the getView of your adapter, you set it invisible with
checkBox.setVisibility(8);
When you want to show them, you set some boolean
showBoxes
of your adapter to true, then in the getView oyu don't hide the checkboxes.
Then
notifyDataSetChanged
on the adapter.
Hope it's clear what I mean.

how does scrolling in android listview work?

I have an android-app with a listview in an activity. The listview has, if I call it so, three data states.
no data loaded from inet -> only one dummy item is visible, saying that data is loading;
data is loaded and shown in list;
one listitem is clicked and now shows more information for this listitem (so it is increased in its height).
On every state change (1 -> 2, 2 -> 3), I call notifyDataSetChanged() on this ListAdapater.
This causes the listview to scroll down to the last item. This is ugly in the first transition and even more ugly in the second because the clicked list item is now out of focus.
As I can see, this happens with a google g1 with android 1.6. An htc touch with the same sdk acts like desired (I will try to figure it out with some more devices).
To avoid this, I tried to read out getScrollY() and set this value back. but this returns 0. The reason for this return value I already found on stackoverflow in other questions.
Has anyone else seen this problem? Why does the listview scroll to the last item? It was mentioned that listview keeps track of the scroll position. but it seems that it does not in my case.
Or maybe I am calling the wrong refresh method? Is notifyDataSetChanged the correct one?
I believe you want:
listview.setTranscriptMode(ListView.TRANSCRIPT_MODE_DISABLED);

How can I "set View" in right place where I remove an item in ListView (Android)

I choose an item to remove from my ListView. And after the Item was removed, my ListView was scrolled back and display at the first Item.
I want my ListView display in right place where the Item I had removed (It like remove a contact in Android Contact list). How can I do that?
I suppose you want to update your ListView display, after you remove an item, right?
When you remove the item, you have to modify your data adapter, and let the ListView change accordingly (something like the MVC pattern behavior). I've seen somewhat similar questions (and the corresponding answers :) ) here and here.
Edit:
Aha, in that case, try using setSelection(int), after recalculating new indices and new item count.

Categories

Resources