Common Errors of Fragment nested call in android Development

Source: Internet
Author: User

Sometimes fragment needs to be nested in an activity, but nested calling often leads to display of views is different from expected or there is a problem with fragment switching. Pay attention to the following points during use:


1. fragment is nested in fragment. The sub-fragment view cannot be displayed:


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

 
 

Xml file of sub-fragment:

 


The running result is as follows:

 

 

Only the parent view is displayed and is not nested in the Child view. The reason for analysis is that the parent view is always displayed but not overwritten. Due to its layout:

 

 android:layout_width=match_parent    android:layout_height=match_parent

To fill the entire screen, so it cannot be displayed. Change layout_width and layout_height to wrap_content. The result is as follows:

 

To enable the sub-Fragment to fill the screen, the parent Fragment must be in the FrameLayout layout. Modify the parent. xml file:

 

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

Result:

 

 

2. to switch between Fragment, A fragmentmanager must be able to manage all fragment. In this way, fragmengmanager can be used to call transaction to perform operations on these fragment.

For example, the demo of the following structure:

Mainactivity includes fragment1 and fragment1 and fragment2. in this way, in order to switch fragment1 to fragmeng2, The Mainactivity includes fragmengmanager fm to switch between 1 and 2. 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 above static method in the. java file of fragment1. Here, getChildFragmentManager cannot be used in fragment1 to manage fragmeng2, because this will lead to 2 as a subview of 1, which is called:
transaction.hide(fragment1).add(R.id.container, fragment2,frag2).commit();
Because fragment1 is hidden and fragment2 is hidden, the screen will be blank.

 

 

This article is only for you to provide an idea when you encounter problems. It is not a technical post.

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.