I googled this answer but didn't find something for me.
I want to use fragments (two different fragments) as items on RecycleView.
If you know how I can do it please help me.
Thanks.
Create an interface and use it as your generic type of your RecyclerView and Adapter. Your item's layout will be this interface. And finally your fragments implements this interface.
Note: Fragments may cause irregularity since they are attached activity. Not preferred!
Related
I am having a situation in which as a List Item I want to inflate a Fragment, but It seems like a bad approach. As Fragments are processed/managed by Activity's FragmentManager or by child FragmentManager and list item views are by ListView & ListAdapter.
Any comments and research regarding this would be highly appreciated.
Thanks
Here are my views and questions in mind on your problem.
You want to use ListView with fragments, since you already have a fragment which does that job and you dont want code to become redundant.
Though you can definitely use fragment, but i suppose its not the best practice. Fragments have their own life cycle and you are not going to use fragment life cycle methods (I suppose). Thus semantically it would not fit into this usecase.
And also your adapter will always be dependent on activity to retrieve fragments. (could there be any problems with orientation change again?)
List items and adapters are finetuned to work really well with scrolling really long lists. While the list view items get recycled when using view holder pattern, while scrolling, does any of fragment lifecycle methods come in between? would that cause performance impact. (I suppose yes. Havent tested it out yet)
You can instead have your view code in different layout file and include this layout in both fragment and also list adapter.
<include layout="#layout/YOUR_COMMON_VIEW_CODE"/>
and have utility class which takes the context and this layout container. Have all the functionality exposed inside that utility class.
You can't use fragment as list item views because the API doesn't allow you - View and Fragment aren't even related so there's no way you can use it like that. Make custom views and use adapter getViewTypeCount and getView to use different list item behavior.
Fragment are managed by Activity's FragmentManager or by other Fragments child FragmentManager; while list item views are managed by ListView & ListAdapter. You can use ListViews in Fragments, but not the other way around.
I googled and got this.
Is it possible to inflate a ViewPager inside a Fragment which is inflated on a Activity? Coz almost all examples that a found is a ViewPager inside a FragmentActivity and i dont want to use FragmentActivity (for some reason). It will be looked like this photo :
Please help me, thank you :)
Yes, it is possible, but you have to remember the FragmentManager used inside a fragment to manage child Fragment (for example, the one provided to your FragmentPagerAdapter in its constructor) is obtained with getChildFragmentManager and NOTgetFragmentManager. That is very important, your app will crash if you don't.
Have a look at this tutorial for an example implementation.
In my app I have the dropdown enabled in the ActionBar. The user has two elements to choose from. Depending on the choice I want a ViewPager to show different contents from different FragmentPagerAdapters. The user has to have the ability to switch all the time.
I've tried to set two different PagerAdapters in the listener with no luck. The Pager would just reload the previous fragments every time. Similar problems are described here and here. However in my approach I don't want to change the content of one adapter. I really just want to switch between two separate adapters.
This whole thing seems really confusing to me. Are there any known workaround for this or is there an alternative solution to my problem?
If it is possible for your application, I would suggest using FragmentStatePagerAdapter instead of FragmentPagerAdapter. Each adapter should be able to store a separate list of fragments, and should keep the saved state of your fragments when you switch to the other adapter.
I've tried to set two different PagerAdapters in the listener with no luck. The Pager would just reload the previous fragments every time.
I am assuming that you are using FragmentPagerAdapter or FragmentStatePagerAdapter. Both of those store their fragments by tag name in the FragmentManager, and therefore this switching approach will not work.
Are there any known workaround for this or is there an alternative solution to my problem?
One approach would be to fork FragmentPagerAdapter or FragmentStatePagerAdapter, replacing the makeFragmentName() method to use an alternative tag syntax, and use the revised adapter class for one of your two.
There are two fragments. However it is slightly different from the tutorials case when there is one ListFragment and one DetailFrament, because I have two ListFragments. Now the problems is when I use this in one fragment :
getListView().addFooter(someView);
It would automatically assign this footer (or header) to another fragment as well, because getListView() method "Gets the activity's list view widget" and my two ListFragments are both in the main activity.
Any ideas how to get around it???
Considered having a reference to you listview by an id in the xml? Then you could find the listview in oncreate and do something like following:
myListView.addFooter(someView);
Uhu solved.
The problem was that one of my fragments was actually called "ListFragment" and at some point my fragment and the actual class ListFragment were confused and it caused a problem.
I took this idea from this tutorial but it seems that that guy has already tweaked it for the better. Anyway I shall punch him in the face on the first occasion.
How to put Expanded List inside a Fragment Tab?
I have 3 fragments in one page, The 2nd Fragment consist of two tabs,I want to put an expanded List inside the second tab.please help ,Thanks :)
Correct me if I am wrong in assuming that you are using ListFragment to show the lists. If you want to use a ExpandableListView you are going to have to implement it yourself inside a normal Fragment since there is no ExpandableListFragment implemented in the APIs.
Here is a link to Android's documentation on how to implement it: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList1.html
Hope it helps.