I'm using Recyclerview to show a list. I want to delete some items like IOS. In my listview template I have added a button to delete item which is invisible by default. In my activity I have another button attached at bottom (Not part of listview) and on tap of this button I want to make all delete buttons of listview visible.
My Question is how can I get reference to all delete buttons of listview in activity and is it the right way to do this?
Thanks
Assuming you have ViewHolders set up, you already have references to all the buttons in your list. All you have to do is to make them visible for every item in the list with a simple loop.
In case you haven't implemented ViewHolders I suggest you check out the documentation and take a look at some simple tutorials on how to use them.
On a side note. If I understood correctly you're making a bottom tab for your app and since you referenced iOS I gotta say this; Remember that Android and iOS are two unique operating systems with their own ways of handling things. Check out Googles pure Android documentation.
In your question title you say RecyclerView, but in your text you say ListView. The solution is similar either way, but it's best to be perfectly clear what you're doing.
In either case, there are at least two different solutions.
First, you could use a boolean flag to determine if all the the item buttons should be showing or not. You check this flag at the time the item view is inflated or created and toggle the button accordingly. If the boolean flag is ever changed, the easiest thing to do is tell the RecyclerView/ListView that the underlying data has changed and to redraw all the views. Call notifyDatasetChanged on the adapter.
The other thing you can do at the time the item buttons should change is iterate all the visible item views, find the button, and change its visibility. With RecyclerView, you can do this, and with ListView you can do this.
Related
I am developing an activity with a ListView in which I need to change the current row by another layout by clicking on the row, and I'm not finding any way to do as much as I look (I take hours searching for possible solutions and I have not seen any reference to this problem). I do not know if this can be done in Android, but if anyone has an idea of how to do this would be appreciated.
Thanks in advance.
PS: The ListView control is normal and just want to replace a layout with a different layout. I'm using the API 15.
Use a ViewSwitcher
http://developer.android.com/reference/android/widget/ViewSwitcher.html
A ViewSwitcher is -
ViewAnimator that switches between two views, and has a factory from
which these views are created. You can either use the factory to
create the views, or add them yourself. A ViewSwitcher can only have
two child views, of which only one is shown at a time.
I suggest merging the two layouts in a single one and hide the second one. In your adapter data you should have a flag or something to indicate which layout to display. When you click a row, toggle that flag for the selected item and notifyDataSetChanged() on the adapter. This will make sure the changed layout remains even if you scroll up and down and the row goes off screen.
A more optimized solution is to have different item types in the adapter.
If you are creating a very dynamic list, say, where every row can have a different set of input types plus optional buttons, and the list length is based on another dynamic value, is it better to do this in a list adapter or creating a custom view in a scroll window?
After struggling with list adapters for quite a while now something finally occurred to me- this seems dumb. It seems like I am going through a lot of work keeping track of what spinner is set to what value, which row was clicked and so forth.
For example, say you are showing something like a contacts screen with various details that can be entered about a contact. Some rows will have text inputs (name, address etc), some will have spinners (ie. state, group), some will have checkboxes (like 'favorite' or something). Also, there is an 'add' button that allows you to add another field to edit. Is it worth making this in a list adapter or is it better to populate a custom view, and if the "add" button is clicked, we re-create the custom view, adding a view of the type they want to add?
I hope this is clear.
ListViews (and List Adapters) are meant for data that is to be displayed in mainly similar views. For your example, it is much easier and more natural to have a predefined layout file with the screen and use view visibility so select which views are to be shown. If you need to add views to the screen you can do this dynamically by using findViewById on the layout and then using it's addView method.
Let me know if you need more clarification or sample code...
(FYI, I'm targeting 2.2 Froyo in my project, so any solution would have to work at that build level.)
What I want to do seems quite simple; show a list of items in a ListView, and allow the user to tap to select multiple items before performing an operation on all of them at once.
To provide a little more detail, I am binding a ListView to an array of objects. The screen consists of other controls, with the ListView in the middle. Each list item has several components; two images, and a text label. NO checkbox. Instead, when the user taps an item, the background should change to indicate that it is checked. If the item is tapped again, the background should change back to indicate it is not checked. The user may tap one or more items. If the user scrolls the list off the screen and scrolls it back, the state of those items should be preserved.
From what I've researched, I gather that I need to:
use a Drawable as the background for the list items with selectors for checked, pressed and default states
create a custom class extending LinearLayout or RelativeLayout and implementing Checkable, then use this as the root View for the list item layout
I've found several tutorials online, but none work. Either they have runtime errors, or simple don't do anything... pressing the buttons does not change their appearance to checked.
I found an alternative approach in the O'Reilly "Android Cookbook." Rather than doing all of the above, they add a boolean to the objects the ListView is bound to, then manually add code to change that boolean when a list item is clicked and to change the background in the adapter for an item where the boolean is true. In other words, they don't use Checkable at all. This does not seem like an ideal solution to me; abandoning the Android API in favor of custom hacks often seems to cause bugs later on, and I'm uncomfortable with adding GUI information (whether or not an item is selected) to what should be a purely data-carrying POJO conceptually representing a chunk of information.
My question is this: does anyone have a WORKING tutorial to accomplish what I have described, using Android's Checkable functionality? Or is this so problematic that something like the O'Reilly hack always has to be used?
I have never read Android Cookbook but their strategy is exactly what I would do. I would decline to call this a hack and suggest this is the kind of thing that the Android framework intends to do. A selected state in my opinion is part of the model you wish to protect, I do see how it could be in a gray area as it removes the purity of your POJO's.
There is a second strategy you could use to protect the purity of your POJO's use the state of the background to find if something is selected or not. Additionally you could also use a plain color resource instead of a drawable background.
I have an issues, I want to show 20 items in the list.
But there is a catch: if the user scrolls down to the bottom of the list, there will be an item that says: "Show more items", and when the users click on it, more items will be added to the list.
My question is how is poosible to have a last item, that has a different style and looks different: and does different things,(I think this is used in QuickSearchbox)
If you still want a clickable item rather than an infinitely scrolling list you can try using ListView#addFooterView to add your "Show more items" item. This lets you add a view as the last item in a list. Make sure you call it before calling setAdapter.
I would recommend you commonsware's cwac-endless.
cwac-endless: Provides the
EndlessAdapter, a wrapper for an
existing ListAdapter that adds
"endless list" capability. When the
user scrolls to the bottom of the
list, if there is more data for this
list to be retrieved, your code gets
invoked in a background thread to
fetch the new rows, which then get
seamlessly attached to the bottom of
the list.
While commonware has some awesome stuff. His endless lib may not be what you want. What you probably want is a footer. On your ListView, before you set your adapter, call addFooterView. Note that if you do that, the adapter you get from ListView.getAdapter will not be the same as what you passed to ListView.setAdapter.
Edit
Speaking of commonware, he sells a few books on his site. Buy them. They are the best $40 you will spend on your android education.
In the layout below the listview you can put a linear layout with "Clear" and "Get More Results" buttons. Not exactly what you are asking but it can achieve the same result.
I would like to implement a ListView, which I can do no problem w/ my cursor. Right now depending on which row you click on it takes you to a new activity based on the information pressed on that row (just like it should, and as expected). I would like to have a button however to delete the row, so a user can press any part of the row to launch the new activity, but if they press the button on that row, it deletes the row (or launches a delete activity/function).
If you can look # DroidRecord, they have a similar layout as I am looking to achive.
Thanks!
Chris.
As Mariano Kamp said, adding buttons to a row will make it "untouchable", but in my experience, this problem goes away if you set these properties on the buttons:
android:focusable="false"
android:focusableInTouchMode="false"
See also How to fire onListItemClick in Listactivity with buttons in list?
Another possible workaround - you can use an ImageView instead of the button, and set the ImageView's onClickListener (For example, when you're inflating the cell view).
ImageView is not focusable so it doesn't prevent OnListItemClick() from being dispatched, and when you click on the image only the image's listener fires.
what is your question? How to add a button to a list row?
Very simple, just as you expect it will be added to the row's layout.
Unfortunately though that will also make the whole row "untouchable". The Google developer I asked said that this is by design (as far as I remember), and that you should use TouchDelegate to cope with this. As there are no samples, not even in the android source, and only very thin documentation that didn't work for me
Anyway, it seems that not many applications use a button in the list row. I only know about mine (newsrob, see Articles List) and the Alarm clock. Maybe you could use a context menu?
Otherwise the ugly solution would be to add to call setOnClickListener() on your row view in the getView method.
Cheers
It's not the answer to your question, but the long-click/tab is generally the place to pop up a context menu and put extra actions, like delete. You can read how to do it here: How do you implement context menu in a ListActivity on Android?
I would like to thank BoD for its hint on removing the focusable state to the button(s), it saved my day.
But for information, since the button is no more focusable - state_focused on < selector > xml - , its design won't display anymore to the user.
Those buttons will still get the state pressed though, but also when clicking anywhere else on the parent view (anywhere BUT another button) !
Keep that in mind, it could not be a good solution for your own case, but it does work well.
I tried this to be able to click on the buttons but it didn't work for me
android:focusable="false"
android:focusableInTouchMode="false"
so what I did was to change the activity layout to scrollview and then add a linerLayout inside of it.
after that you can add buttons to the layout and each button will be clickable.