替换片段不会完全替换之前的片段.为什么这样? [英] Replacing a fragment does not replace the previous fragment entirely. Why so?

查看:64
本文介绍了替换片段不会完全替换之前的片段.为什么这样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一个"fragments-101"程序,在该程序中,单击相应的按钮时将替换一个片段.但是,发生以下情况:

I'm implementing a "fragments-101" program, where-in I replace a fragment when its corresponding button is clicked. However, the below thing happens:

为什么会这样?为什么最初的片段没有被完全替换? MainActivity和两个片段xml文件都使用LinearLayout.我在这里搜索了此问题,发现replace()方法有时可能有问题,因此我尝试使用add(),但无济于事.片段中没有特殊的代码,它是一个简单的hello-world-type片段程序.

Why does this happen? Why is the initial fragment not replaced completely? MainActivity and both fragment xml files use LinearLayout. I searched here about the problem, and found that the replace() method can sometimes be buggy, so I tried using add(), but to no avail. There is no special code in the fragments, it's a simple hello-world-type fragment program.

我正在AVD中使用Android Lollipop版本以及targetSDK,在Android Studio 1.0.2中使用minSDK版本对应于Android 4.0.3.这可能与Android Lollipop有关吗?

I'm using Android Lollipop version in the AVD as well as the targetSDK, with minSDK version corresponding to Android 4.0.3, in Android Studio 1.0.2. Can this be something related to Android Lollipop?

MainActivity.java

MainActivity.java

package com.example.fragmenttest;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;


public class MainActivity extends Activity {

    FragmentManager fm;
    FragmentTransaction ft;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void launchFragment(View v)
    {
        Fragment frag;

        if(v == findViewById(R.id.btnFrag2))
        {
            frag = new FragmentTwo();
        }
        else
        {
            frag = new FragmentOne();
        }

        fm = this.getFragmentManager();
        ft = fm.beginTransaction();
        ft.replace(R.id.myFrag,frag);
        ft.commit();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

layout_frag1.xml

layout_frag1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00ffff">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment 1."
        android:id="@+id/textView2" />
</LinearLayout>

layout_frag2.xml

layout_frag2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffff00">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment 2."
        android:id="@+id/textView3" />
</LinearLayout>

activity_main.xml

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <Button
        android:layout_width="348dp"
        android:layout_height="wrap_content"
        android:text="Launch Fragment 2"
        android:id="@+id/btnFrag2"
        android:onClick="launchFragment"
        android:layout_gravity="center_vertical" />

    <Button
        android:layout_width="355dp"
        android:layout_height="wrap_content"
        android:text="Launch Fragment 1"
        android:id="@+id/btnFrag1"
        android:onClick="launchFragment"/>

    <fragment
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:name="com.example.fragmenttest.FragmentTwo"
        android:id="@+id/myFrag"
        tools:layout="@layout/layout_frag2" />

</LinearLayout>

不能替换直接使用<fragment/>标记从xml布局夸大的

推荐答案

Fragments.

xml布局中应该有一个空的FrameLayout.并且您应该以编程方式添加,删除所有Fragments并将其替换为该FrameLayout.

You should have an empty FrameLayout inside your xml layout. And you should add, remove, replace all your Fragments programmatically into that FrameLayout.

这篇关于替换片段不会完全替换之前的片段.为什么这样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆