I have a ViewPager with a FragmentPagerAdapter. My customer wants me to switch from ViewPager to list; a vertical list with all the fragments.
There can be a lot of fragments, so adding them all in a ScrollView is out of the question.
Can I somehow do this while still using a FragmentPagerAdapter? Adding fragments to, for example, the views in a recyclerview is not a good idea, I suppose?
What might be a good solution? I'm afraid I will have to stop using fragments for this altogether, but there is a lot of controller code in these fragments.
Do not use any fragments on any recycling views like list view, recycler view etc. Fragments are attached to its container, in recycler view container will be changed frequently, as it is getting recycled. It will trouble later.
Better you can change the view pager to vertical scrolling view pager.
You can refer link to make it vertical scrolling.
Related
I am a little bit confused on how should I approach this particular case of doing some swipes between fragments.
So yea, I asked ViewPager or RecyclerView, because these 2 are my only options, if anyone can come up with a better idea, it is really welcome.
The flow is the following, I have a Main Timeline(ListView), each item of it opens a fragment with details about it. What I would actually want to do is to swipe between these full screen fragments without going back to MTL and open another item of the list.
You would ask me what I tried, well:
RecyclerView - HORIZONTALLY oriented as a root of the fragment, and each item of this RV had the details of each event. The problem with this is that it gets really buggy because I have a huge logic inside each item(like, another RV - horizontally , a PagerView also horizontally to swipe between images (or a youtube frame that is being played if is the case. Plus a lot of other stuff in this, so the logic of parent RV inside the onBindViewHolder() is really tricky.
Would be better to use a PagerView with fragments(since I have the DetailsFragment kind of ready) ? The problem here is that I need a certain number of swipes, right ?
Go with viewpager.
Because creating fragments inside recyclerview causes recyclerview performs to slow down.Also to create fragments in onBindViewHolder() dynamically every time you need different unique id of frame layout to load which will be tough to generate.
For more information on why recycler view is bad idea to load fragments check this.
Fragment replacing in RecyclerView item
Also try to use the ViewPager with an implementation of FragmentStatePagerAdapter. The adapter will optimize the memory usage by destroying fragments that are not visible at a given moment.
Check the documentation for details and code sample.
https://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html
I have a ViewPager. Inside this ViewPager I have Fragments that have all the same layout. The layout contains a header and a RecyclerView and I want only the header of this layout to be swipeable. So the user may only switch between the fragments inside the viewpager when he is swipping on the header. I have tried a CustomViewPager but it was not working the way I wanted it to.
Does someone have a solution?
My dirty but working solution is to set a custom TouchListener to my header view and every time it detects a fling to the right or left I call setCurrentItem(index, true) with the ViewPager. It is not the optimal solution, but is working for now.
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.
I have an activity layout in which I have a viewpager that contains a couple of fragments. One of the fragment holds a listview (image sample attached).
What I'm trying to do is to scroll the listview and stick the TabStrip to the top of the layout, so when you are scrolling the list, the viewpager's fragment titles are always visible but you can see the entire listview content.
Above the viewpager there are views that needs to scroll up when the listview is scrolling.
Is any way to achieve this?
I've tried with scrollview and placing the viewpager inside it, but it doesn't work because the viewpager doesn't display at all.
Please help!
i've found the solution by using this nice library from github!!!
noties/Scrollable
I wrote a FragmentActivity app with the use of the v4 support library, consisting of a fragment with a layout that contains a listview (list fragment), and a reusable fragment that changes its layout based on the list item selected (detail fragment).
In the layout for large screens, I have a fragment tag for the list fragment and a scrollview as a fragment container for the detail fragment, to allow fragments whose layouts overflow out of the screen.
Originally, for small screens, I used a basic ScrollView as the fragment container. But, since the listview in the list fragment is a scrollable fragment and there cannot be scrollable views in a ScrollView, it couldn't be scrolled; though the other fragments could be scrolled.
So, I changed the ScrollView to a FrameLayout instead, and while fragments with scrollable views can scroll, the fragments that overflowed the screen could not be scrolled.
How would I go about this problem, with the intention of enabling both fragments with scrollable views and overflowing fragments to scroll in a one-panel fragment view?
Thanks in advance
EDIT: I might be able to use ScrollView in the dynamic layouts used by the detail fragment as I see fit, using it where there are no scrollable views, but are there any better solutions?
Ok, I guess this does require the ScrollView to be used only in layouts in which there are no scrollable views.
I stopped using ScrollView as the root element in the fragment activity layout, and I manually used ScrollViews where applicable, and it's working now.
A better way is still welcome and appreciated.