新聞中心
先上一段代碼。

成都創(chuàng)新互聯(lián)公司專注于洪洞網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供洪洞營銷型網(wǎng)站建設(shè),洪洞網(wǎng)站制作、洪洞網(wǎng)頁設(shè)計、洪洞網(wǎng)站官網(wǎng)定制、重慶小程序開發(fā)公司服務(wù),打造洪洞網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供洪洞網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
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
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
1。上述代碼可以正常運行。出現(xiàn)“oooooooooo”
2。若將TestFragment.java中的代碼替換為:
View view = inflater.inflate(R.layout.fragment, null);
出現(xiàn)“l(fā)alall”
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.
那么根據(jù)提示移除父組件里的子布局:
View view = inflater.inflate(R.layout.fragment, container); ((ViewGroup)view.getParent()).removeView(view);
不出錯,但什么也不出現(xiàn),因為view已經(jīng)被移除了。
4。若將TestFragment.java中的代碼替換為:
View view = inflater.inflate(R.layout.fragment, container, false);
出現(xiàn)“l(fā)alall”
若改為:
View view = inflater.inflate(R.layout.fragment, container, false);
((TextView)view).setText("!!!!");出現(xiàn)“!?。。 ?/p>
5。若將TestFragment.java中的代碼替換為:
View view = inflater.inflate(R.layout.fragment, container, true);
出現(xiàn)和3一樣的錯誤。true和false起到?jīng)Q定性作用。
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中的代碼:
出錯:android.widget.RelativeLayout cannot be cast to android.widget.TextView。
這說明view已經(jīng)被add到TestFragmentActivity中,并且隨著父組件container變化。
7。若將fragment.xml中代碼改變?yōu)椋?/p>
TestFragment.java中代碼改變?yōu)椋?/p>
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);
都會出錯:
于是改成:
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。
結(jié)論:
1??瓷先nflater.inflate(R.layout.fragment, null)類似于inflater.inflate(R.layout.fragment, container, false);
但是inflate方法中的viewGroup若為null,則inflate出來的view會丟失其屬性,所以通常會放入viewgroup,但是attachToRoot屬性設(shè)為false。
(項目中直接在xml中寫ListView,用inflate(id,null)會出現(xiàn)卡UI的現(xiàn)象,但是用inflate(id,container,fale)不會加載到父類中出錯,但也不會卡UI)。
2。inflater.inflate(R.layout.fragment, container, true)類似于inflater.inflate(R.layout.fragment, container)
-----------------------------------------------------------------------------------------
若activity.xml代碼為:
在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的關(guān)系。按返回鍵,會從TestFragment1回退到TestFragment。
文章名稱:通過代碼在FragmentAcivity內(nèi)變換Fragment遇到的一些問題
文章位置:http://www.dlmjj.cn/article/ieipdg.html


咨詢
建站咨詢
