I've been searching for a while and only came up with methods to NOT leave an open space when hiding fragments.
I have a 3-panel application and I would like that the remaining panel stay the same sizes when I hide one of them.
Here is my xml code:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="#+id/fragment_container"
android:background="#140506">
<fragment
android:name="tets.twopane.FragmentA"
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/titles"
android:layout_weight="1"
>
</fragment>
<LinearLayout
android:layout_width="5dp"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
<fragment
android:name="tets.twopane.FragmentB"
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/viewers"
android:layout_weight="1"
>
</fragment>
<LinearLayout
android:layout_width="5dp"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
<fragment
android:name="tets.twopane.FragmentC"
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/pass"
android:layout_weight="1"
>
</fragment>
</LinearLayout>
And this is the function in my main activity who hides the fragment:
public void Hide3() {
final FragmentTransaction ft = this.getFragmentManager()
.beginTransaction();
final Fragment myFrag = this.getFragmentManager().findFragmentById(
R.id.pass);
ft.hide(myFrag);
ft.commit();
}
Any help will be welcomed
I think you could probably wrap each of your fragments with a container such as a linear layout and you can transfer the layout weight to it. So for example wrapping FragmentA this way:
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<fragment
android:name="tets.twopane.FragmentA"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/titles"
>
</fragment>
</LinearLayout>
Related
I am facing a problem of overlapping fragments when i inflate fragments from below.
This is code in layout:
<FrameLayout
android:id="#+id/nonAdParent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white_blur">
<FrameLayout
android:id="#+id/collage_view_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white_blur"
android:layout_marginBottom="#dimen/collageview_margin" />
<FrameLayout
android:id="#+id/menu_footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#color/colorSecondary" />
</FrameLayout>
fragment "collage_view_container" contains 1 custom view and fragment "menu_footer" contains horizontal recyclerview.
This is code programmatically:
getSupportFragmentManager().beginTransaction()
.add(R.id.collage_view_container, new CollageContainerFrag().setLayoutModel(model)).commit();
FooterMenuFragment footerFrag = new FooterMenuFragment();
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.slide_in_up, R.anim.no_anim)
.add(R.id.menu_footer, footerFrag).commit();
Issue: Custom view in "collage_view_container" fragment overlap horizontal recyclerview in "menu_footer" fragment on SAMSUNG device solely.
If i config line this code:
if (SystemUtils.isSamsungDevice()) {
getView().setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
then issue not appear but app will process "slowly". Otherwise, issues appear again.
This is whole code of view where "nonAdParent" framelayout located-in.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white_blur"
android:clipChildren="true"
android:clipToPadding="false"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/toolbar" />
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111"/>
<FrameLayout
android:id="#+id/adParent"
android:layout_width="match_parent"
android:layout_height="20dp"
android:visibility="gone" />
<FrameLayout
android:id="#+id/nonAdParent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white_blur">
<FrameLayout
android:id="#+id/collage_view_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white_blur"
android:layout_marginBottom="#dimen/collageview_margin" />
<FrameLayout
android:id="#+id/menu_footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#color/colorSecondary" />
</FrameLayout>
P.S. Please tell if some more code needed. Thanks in advance.
Replace your Frame layout(nonAdParent) part with the code given below
<RelativeLayout
android:id="#+id/nonAdParent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white_blur">
<FrameLayout
android:id="#+id/collage_view_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white_blur"
android:layout_alignParentTop="true"
android:layout_marginBottom="#dimen/collageview_margin" />
<FrameLayout
android:id="#+id/menu_footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_below="#+id/collage_view_container"
android:background="#color/colorSecondary" />
</RelativeLayout>
I am having a layout with four Fragment layouts as shown below.
Now I need to access only two or three Fragments depending upon my selection which is from the database.
How to access only the desired Fragments?
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<FrameLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context="com.example.rknikhil.myapplication.fragment_sample">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<fragment
android:id="#+id/list_Fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
class="com.example.rknikhil.myapplication.ProfileFragment" >
</fragment>
<fragment
android:id="#+id/list_Fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
class="com.example.rknikhil.myapplication.Notificationfragment">
</fragment>
<fragment
android:id="#+id/list_Fragment2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
class="com.example.rknikhil.myapplication.ProfileFragment">
</fragment>
<fragment
android:id="#+id/list_Fragment3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
class="com.example.rknikhil.myapplication.Notificationfragment" >
</fragment>
</LinearLayout>
</FrameLayout>
</ScrollView>
You can simple show and hide the fragment on the basis of the selection.
Use below code in OnCreateView() method . i.e
Fragment f= getFragmentManager().findFragmentById(R.id.list_Fragment);
Fragment f1= getFragmentManager().findFragmentById(R.id.list_Fragment1);
Fragment f2= getFragmentManager().findFragmentById(R.id.list_Fragment2);
f1.getView().setVisibility(View.GONE);
When get in to the fragmentA at the first times, the button show as normal.
Then,I get fragmentB to replace fragmentA.
Then,I get fragmentA to replace fragmentB again and the button disappear in fragmentA.
What's the problem i met?
My custom button code : https://gist.github.com/zhongbaitu/26ad920cfb4273b3206c
My XML:
```
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="cn.zmn.nizaoma.FragmentNizaoma">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<cn.zmn.widget.RaisedButton
android:id="#+id/question_one_but"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="问题1"/>
<cn.zmn.widget.RaisedButton
android:id="#+id/question_two_but"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="问题2"/>
</LinearLayout>
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/nizaoma_refesh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager_nizaoma"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
</FrameLayout>
```
thanks.
I have a multipane main activity.Its layout is
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="#dimen/multipane_half_padding"
android:layout_weight="2"
android:background="#drawable/grey_frame"
android:orientation="vertical" >
<fragment
android:id="#+id/fragment_navigation"
android:name="com.armsoft.mtrade.fragments.NavigationFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<FrameLayout
android:id="#+id/fragment_container_detail"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="#dimen/multipane_half_padding"
android:layout_weight="5"
android:background="#drawable/grey_frame" />
And than I add another fragment to fragment_container_detail with
<FrameLayout
android:id="#+id/planned_routes_map_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
<com.armsoft.mtrade.widget.BidirectionalDatePicker
android:id="#+id/bidirectional_date_picker"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#+id/planned_routes_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
layout.
When pressing menu item I add a MapFragment to planned_routes_map_container
and my application starts blinking.
How can I fix this problem?
i try to display a two-pane UI design. I try to add two fragments in a linear layout with a weight. However, the linear layout seems to ignore the weight. How do i correct it?? Thanks
[Link:] http://dl.dropbox.com/u/78582670/twopanes.png
my layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<fragment
class="com.usci.view.fragment.CatalogFragment"
android:id="#+id/fragment_catalog"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.2">
</fragment>
<fragment
class="com.usci.education.TestFragment1"
android:id="#+id/fragment_test"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.8">
</fragment>
</LinearLayout>
Solution:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<fragment
class="com.usci.view.fragment.CatalogFragment"
android:id="#+id/fragment_catalog"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="8">
</fragment>
<fragment
class="com.usci.education.TestFragment1"
android:id="#+id/fragment_test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2">
</fragment>
</LinearLayout>
always , when you use weights , set the width/height (of the children) to 0px ,and prefer integers for the weights .
so , the solution is :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<fragment
class="com.usci.view.fragment.CatalogFragment"
android:id="#+id/fragment_catalog"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="2">
</fragment>
<fragment
class="com.usci.education.TestFragment1"
android:id="#+id/fragment_test"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="8">
</fragment>
</LinearLayout>
With me is working by putting 20,80 .. try also 0.8,0.2:
<fragment
class="com.usci.view.fragment.CatalogFragment"
android:id="#+id/fragment_catalog"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.8">
</fragment>
<fragment
class="com.usci.education.TestFragment1"
android:id="#+id/fragment_test"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.2">
</fragment>
Set android:layout_width and android:layout_height value match_parent, then for example set android:layout_weight (1 fragment and 2 fragment) value 1