Android development fragment nested calls common errors

Source: Internet
Author: User

Nesting calls to fragment are sometimes required in activity, but nested calls often lead to problems with the view's display being different from what is expected or fragment switching. Some points to note when using:


1, fragment nested fragment, sub-fragment view cannot display:


As follows:
The. xml file of the parent fragment:

<pre name= "code" class= "HTML" ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"      android:layout_width= "match_parent"      android:layout_height= "match_parent"      android:baselinealigned= "False"     android:background= "#339922"    android:id= "@+id/mainfrag" >     <textview       android: Layout_width= "Match_parent"      android:layout_height= "match_parent"      android:text= "  hahaha"/> </LinearLayout>  



XML file for child fragment:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout     xmlns:android= "http://schemas.android.com/apk/ Res/android "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android: Baselinealigned= "false"     >       <fragment          android:id= "@+id/fragment1"          android:name= " Com.fff.test.Fragment1 "          android:layout_width=" 0dip "          android:layout_height=" Match_parent "          android: layout_weight= "1"/>        <fragment          android:id= "@+id/fragment2"          android:name= " Com.fff.test.Fragment2 "          android:layout_width=" 0dip "          android:layout_height=" Match_parent "          android:layout_weight= "1"/>  </LinearLayout>

The results of this run are as follows:


Only the parent view is displayed and not nested in the child's view, the reason is that the parent view is always displayed without being overwritten, and because of its layout:

Android:layout_width= "Match_parent"    android:layout_height= "Match_parent"

To fill the entire screen, it cannot be displayed. We will change layout_width and Layout_height to wrap_content, the result is as follows:


In order for the child fragment to fill the screen, the parent fragment must be in the Framelayout layout mode. That is, the parent. xml file is modified as:

<framelayout xmlns:android= "http://schemas.android.com/apk/res/android"    android:id= "@+id/mainfrag    " Android:layout_width= "Match_parent"    android:layout_height= "Match_parent"/>

The result is:



2, to switch between the various fragment, must have a fragmentmanager can manage all the fragment, so that the switch can be used Fragmengmanager call transaction to operate the fragment.

For example, the following structure of the demo:

Mainactivity contains Fragment1,fragment1 and contains Fragment2. In order to let Fragment1 and fragmeng2 switch, in Mainactivity contains Fragmengmanager FM to 1, 2 switching, the code is as follows (in Mainactivity.java):


public static void Switchcontent (Fragment from,fragment to,string totag) {if (from!=to) {fragmenttransaction transaction =fm.begintransaction (); Transaction.setcustomanimations (Android. R.anim.fade_in, Android. R.anim.fade_out), if (!to.isadded ()) {transaction.hide (from). Add (R.id.container, To,totag). commit ();} Else{transaction.hide (from). Show (To). commit ();}}}

Call the static method above in the Fragment1. java file. It is not possible to manage the fragmeng2 in fragment1 with Getchildfragmentmanager, as this will result in 2 as a 1 child view in the call:
Transaction.hide (fragment1). Add (R.id.container, Fragment2, "Frag2"). commit ();
Because, will fragment1 hidden, this thing fragment2 also follow hidden, screen will be blank.


This article is only for everyone to have problems, to provide a way of thinking, not technical stickers, hope that the gods do not spray

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.