Android - ListView with only 1 expandable item - android

I have a listview with tickboxes, which uses an Adapter and which works well.
And I know how to make a whole list expandable.
But, it it possible to have just a single item expandable, or does it have to be the whole list?? If so, how is it done?

You would need to use Lists of one item each. Unfortunately there is no SDK widget that handles the special case you need.

You can insert (or remove) the 'child' items in the right place within your adapter's data set when the 'parent' is clicked using a normal ListView.

You could actually use an ExpandableListView and think of the non expandable items as group item with no child.
That's the way I have done it. I have my own adapter which handles showing/hiding the indicator depending on whether the group item has children or not.

Related

Access Adapter items of Recyclerview

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.

Updating android listView adapter's items' view

I'd like to change my listView items dynamically. The algorithm goes like this:
1.I create the default view for my listView using adapter and show it to user. The list item contains Imageview, textview and another imageview which is invisible.
2.The data is beeing dowloaded in the meantime.
3. after my data is downloaded, I'd like to check whether my listview contains any of downloaded items. If yes, I want to make visible the previously invisible ImageView for this item.
Should I add some kind of method to my adapter, call it and then call invalidateViews(), notifyDataSetChanged(), or notifyDataSetInvalidated()? Or maybe there is some kind of standard way to find my adapter's item by Id or sth and then make visible the imageview for this item?
This list update operation is the only one left to implement for me.
Should I add some kind of method to my adapter, call it and then call invalidateViews(), notifyDataSetChanged(), or notifyDataSetInvalidated()?
Yes, exactly.
maybe there is some kind of standard way to find my adapter's item by Id or sth and then make visible the imageview for this item?
Above mentioned way is enough. There is no such standard or special way to do it AFAIK.
Read the Displaying Bitmaps Efficiently introduction and in particular the part about Handle Concurrency. This will give you all the information you need.

Android - Filtering a listview

I want to implement the following:
Listview 1 contains items with checkboxes. If one or more items are checked, listview 2 should only show items that contain the checked items.
What would be the best way to accomplish that?
I had a similar question about how to implement checkboxes in each listview element. It could be tricky since the adapter refreshes the states of each checkbox on scrolling. Check David Scott's answer and my comments on it for proper use. Also check out Joey's answer after that.
ListView adapter with many checkboxes
Regarding your 2nd listview you will have to check your boolean array which rows are checked. Then get all elements at those indexes from your collection and save them. Either you:
Use all the element rows that was checked(in some container class) and set these to your listview adapter number 1. Then use adapter.notifySetDataChanged() and these will appear.
If you want to keep your listview number 1, create a new activity with the 2nd listview, pass the chosen objects to it and use them as elements. Or simply put a listview below the 2nd one with the checked rows.
Dont know how much you know about listviews but this are my 50 cents about the topic.

Android: ListView, Last item - Show more

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.

How to generate a ListView with headers above some sections?

I want to generate a ListView that has some dividers between some of the entries, like it can be seen in some of the property sections. See the example below. I try to generate a List that consists of some textviews followed by one of the fancy dividers explaining the next part of the list and then again some text views. How can this be done? I thought about creating different views to add to the list? Is this the way to go?
I got a solution. I don't know if it is the best one.
I use a custom adapter derived from ArrayAdapter for the list as described in this tutorial. In the adapter class I check if the position in the getView method is a normal row, then I inflate the row layout. If it is the first row from a new group I inflate a headline layout that is a normal row plus the group headline above it.
If you don't want to mix the header into one of your rows. Consider the following solution:
You can overwrite the two methods getItemViewType and getViewTypeCount.
You now have a list that can display different rows. You need to check the expected view type for the item in the getView Method and inflate different layouts depending on it.
The list will handle the recycling for you in a way that it will return only correct recycle views to your getView method, this means if the recycleView is not null it can be used to display your current cell.
You can use my SectionedAdapter, if GPLv3 is acceptable (licensed that way due to some upstream code). You can use my MergeAdapter, if you need something more flexible and with a less-limiting license (Apache 2).
I think you might be looking for android.widget.ExpandableListView
http://developer.android.com/reference/android/widget/ExpandableListView.html
I'm also interested in an answer to this. There must be a more straightforward way to do this.
In looking at the Adapter, there's a method, Adapter.getItemViewType(int position).
ListView defines a return value, ITEM_VIEW_TYPE_HEADER_OR_FOOTER which indicates if the returned item is a header or footer.
I haven't tried it, but I assume if you create your own Adapter and return an item with the type indicating it is a header or footer, that the ListView will display it appropriately.

Categories

Resources