How to set up a real custom Listview? - android

In my app, I should set up a "real" custom listview.
For example :
First line(Some Text) Checkbox
Second line(Some Text) Spinner
What I mean is that each line has different widget. In the example, for the first line, we have a checkbox, for the second, it is a Spinner.
Does someone have any idea of how I can set it up?
Thanks a lot!

Do you really think you need a ListView? Or could a ScrollView fix your problem already?
If you definitely need a ListView, read my answer here: How to add multiple headers throughout a single ListView with addHeaderView()?
You can define different types for your items and return a different layout for each type, while still having the possibility to get higher performance by using convertView.

Check out an example given on this page for defining custom-listview: http://www.androidpeople.com/android-custom-listview-tutorial-example/
Same way, you can define your custom-layout for listview, you just need to implement getView method.

Related

In Android, how can I make a form that changes depending on categories ? (the amount of inputs varies)

I'm doing a reminder application for Android that has an activity which is populated with inputs that depend on the category of the reminder. The amount of inputs (EditText) changes depending on the category, so im a little confused on how to tackle this case.
I propose 3 solutions here, but I'm not sure if there is a 4th solution or a better way to do it. I'm open to criticism. These are my choices:
I can make fragments for each category
Or I can make a common xml layout file that contains a number of inputs that reflect the category with most inputs, and find them programatically and assign them their properties
Or I can make the inputs programatically.
I'm trying to find the most elegant solution here. Thank you for your help.
What you can do is write all the inputs in one file,then set their visibility to hidden, and only programmatically change the visibility to show when you want it to. Tht way if you want to show three inputs, you can change the viibility of the three inputs and the rest of them would still be hidden, hence solving the issue.
Use some kind of adapter-based View. I recommend RecyclerView.
With it, you can covert your form objects into scalable View hierarchies in an elegant, efficient View, using an Adapter object.
You can use <include> to add your different screens and assign the visibility programatically

Android ListView custom row overlay

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.

Individual preference for each listview item example

What's the easiest way to create separate preferences for each item in an Android listview? I found this question, but it's kind of general. Could anyone be more specific, possibly show a quick example?
If you want to do this, you should write some code into your adapter getView method.
You can check the position (with an if-clause) in the method and add a custom desgin/layout depending on the position.
If you use a LinearLayout or RelativeLayout in your ListView item XML you can fill this programmatically in the getView method with all the widgets (edittext, checkbox, etc) you need.

ListItem click and checkbox click at the same time

I've been trying many things with list items and i'm facing some problems. In my previous question, thanks to #nEx.Software i was able to resolve the problem. But I'm still missing some concepts here.
Right now i'm trying to differentiate between an item's click and a checkbox within it. However, I want to do it without extending the array adapter. Is there a way to use both methods: listView.onItemClickListener() AND listView.getCheckedItemPositions(), together!
There should be a way to use an xml file [doesn't matter how complex it gets] along with extending the available Views and this thing should be done.
Putting it simple, open the gmail app, and u'll find all emails listed with checkboxes where u can click on the checkbox to mark it OR the rest of the item to open the email.
Again, I know that it is doable with extending Adapters and adding an Array for the checkboxes, but, is it possible to use the convenient methods: listView.onItemClickListener() AND listView.getCheckedItemPositions()? is CheckedTextView a part of the solution?
One more thing, rather than just answering me [where i become lazy]
Wheather it is possible or not, is there a verry reliable reference for such issues? I would really like to fully understand everything that goes into this matter... if its not possible, i must be able to tell why!
Thank you :)
add custom row into your listview. into your custom row you add one textview and checkbox into linerlayout and get linerlayout click event.

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