java - Android - fragment 重叠

标签 java android xml android-fragments overlap

所以我遇到了一个问题,当我单击 float 操作按钮时,下一个 fragment 会弹出,但另一个 fragment 会保留。我想要发生的是,当我单击按钮时,它会随着 fragment 一起消失,然后弹出下一个 fragment 。

这是带有按钮的 fragment 类

import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * A simple {@link Fragment} subclass.
 */
public class ScoutFragment extends Fragment {

    FloatingActionButton addDataScout;

    public ScoutFragment() {
        // Required empty public constructor
    } //End of ScoutFragment

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_scout, container, false);
        view.setBackgroundColor(Color.WHITE);

        //Setups Floating Action Button
        FloatingActionButton addDataScout = (FloatingActionButton) view.findViewById(R.id.fab);
        addDataScout.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                AddScoutDataFragment fragment = new AddScoutDataFragment();
                FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left);
                fragmentTransaction.replace(R.id.layoutScout, fragment);
                fragmentTransaction.commit();
            } //End of onClick
        }); //End of setOnClickListener
        return view;
    } //End of onCreateView
} //End of class

这是我要弹出的 fragment 类

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * A simple {@link Fragment} subclass.
 */
public class AddScoutDataFragment extends Fragment {
    public AddScoutDataFragment() {
        // Required empty public constructor
    } //End of AddScoutDataFragment

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_add_scout_data, container, false);
    } //End of onCreateView
} //End of class

这是按钮 fragment 的 xml 文件

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/layoutScout"
    tools:context="com.compscitutorials.basigarcia.ramfernoscout.ScoutFragment">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="61dp"
            android:layout_height="57dp"
            android:src="@mipmap/ic_action_add"
            app:backgroundTint="#F28016"
            app:elevation="5dp"
            app:borderWidth="0dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"
            android:layout_marginBottom="12dp"
            android:layout_marginRight="10dp"
            android:scaleType="fitCenter"
            android:clickable="true">
        </android.support.design.widget.FloatingActionButton>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Western University"
            android:id="@+id/testText"
            android:layout_centerInParent="true"
            android:textSize="24dp"
            android:textStyle="bold" />
    </RelativeLayout>
</FrameLayout>

这是我想要弹出的 fragment 的 xml 文件

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_add_scout_data"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.compscitutorials.basigarcia.ramfernoscout.AddScoutDataFragment">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Accept Me"
        android:id="@+id/testViewData"
        android:layout_gravity="center"
        android:textSize="24dp"
        android:textStyle="bold" />
</FrameLayout>

通过 MainAcitivty 启动我的 fragment ,它扩展了 AppCompatActivity 并实现了 NavigationView

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_welcome) {
        //Set the fragment initially
        WelcomeFragment fragment = new WelcomeFragment();
        android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.commit();
        // Handle the camera action
    } 
    else if (id == R.id.nav_facebook) {
        FacebookFragment fragment = new FacebookFragment();
        android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.commit();
    } 
    else if (id == R.id.nav_members) {
        //Set the fragment initially
        MembersFragment fragment = new MembersFragment();
        android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.commit();
    } 
    else if (id == R.id.nav_robot) {

    } 
    else if (id == R.id.nav_scout) {
        //Set the fragment initially
        ScoutFragment fragment = new ScoutFragment();
        android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.commit();
    } 
    else if (id == R.id.nav_match) {
        //Set the fragment initially
        MatchFragment fragment = new MatchFragment();
        android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.commit();
    } //End of if statement

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
} //End of onNavigationItemSelected

最佳答案

我认为这是因为您将当前 fragment 中 View 的 id 传递给了替换方法。 View 层次结构中的该节点当前不包含 fragment ,因此只会添加它。您必须传递包含 ScoutFragment 的 View 的 id。 (如果您使用 xml 扩充 fragment ,则 id 应该是 <fragment> 标记上的 ID。如果您从代码扩充它,则传递与您在 Activity 中传递的相同的 id)

关于java - Android - fragment 重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35976591/

相关文章:

java - Openshift 重定向服务到 https 丢失用户 IP 地址

java - Polarion WorkItem 类 getter 方法返回 null

java - 是否有将 F6 作为默认加速器的 Swing 元素?

java - 单个 Activity 中的多个 radio 组

c# - 从 URL 读取 XML 文件,结果为空

java - 构建具有两个源文件夹的插件失败

android - 如何创建用于模拟改造的 ResponseBody 实例

java - 生成的 GraphQL 类实现查询接口(interface)而不是订阅接口(interface)

java - 如何在 AppCompatButton 旋转时更改其背景?

c# - 获取具有特定元素名称的所有元素