Android Fragments In A List View - android

I have looked everywhere for examples on how I can have individual fragments set up as in a list view. It seems either this is highly not recommended or not possible. Does anyone have examples that I can look at? Essentially the app that I am trying to build is a an e-commerce app with a list of items to purchase. I know it may be overkill to have every item to be listed as a fragment but for this project, I need to do this.

This is highly not recommended. If your using a ListView then your xml layout file for each list item will be the same. Ideally if you use a ListView your elements will be mostly the same. You could have one Fragment for when an item is selected from your ListView, however. This one Fragment will be able to function properly for all elements of the ListView. That way you only have on Fragment for all of the elements in your ListView. Having a Fragment for each item in a ListView would be very inefficient.

Related

Android List view row move vertical

I want to create List view row animation like below . I want to move row from one list view to other list view. Both list view are in same activity.
Anyone can give me idea how I can do this.
First of all because you mentioned "ListView":
In my opinion the best way to perform dynamic "lists" in android is to use to android-given class
RecyclerView.
It's easy to use like a normal ListView but like I said before it handles dynamic data.
Moreover it has some support librarys like ItemTouchHelper to drag/drop and swipe items in the list around. Its very easy to expand your RecyclerView with this upgrade. Here is a good tutorial:
Tutorial.
I would like to give you two ideas how I would proceed to implement such a list like the example of your post:
1) (Recommended) Search on Github or similar sites for 3rd library parties that already solved this.
2) Use the RecyclerView with the ItemtouchHelper-Upgrade i mentioned above and try to expand it with two lists. When an item is onMove() set the visibility of the first list on GONE and the second on VISIBLE. Now you only have to add the data of your item to the second list and remove it from the first. Then use notifyDataSetChanged() on both lists and your done.
I dont know how difficult it will be to implement it but thats the only way I know how you can do that and how the programmers of your example could have done it.

How to share adapter items with two recycler views?

I am trying to implement Instagram like Search fragment where they have explore posts(contains only the image) in a grid layout and when we click on them, it changes to another feed like view showing more details.
The way I am thinking to implement is to have two fragments with recycler views: 1 for grid layout and 1 for feed like. Share the currently, loaded feed items between these two recycler view adapters.
Question: Is it the correct approach to share adapter items between two views? If not, what would the design be? I suspect I will run into issues if we share the items.
Kindly let me know if any more details is needed. I can share pictures as well in case someone needs for android device.
I went ahead and implemented my design. Till now, I haven't seen any issues.

Whats the best way to put a customized scrollable list in android studios?

I'm trying to make a android app with a scrollable list like this one...
When I made this app in iOS, I used a UICollectionView. What`s the best way to make this in android studios?
I tried using a list view, but I can't seem to customize it to my needs?
Any Ideas?
ListView was a great way to start and it is customizable to your needs.
However I would recommend to use RecyclerView which works almost on the same principle as ListView but it is a newer concept in Android. It handles the usage of the ViewHolder pattern for you which makes everything super easy.(With ListView, you would've had to implement your own ViewHolder pattern)
All you need to do is to have the RecyclerView in your activity/fragment as the view to hold your data. Then, the key component is to implement the RecyclerView's Adapter which will handle the inflation and setup of each list item.
Here is a really great and short tutorial to get you started with RecyclerView.
If you're done with that here is a bit more advanced video material on RecyclerView from Dave Smith explaining a lot of ways on how to understand and use RecyclerView in your apps.
A ListView fed by an ArrayAdapter of custom objects can do the job.
You might have a class that contains the text and the image of a single item.
Then you can create an ArrayList with items of that class, where you put in all the data.
This ArrayList can be used as dataset for a custom ArrayAdapter, which overrides the getView()-method.
The getView()-method must inflate the xml-layout for each item and attach the data from the ArrayList to the inflated view.
You might want to implement the Viewholder-pattern to achieve a good performance and avoid unnecessary inflations and getViewByID()-calls.
You can also use the new CardView, which can make it a lot easier, since it is made for the new material design in android 5, which looks similar to your item list.

Using one or multiple ArrayAdapters and where to place them?

I'm not sure how to correctly build my application. I have an activity that has an array of items. The activity has no GUI. Further on, I have multiple fragments. Each fragment should read (filter) items from my Activity and display the items using the same views. So each item is displayed using the same view. Should I now implement an ArrayAdapter in my activity or should I create multiple ArrayAdapters in my fragments? Unfortunately I'm not used to work with adapters.
Thanks for your advice!
You can share Adapters between AdapterViews if they have the same backing data. But as a general rule that's not the case.
In short, unless all AdapterViews (e.g. ListView, Spinner) show the same items, you should use separate Adapters.

Listfragment divided into sections

I am programming for android 4.0
I would like to create a listFragment divided in 2 sections. This means i want it to be 1 long scrollable list but with a divider between the online items and the offline items. And of course when one item comes online it should jump upwards + the other way around.
All the items are clickable but the divider shouldn't be (and preferably have a differend colour)
How can i do this or is this even possible?
Ok so , a fragment is basically an activity and you can treat it like an activity, in your case you should extend ListFragment (which will act like a ListActivity in a sense).
now, a List Adapter (which populate a list) in its default way will only allow you to display a list in its most simple form so in order to achieve what you want (a list that deals certain list items differently) you will need to write your own Adapter.
its best if you get the data in the order you want them to be displayed so if you can sort the "online" items from the "offline" items straight from your data source you should query it this way. so now the only thing you need to add is a separator between them and you can do it by finding the first "offline" item and inflating a separator above it (this is done inside your adapter).
each task by its own has dozens of tutorials and Q&As around the web and on StackOverflow.
hope it helps and i'm here if you need more help.

Categories

Resources