简体   繁体   中英

Android Fragments: fragments inside a Frame or other layouts?

I am creating an Android app right now and I have a Frame which shell contain fragments. Till now all works fine, but I have come to a question which I cannot fully understand.

I have a Frame which is the entire screen (FrameLayout - should i use a FrameLayout as the main Frame?) and inside this Frame there are fragments that change, depending on the users interaction. These fragments are in the .xml files FrameLayouts. I am wondering now whether they can or should be FrameLayouts or fragments... I created a Google Maps frgment which is a fragment ideed and that made me thinking.

So my question ist: Does it make a difference or has it any impact on the perfomance or can I simple use whatever serves it's purpose?

To show what I mean, here are some code samples: (Inside the FrameLayout inside the Framelayout are all fragments put)

MainFrame (activity_main.xml):

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="MergeRootFrame" >

    <FrameLayout
        android:id = "@+id/mainFrame"
        android:layout_width = "match_parent"
        android:layout_height = "match_parent"
        android:layout_marginBottom = "@dimen/bottom_Main_Tabs">
        </FrameLayout>

    <LinearLayout
        android:layout_width = "match_parent"
        android:layout_height = "@dimen/bottom_Main_Tabs"
        android:layout_gravity = "bottom"
        android:background="@color/grey2"
        >

        <ImageButton
            android:id = "@+id/bottomButton_home"
            android:layout_height = "match_parent"
            android:layout_width = "0dp"
            android:layout_weight = "1.0"
            android:layout_marginLeft = "2dp"
            android:layout_marginRight = "2dp"
            android:background = "@drawable/ic_home_white"
            android:onClick = "openHome"
            />

        [...]

    </LinearLayout>

    <RelativeLayout
        android:id = "@+id/TopBar"
        android:layout_width = "match_parent"
        android:layout_height = "@dimen/top_Bar_Hight"
        android:layout_gravity="top"
        android:background="@color/grey_transparentBy50"
        >

        <ImageView
            android:id= "@+id/TopBarLogo"
            android:layout_width = "@dimen/top_Bar_Hight"
            android:layout_height = "@dimen/top_Bar_Hight"
            android:background="@drawable/ic_launcher"
            />

         [...]

    </RelativeLayout>

</FrameLayout>

One Fragment (FrameLayout:)

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/home_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/blue"
    tools:context="com.domain.app.HomeFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:textSize="100sp"
        android:text="Home" />

</FrameLayout>

Another Frament (fragment)

<fragment 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"
          android:id="@+id/map"
          tools:context="com.domain.app.MapsActivity"
          android:name="com.google.android.gms.maps.MapFragment"
          tools:layout = "@layout/activity_main"
    />

Is too hae problems with the GoogleMaps implementation but I have not spent enough time on it yet to ask about help here.

Thanks in advance.

John

If you are attaching fragments in xml like GoogleMapFragment or by using FrameLayout(or any other ViewGroup like LinearLayout or RelativeLayout) in xml. Both ways are same. It is more likely creating a textview programmatically (ie in Java) or defining it in xml.

Using fragment in Java:

FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();

and in xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Here R.id.fragment_container is id of your Frame Layout.

Using fragment in XML:

<fragment android:name="com.example.ExampleFragment"
            android:id="@+id/fragment"               
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

In case of Fragment in XML your fragment will be defined by <fragment/> xml tag and on the other side when we are using fragments programmatically, your FrameLayout will work as a container for your fragment.

Conclusion: Using FrameLayout will increase a hierarchy by one but it will not effect your performance very much.

See: http://developer.android.com/guide/components/fragments.html#Adding

The android:name attribute in the specifies the Fragment class to instantiate in the layout. When the system creates this activity layout, it instantiates each fragment specified in the layout and calls the onCreateView() method for each one, to retrieve each fragment's layout. The system inserts the View returned by the fragment directly in place of the element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM