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.
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?
I have a situation involving animation of listview's item appearances.
I have a few views in a ScollView , the last of which is the listview. I have attached an appearence animation of the row (a fade in animation).
The problem I have is that when the screen is loaded , the getView() of listview already executes for the initial items , even though the listview is not currently in view.
Hence when a user scroll downs , he sees the list plainly.
I am unsure how to go about this situation . Is there any callback that can be invoked when a row from a listview becomes visible on screen ? .
Yes there is a callback (OnScrollChangeListener), first visible index and last visible index etc. You can achieve what you are trying to using combination of these.
But you need to try that yourself first. No one can simply write a code for you.
You can learn about listview here
I realise that this question has been asked a lot and i've read the first 2 pages of google about it but i couldn't find an answer that worked.
Basically i want a ListView with custom items, each one should have a checkbox. How can i make it so that when i click a checkbox the equivalent of listView.setItemChecked(int, boolean)... would happen?
Some of the answers i've found required having another boolean in the list objects, or some would reuse the checkboxes so they reappear checked further down the list. Is there a way to have a checkbox modify something in the listView or how could i add multiple listeners to a list item? For example when i clicked the text something would happen but clicking the image would trigger something else?
Thanks.
You can listen for multiple items at a list row easily. just listen for them inside your list adapter getView method after finding them.
hope it helps
I am working on a simple to-do list app, and on the main view I have a list of tasks with a checkmark aside to each, as the image below:
Upon clicking on the checkmark I want the respective task to be removed from the list. I tried to implement this as TextViews (for the tasks) and Buttons (for the checkmark), but I would need to know the number/position of the clicked checkmark (0,1,2 or 3) to remove the correct task from my array. Can I get this some how?
I also thought about implementing the tasks/checkmarks as a ListView, but then I would need to set a onItemClickListener only on the checkmark, and not on the task text. Is this possible?
Any other ideas? Thanks.
Checkbox has a tag property which you can use to set custom data like row number. Something like this should work,
checkbox.setTag(row_number);
So when you click on it, do something like
int rowNum = Integer.parseInt(checkbox.getTag());
removeTaskAt(rowNum);
If you're using a Custom Adapter, you can add a click listener on the check on getView, so the click is only on the checkbox and not on the full item.
The only problem with this is that the scroll may not work if you try to scroll by clicking on the checkbox.
I have created a custom ListView.. every element of my ListView constructing from a layout.
My layout will have some TextViews and a ToggleButton. My ListView will have some 50 elemnts and my device screen can show 2 elements at a time.
Now if we select ToggleButton on a Item and scroll down my ListView , the selected item will be out of screen and when I come back to the previous selected item I am observinf that selection for that toggle button is gone.
I know that this is a normal behavior in android that It will construct the elements which is currently displaying.
But I want to keep that value.. Is there any way that we preserve selection. or can I tell ListView to donot construct every time.. memory is a not a issue for my application.
Please let me know if there's any good solution for this.
You could store the states of the checkbox in a map with the position as the key. So whenever the list reloads after scroll, it loads based on the hashmap state. You should write this logic in the getView()