温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-09-26 18:05:02 来源:网络 阅读:1770 作者:xc0415 栏目:移动开发

先上一段代码。

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。




向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI