Use one ListFragment multiple times for multiple data - android

this might be a dummy question, but I am struggling trying to come up with a solution.
In my app I commonly use a ListView with a Header, and two TextViews for row. But right now for each ListView that shows different data I am creating a ListFragment. My idea is to keep only one class extending ListFragment and use that to show all ListViews in the app with the same structure (Header, and two TextView for row).
For example:
In this image I am using two separate Fragments. What Would be the best way to re-use the ListFragment

Related

How to show two different independent objects/classes in one Recycler View?

Is there any way? two independent classes can communicate with single recyclerView...
what i mean actually i have one recyclerView and i have two objects, for instance: one class has "name" and other class has "qualification" something like that, and both classes extends from AppcompatActivity. I wanna show both these in that one particular recyclerView
The Case is:
I am using Room persistence lib. with MVVM i have one Entity class personal information and i am showing all those stuff in recyclerView and working fine. Now i have Button of objectives when i press it, it goes to different activity and have to enter some data and have to show in the same recyclerView and that objectives has its own Entity class which belongs to lets say objectivesActivity class..
in short we can say that, there are two Entity classes and two activity classes but one recyclerView which has to show both of their data..
i need more to know about your use case but my general solution is that you can create two adapter and when you want change RV you can just change your adapter.

Android: reusing a customadapter when dealing with multiple layouts

i'm quite new to app developing and I am wondering about some good design patterns when it comes to adapters for ListViews.
I don't have any specific code (at least not simple and clear code) to accompany the question, but anyway, here is the situation.
I have an Activity in which I want to show any List of Books.
I made a CustomAdapter for this, based on layout called "row.xml." This xml includes a TextView and an ImageView.
I create my CustomAdapter in this Activity as follows:
customAdapter = new ListAdapterFullList(this, R.layout.row, bookList.getList());
Now i want to reuse this Activity to show a List of Books, but the layout needs to be different this time! It should be "row_alt.xml". Instead of a TextView and an ImageView, this list now needs to display a TextView and a RatingBar.
The problem that i am facing here is that obviously I want to reuse the Activity (since what i need to display is still a List of Books and all the other behaviour is the same), but I don't know how to handle the adapter. I can't just use this:
customAdapter = new ListAdapterFullList(this, R.layout.row_alt, bookList.getList());
because this ListAdapterFullList is not implemented to handle a RatingBar instead of an ImageView.
How can i bypass this problem? Do I need to create a different Adapter to show this layout? If so, then I will need to modify my existing Activity also, although I do not know how.
Btw, I read a lot about problems where different layouts need to be used for each row in a ListView, but this is not what I am struggling with. I want to reuse an adapter with a completely different layout for the Adapter. The rows themselves are always the same.
Thanks for the help, and apologies for the possible confusing question. You can always ask for more info!
If row.xml and row_alt.xml only have two different widget, I suggest that you create two different Adapter. If really there is a common behavior between this two Adapters, create a parent class that will implements the common behavior and make your two adapter extends this parent class. Then each Adapter implements its own [getView()](https://developer.android.com/reference/android/widget/Adapter.html#getView(int, android.view.View, android.view.ViewGroup)) method.
Then for further optimizations use the ViewHolder Pattern.
I also suggest that you take a look at RecyclerView which is the new version of ListView and don't need the ViewHolder Pattern

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.

Two ListViews, 1 Activity - and Pull to Refresh?

I am not sure how to go about doing this. This is my requirements for an Activity:
Header
ListView of 10 items
Header
ListsView of 10 items
The ListViewsare coming from two different data sources and may have two different row layouts. So I am thinking I need to make two Custom adapter classes (two getView()'s, etc).
Here's the kicker, I want to be able to pull-to-refresh the whole list and update both ListViews. If that is too much, I'll settle with a refresh button (that Google seems to prefer anyway); currently I use com.handmark.pulltorefresh.library.PullToRefreshListView
Is this possible? If so, what is best steps to make this work?
Very much possible.
Approach can be one ScrollView with one LinearLayout child(vertical orientation).
Now two ListView as child views for linearlayout.
As you said already,you will need two adaptors(with different row layout). Each list can have its own Header View.
For PulltoRefresh functionality,
you can have a look at pulltorefresh library
This allows you to make any View as pull to refresh. As i explained above you need ScrollView as root view, So you need to use PullToRefreshScrollView from above library.
MergeAdapter will help you to merge any number of headers and adapters into a single one. So basically in your activity only one ListView is needed and this ListView should be use a merge adapter with two headers and two custom adapters.
Merge Adapter

ViewPager where each view shares the same ListView

** Current Situation: **
One Activity, with one ListView, that changes its contents based on resetting the adapter between three different adapters. There is just one ListView with id : list. Thats it. When I reset adapter it adjusts the content of the list view.
What I want to do:
Use ViewPager to page between the different list instantiations.
Problem: View pager seems to be setup to take separate layouts, but I have written all my logic against a single list. It would be a big rewrite to use three different ListViews pointing to differnt list.
Question: How do I use the ViewPager to switch between views of the same list that are generated by different adapters being applied to the list?
Just use the same fragment for every page. Set a single variable in the intent that tells the fragment which adapter should be used. Technically it's not the same listview, although it is the same code (which is what usually matters I think).
I don't think it's possible for it to technically be the same listview considering the swiping animation.

Categories

Resources