android fragment inside listview - android

my app is a social network where users share links and tag them to let the right persons receive it. Basically, the main activity is simply a listview of posts. I use an open protocol parser to get the web objects metadata on server side.
Now I need to display the right layout in each post item depending of the metadata (video, app, web page, ...). And of course, layouts must react to user clicks event and call intents.
So my first idea is to have a framelayout for each item where I load a specific fragment in charge of generating the right layout depending of the resource type.
But I'm really not sure loading a fragment inside a listview item is a good practice as the reuse system of views is totally messed up.
I'd just like to know what is the best way to implement this functionality for you guys.
Thank you.

This is not really what fragments are designed for. You do not need to use fragments to have ListView rows of differing types -- just override getItemViewType() and getViewTypeCount() in your ListAdapter, then be sure to create the right type of row on demand based upon the metadata.

Related

Correct design for using the same data for different views

I am new in the corporate world & from design perspective please correct me if I am doing something wrong here
I am fetching images from Flickr API.
GOAL: Show these images in two different type of view, grid view and listview. Which can be switch through the slide.
So I am using a View pager with two fragments and both of these fragments has separate listeners. So when the response came from Flickr both of these listeners are notified.
In my opinion, this saves two times calling of REST API, but I am looking for even more efficient design or flow through which
Using single listener
Rest API should be called once
Result should be store (Just in ArrayList) and share to both of views
May not choosing two separate fragments
Avoid creating Adapter object two times
Image should be stored in cache
Any tweak or suggestions will be helpful a lot, please comment if you don't understand any part or whole question.
For above problem, the tweaking you are thinking is almost right. Other than below:
What I believe you must create two different adapters to have more control over different views. For example, you might want to show with scale type crop center for an image in list view but scale type center inside for an image in grid view. There might be the different type of thing you may want to perform. So, it's a good practice to make two different adapters, to make the code more manageable.
Again the same goes for the fragment, see if actions in both the fragments are same or can be done with single variable passing. Then only go with a single fragment.
Rest of the things are perfect.

create tabbed view to display web pages

In my app I am making a web browser in which I want to show different pages in form of tabs. I want to create a view like this
But I have no idea how to do this. Need guidance regarding the components required.
Well the "simple" way to achieve this would be to create an adapter of some sort that overrides the getView() function to create a WebView
and return it as one of the items. The URL of this webview can be mapped to an array or cursor. Since a WebView is a very costly component, you will need to implement the ViewHolder pattern to ensure the webviews are recycled properly.
A lot more work is required to get the rounded corners plus the extra top features but hopefully this can get you started on the right track.

How to handle ListView in this situation Android?

I ran into the situation that I need a way to edit the data of list-view item from another activity. I can handle the click event on each item, then edit it on the fly. However, I still prefer to handle all the editing in a separate activity. My listview item is customized from BaseAdapter.
Here is my main page,
Each item within the ListView, contains two other TextView. When the I hit the menu setting, it will go to another activity where I can edit and update the information:
I'm currently having two solutions in mind. Either retrieving data from the previous activity and update the whole ListView (which I think it's kinda expensive in the case user just edit one item). Or I can just get rid of the ListView and create a bunch TextView in the xml, by doing this I can just reference to each TextView by their id. So my question is, which way is preferred in this case? Any suggestion would be greatly appreciated.
Your ListView is displaying Email, Name, Headline, etc? That should be a fixed XML layout with TextView entries, I think. ListView is for variable numbers of elements. You CAN implement it with a ListView, but I wouldn't.
However, your concern about updating the whole list being overkill, I wouldn't worry about that either. You're talking about 7-10 fields. The amount of time Android needs to run through its lifecycle and display everything will dwarf you updating a few fields.
You can use SharedPreferences for this. You can create a wrapper class through which you can access the preferences.Thats the usual way to go about solving these kind of problems. You can check this for details.
You can have it as a variable in your application class, so that you can access that in a global context.
Use text views instead. List View code has been optimized for large amounts of data only and not recommended for small data.

Android, how to display a list of objects, one per page?

I need to implement an Android viewer to display a list of results (from a real estate site web query), which I have in a list property (
private List<Estate> estatesList;
)
For each item I have one image, one description, one phone number (and some more text fields).
I would like my app to display each result, one per page, and to display the next (or previous) one on a "swipe" (or "fling"?) gesture from user.
The question is: which is the right approach? Should I use a "ListActivity"? If so, how do I bind it to my objects list? Or should I better use a ListView?
A ListActivity is a special kind of Activity that contains a ListView. Basically, a ListActivity does just provide an easier API to build an Activity around a ListView. Nevertheless, this class is a bit useless and tends to be less interesting on the long run. You should better use an normal Activity (I mean build your own class that extends Activity) and add a ListView in it.
You should learn how to use a ListAdapter to feed your ListView with some data (the list model in MVC). You got two main alternatives : using a memory adapter, or use a CursorAdapter that will pick data directly from a database.
There are many tutorials on the web to start learning ListViews. This could be a good one, among many many others.
Regards,
steff
A ListActivity is just an activity that manages a ListView, so that doesn't change much. If you want to swipe sideways, you'll have to use a Gallery instead.
For the swipe, you can detect it using a GestureDetector and your view's onTouchListener. The ListView can be scrolled either using setPosition(int position) (jumps to a given position) or smoothScrollToPosition(int position).
If what you want is something like the home screen, which whole screens scrolling left/right, then you might want to use the Workspace widget. That widget isn't included in the default API, but the code is freely available :)

How to setup a ListView that get its data from a server

I've been trying to find a tutorial of how to create a ListView that get its data from a server dynamically as it's scrolled. I have not been able to find complete documents of how to do this. Only small examples or ListView tutorials that deals with set arrays. This has left me where I don't know where to start building the ListView code.
I have some code that gets data from the server. I also do have all the layouts and basic Activity code.
How/what do ListView do to get one more rows as it is scrolled?
And when I need to have different row layouts. How do ListView handles this?
Is there a complete tutorial that deals with ListView and getting data from a server?
How/what do ListView do to get one more rows as it is scrolled?
There is nothing built into Android for this. You could use my EndlessAdapter, either directly or as an idea of how to accomplish similar ends in your own code.
And when I need to have different row layouts. How do ListView handles this?
You need to implement an appropriate ListAdapter, one that has proper implementations of getViewTypeCount() and getItemViewType().
Is there a complete tutorial that deals with ListView and getting data from a server?
Along with your other two bullets? Probably not. Your requirements are individually uncommon and are even less common in conjunction.

Categories

Resources