Which method is better?
1) Using a listview
2) Inflating and adding views in somelayout manually
IMO:
Use a listview.
Because
1) Listviews are standardised Android UI elements. They are recogniced by users as such, and used by developers that know how to use them. If any of them then has to maintain your code, (s)he'll thank you.
2) With some personal experience: Down the road of doing this manually: Lies pain and madness of the worst sort. Just use the tools avilable and save yourself the headache. ;-)
It deppends on what you want to do...
If your layout will be like a listview, make a listview with your custom adapters, more optimal and fast ;)
It depends on the use case, though adding views dynamically is going to be slower, also avoid deeper layout hierarchies.
For list view use ViewHolder to hold the views.
If you need your list to be customized or to have different look and feel than the android inbuilt list, then you can go for inflating and adding views in some layout manually. But efficiency wise , you can go with inbuilt one.
This is help you to know the things more better.
http://www.technotalkative.com/category/android/listview/
Related
Does someone know to implement this kind of views structure in Android ? It's from youtube app.
Thanks for your help.
Android have a great little tool when you want to know how something was done.
It's the DDMS (device monitor), look around on Eclipse or Android Studio that you can find it.
Over there, you will find the option: "Dump View hierarchy for UIAutomator" and you can use it to get the View hierarchy of different apps. So for example YouTube:
You can see that it's just a normal ListView with a very cleverly built Adapter. Check out carefully the BaseAdapter class https://developer.android.com/reference/android/widget/BaseAdapter.html
specially two methods:
int getViewTypeCount();
int getItemViewType(int position);
those can be used to have different types of view depending on the position of the ListView. So then it's just built one-by-one, the types you want to implement.
The "more" will do nothing more than add more items the data backing the adapter and call notifyDataSetChanged()
happy coding...
You would need to create more than 1 listview and use padding around it to create the space between one listview and another. You also need to make the layout where the listviews are setup scrollable.
I would personally setup a fragment for each listview to be able to load more data in each list.
Hope this helps. :)
When you are working with a long, big list, certainly one should use ListView because it handles cell recycling.
Notice here, for example Can i use nested linearlayouts instead of list view, for a big list? the OP is asking about ListView verses a dynamic LinearList -- the answer is "have to use a ListView, because of recycling"
Now, say you are making a short list -- imagine say a popup with only 10 or 20 items. It may even fit all on the one screen, so there's no recycling.
In fact, is there any difference between using a ListView and just using a LinearLayout, and dynamically populating the little views inside it?
It seems to me that the latter is in many cases much simpler, more elegant, and easier to work with. But I could well be missing something that seasoned Android engineers know about.
Should I just use an ordinary LinearList (populate it dynamically) for lists where recycling is not relevant? What's the usual, and why? Cheers!
{Incidentally, for popup cases, is there some better, lightweight method for "choose one from a popup-list" that I'm too silly to know about?! :) )
ListView(and other lists) supports very useful idea: splitting data and view. These parts could be changed at any time so it's important to support flexibility. And it could be solved by special mediator object: Adapter. Adapter roughly speaking says how to fill your view with particular data item.
So I'm sure that if you decide to use LinearLayout sooner or later you will implement you own Adapter.
If you used dynamic linear view then rendering the view will take more time as compare to listview. In listview we are rendering views which are visible only but if you used dynamic linear view then its problem.
can someone help me and tell me is this possible in android. I need to have in my app a gridview alike widget, but with different item height and width, like this:
So is this possible, and if so, what can I use to achieve this.
Thanks
For this you need a work around or you can say a third party Library named as "AndroidStaggeredGrid"
You can check it on here
#Darko,
Yes, it it possible to do like this in Android.
There are many ways to implement this. As you said GridView cannot be used to achieve this. For a normal user this will appear has grids.
Ways:
Use fragment for each and every "grid" you can see and each fragment will have its layout to display either 2 or 3 images in grid (No gridview is required here too). Use ImageViews as usually.
If the layout design is same, then you can define a single layout with internal design in the same format. Each row with imageviews - This is not encouraged by the way.
Design layouts for each and every format of design per row. Inflate each particular row to a list (using custom adapter). Based on position in getView() you can control which row design to be added to list. Inbuilt scroll in ListView, so no problem!
I suggest you to go with number #1 or #3. Using fragments you will have better control.
If you come to know any better idea, please share!
It's StaggeredGridView
You can find an implementation at https://github.com/bulletnoid/StaggeredGridView
It's possible using GridLayout. More details in the link.
http://developer.android.com/reference/android/widget/GridLayout.html
I know it may be weird that I want to succeed.
Let me explain whole in details. Firstly, I want to use two ListView looking in an Activity screen. Just like how Pinterest looks like. (I'm not sure is it true but it's named as heterogeneous gridview.) There is also one thing that I needed to implement: lazy loading. There are about 30 images in my activity, so that's not good to fetch all images at one time. That's why I needed lazy load.
So in first, I pondered on that I how obtain that appearance, I mean heterogeneous gridview. Then decided to add to LinearLayout in a ScrollView. Linears have vertical orientation. With thay approach, I added my new views by using odd-even method in Linear Layouts.
Althought, there weren't any adapter so according to my knowledge, there is no way to controll which items would be displayed. So lazy load is not suitable.
I've also tried two ListView in an Activity, but got some lag and also synchronous scroll problems. On the other hand it has a bad recycling.
So in this scenario, I want to ask is there way to fill LinearLayouts using Adapter? Or what would you recommend to create a heterogeneous gridview but also with lazy load.
If my approaches are wrong, I would not hasitate to change it.
Any clue would be great for me. If there is something unclear, please specify it. I'll explain with more details.
After a lot of research, finally I found a library which have lazy loading and heterogeneous gridview and also not has any scroll or recycling lags/problems.
Maurycy's StaggeredGridView is completely what I'm looking for.
Here is the library and also there is a demo too.
If I get your question right, here is what you need: http://spinlist.autsia.com/
In order to enter information into my database, I'm currently using a scrollview (with a table layout, iirc). However, looking at the api demos, I'm wondering if I should switch to a listview. It seems a bit of a strange way to do it, as I'd have to hard code the numbers of items, it would all have to be in java rather than xml, and each one would need a separate onclicklistener, but as far as layout is concerned it sounds like by far the easier to maintain.
(I'm considering the switch now as I'm doing a major rewrite anyway, so now would be a good time)
Anyone have any opinions that they wouldn't mind sharing? Would this be a horrific abuse of a listview, and should I stick with what I have? Or should I move to something that I can much more easily theme later on (my current layout is pretty ugly)?
I think of ListView as being the exact opposite.
You don't have to hard code list items: they come from various adapters like CursorAdapter and ArrayAdapter.
You can use a single OnItemClickListener to handle the click events for all of your items. onItemClick provides you with the position of the item that was clicked.
Even your presentation can be dynamic, since you can easily switch out a GridView for the ListView later.
I would recommend trying ListView; it will give you another tool to work with and inform your design decisions in the future.