I'm having trouble aligning the fragments to the center of the view pages, in a way that the user can see multiple fragments on the screen. This problem has already been solved before, but not when the fragments have different size.
Can ViewPager have multiple views in per page? is one link that I used. The pagercontainer solution on it works but is very slow. My solution was to set ViewPager padding through setPadding(int, int, int, int) method and false for setClipToPadding. Doing so makes the fragments look as if they're aligned in the center, but assumes that all fragments are the same size (since setPadding is a global setting for a View). So having different fragment sizes just completely messes the alignment.
I was hoping if someone knew how to make it so that the current fragment in view pager can be aligned to center without the use of padding. That way one could have different fragment sizes. Alternatively, could someone suggest how to achieve center alignment with padding with variable fragment sizes?
PS: I've searched everywhere online and couldn't find a solution.
Rather than aligning the fragment inside the viewPager, why dont you try putting all your elements in a layout and then align it to center(inside the fragment).
Make viewPager as wrap_content. and make fragment main layout params as wrap_content too.
If this does not help you. You can share your layout.Also, add one more tag to your question - "android_layout"
Related
I have a project with 99 fragments. A client wants us to make our BottomNavigationView semi-transparent and have our content scroll behind it. But not all our fragments are scrollable. Some are RelativeLayouts with controls attached to the bottom of the page that need to not allow themselves to go down past the BottomNavigationView, so that they're not covered up. And even with the pages that scroll, sometimes it's not even the entire page that scrolls, but just a subset of it. Is there any way I can achieve this kind of ambiguity without going through all 99 of my fragments and making a unique judgement call on each or them?
There is no direct solution as such for what you are trying to achieve.
If your fragments has RecyclerView then add extra bottomSpacing for last item in the list in ItemDecoration class.
If your fragment has ScrollView then add extra paddingBottom for last view in the hierarchy.
If your view has anything other than these two scrolling views, then you can hardcode the bottom margin and give a color below matching to the navigation view. This make it look opaque from translucent.
One thing that is understood is the bottom spacing, margin and padding will be equal to or slightly greater than (ideally height + 8dp) you bottom navigation.
Since you fragments behave differently, there is no use of trying to make single solution for the same. But to make the job easier for future, you can externalise these margins and spacings in dimens.xml (use same object for all the spacing) and hence in future if any requirement changes, you can change the spacing directly from the dimens.
And you can do the same for bottom navigation spacing color, externalize to colors.xml and change it as you like.
My view hierarchy: (left image)
GreenTextField in RedFragment in BlueFragment in BlackFragment
I need to have the GreenTextField go over parent fragments bounds like the image to the right.
I have tried with negative margin, but I doesn't work.
Any suggestion about how to solve this?
This fragments layout is needed and this sample is simplified a lot.
Result: As expected this is not really possible.
First, if there is a chance to use ViewGroups instead of those nested Fragments, you should think about redesign. It would be crazy to maintain such code :)
But if you really have to keep this structure, try something like that.
Set android:clipChildren="false" to all parents of GreenTextField.
This works for layouts, but it could not work for Fragments, so see here.
Calculate GreenTextField's width and set it (of course programmatically).
Make your GreenTextField draw on top, over right blue part. There would be no problem if the blue structures are inside RelativeLayout, but probably they are not. Then try with bringChildToFront(View child) from ViewGroup.
This is impossible.
This is not the purpose of fragment(as independent ui element) and fragments' child view can't exit its parent limits (width and height)...if it is, its a design flaw.
In order to do so you sholud put the black fragment and the textField in a relative layput.
textfield height issues -> change it dynamically on onViewCreated method, of the left red fragment, in order to place it on top of its position.
another solution is to put the textField under the black fragment, and set its position dynamically.
Set to the black rect to be a parent of the green. Calculate offset manualy.
Ok, I have a LinerLayout vertical in which there are three fragments, lets say: HeaderFragment, SubHeaderFragment and ListViewFragment (fragment with listview in it). These are put in the layout in this order, width set to match_parent. The header and subheader fragments have a defined height, lets say 100dp each. The list fragment's height is set to wrap_content.
Now, what I want to do is that when the list fragment is scrolled down, the SubHeader fragment should loose its height (according to the amount scrolled) until it disappears and the user checks the list. If the user scrolls back up to the beggining of the list the SubHeader fragment should get its height back, again according to the scrolling.
This can be seen in several Apps, for better use of the screen size.
I have researched quite a bit, but I found no simple solution to this. One would be to keep track of the scrolling position of the list and reset the heights dynamicly while this is done. Which is trouble because as far as I know the scroll position for the list must be calculated and I am dealing with fragments that have no layout params.
Another idea I had was to put the SubHeader and the List Fragments in a scrollview within the linear layout, but that destroys my list within its fragment and people say it shouldnt be done due to bad performance.
Is there a decent simple solution for this? Thanks for any help in advance!
I am having a really weird problem. Here it goes:
I have got a viewpager which I use to navigate between some fragments. In one of those Fragments I am adding a surfaceview that is supposed to fill the whole UI to a relativelayout. I add it like this:
mRelativeLayout.addView(mMySurfaceView);
Now, the problem is that even though the surfaceview fills up the whole screen, the viewpager does not. There is a small gap between viewpager and screen border. In other words, the viewpager does not fill up the whole width.
However, when I add another ui element to my relativelayout, like an imagebutton like this:
mRelativeLayout.addView(myImageButton);
then everything is fine and the viewpager fills up the whole screen.
Why is this happening??
Try with:
//in onMesure()
mMySurfaceView.setMeasuredDimension(int, int)
more info: http://developer.android.com/reference/android/view/View.html#onMeasure(int,int)
The TabCarouselLibDemo source is here. Thanks for the author's work.
The demo shows the ViewPager is below the tab, but in the xml, the ViewPager seems that it is at the top of the tab.
I think if you can see the demo you will understand what I mean.
I have a question about how to make the ViewPager below the tab? I have seen nothing about it in the code.
It makes me feel uncertain.
Thank you in advance!
It has to do with how RelativeLayout draws its children. If the ViewGroup was a LinearLayout and the orientation was set to "vertical", the carousel would be placed below the ViewPager, like you're thinking. But that's not how RelativeLayout works.
As far as the content in the ViewPager being below the carousel, you have to look at the custom Adapter used. There's a layout called "faux_carousel" that's used to create a layout with the same dimensions as the actual carousel, without showing the carousel itself. That layout is placed at position "0" in the Adapter so that all of the other content (that's in the ViewPager) will rest below the fake carousel and therefore the real one too.