通过代码在FragmentAcivity内变换Fragment遇到的一些问题

先上一段代码。

成都创新互联公司专注于洪洞网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供洪洞营销型网站建设,洪洞网站制作、洪洞网页设计、洪洞网站官网定制、重庆小程序开发公司服务,打造洪洞网络公司原创品牌,更为您提供洪洞网站排名全网营销落地服务。

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。上述代码可以正常运行。出现“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中的代码:

出错:android.widget.RelativeLayout cannot be cast to android.widget.TextView。

这说明view已经被add到TestFragmentActivity中,并且随着父组件container变化。

7。若将fragment.xml中代码改变为:



    

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);

都会出错: 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代码为:


    

在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。


新闻名称:通过代码在FragmentAcivity内变换Fragment遇到的一些问题
网址分享:http://pwwzsj.com/article/ieipdg.html