您好,登錄后才能下訂單哦!
先上一段代碼。
TestFragmentActivity.java
package com.xc.fragment; import com.xc.activity.R; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; public class TestFragmentActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_activity); FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.add(R.id.fragment, new TestFragment()); fragmentTransaction.commit(); } }
fragment_activity.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/fragment" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginTop="50dip" /> </LinearLayout>
TestFragment.java
package com.xc.fragment; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.xc.activity.R; public class TestFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { TextView view = (TextView) inflater.inflate(R.layout.fragment, null); view.setText("oooooooooo"); return view; } }
fragment.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="lalall" />
1。上述代碼可以正常運行。出現“oooooooooo”
2。若將TestFragment.java中的代碼替換為:
View view = inflater.inflate(R.layout.fragment, null);
出現“lalall”
3。若將TestFragment.java中的代碼替換為:
View view = inflater.inflate(R.layout.fragment, container);
出錯:The specified child already has a parent. You must call removeView() on the child's parent first.
那么根據提示移除父組件里的子布局:
View view = inflater.inflate(R.layout.fragment, container); ((ViewGroup)view.getParent()).removeView(view);
不出錯,但什么也不出現,因為view已經被移除了。
4。若將TestFragment.java中的代碼替換為:
View view = inflater.inflate(R.layout.fragment, container, false);
出現“lalall”
若改為:
View view = inflater.inflate(R.layout.fragment, container, false); ((TextView)view).setText("!!!!");
出現“!!!!”
5。若將TestFragment.java中的代碼替換為:
View view = inflater.inflate(R.layout.fragment, container, true);
出現和3一樣的錯誤。true和false起到決定性作用。
6。若將TestFragment.java中的代碼替換為:
View view = inflater.inflate(R.layout.fragment, container, true); ((TextView)view).setText("!!!!");
出錯:android.widget.LinearLayout cannot be cast to android.widget.TextView。
若保持上述代碼不動,改變fragment_acivity.xml中的代碼:
<RelativeLayout android:id="@+id/fragment" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginTop="50dip" />
出錯:android.widget.RelativeLayout cannot be cast to android.widget.TextView。
這說明view已經被add到TestFragmentActivity中,并且隨著父組件container變化。
7。若將fragment.xml中代碼改變為:
<?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="lalall" /> </merge>
TestFragment.java中代碼改變為:
View view = inflater.inflate(R.layout.fragment, null, true);
或:
View view = inflater.inflate(R.layout.fragment, container, false);
或:
View view = inflater.inflate(R.layout.fragment, null);
都會出錯:<merge /> can be used only with a valid ViewGroup root and attachToRoot=true。
于是改成:
View view = inflater.inflate(R.layout.fragment, container, true);
出錯:The specified child already has a parent. You must call removeView() on the child's parent first。
結論:
1。看上去inflater.inflate(R.layout.fragment, null)類似于inflater.inflate(R.layout.fragment, container, false);
但是inflate方法中的viewGroup若為null,則inflate出來的view會丟失其屬性,所以通常會放入viewgroup,但是attachToRoot屬性設為false。
(項目中直接在xml中寫ListView,用inflate(id,null)會出現卡UI的現象,但是用inflate(id,container,fale)不會加載到父類中出錯,但也不會卡UI)。
2。inflater.inflate(R.layout.fragment, container, true)類似于inflater.inflate(R.layout.fragment, container)
-----------------------------------------------------------------------------------------
若activity.xml代碼為:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout" android:layout_width="fill_parent" android:layout_height="fill_parent" > <fragment android:id="@+id/fragment" android:name="com.test.TestFragment" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
在TestFragmentActivity.java中:
FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); transaction.replace(R.id.fragment, new TestFragment1()); transaction.commit();
R.id.fragment指代的是xml文件中com.test.TestFragment。
加入一句代碼:
FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); transaction.replace(R.id.fragment, new TestFragment1()); transaction.addToBackStack(null); transaction.commit();
則fragment棧中保存了TestFragment和TestFragment1的關系。按返回鍵,會從TestFragment1回退到TestFragment。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。