Android Fragment不会从活动

时间:2018-04-12 13:55:00

标签: java android android-fragments mobile

我目前正在构建一个使用导航抽屉的Android应用程序。当用户从抽屉中选择项目时,创建与所选项目相对应的片段。例如,如果用户从导航栏中选择联系人项目,则UI应从仪表板更改为联系人布局。

我正在成功创建片段,但Fragment会从我的仪表板中保留calenderView。

这是仪表板布局:

Here is the Dashboard layout

一旦用户从导航抽屉中选择计时器,这是布局 enter image description here

问题是当片段膨胀时,calenderView仍然存在。理想情况下,它只是"定时器片段"在此片段中可见的按钮。

这是我的mainActivity代码:

//Fragments will be inflated through this method.
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {

    Fragment fragment = null;

    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_patient) {
        fragment = new Patient_Fragment();
        // Handle the camera action
    } else if (id == R.id.nav_timer) {
        fragment = new Timer_Fragment();

    } else if (id == R.id.nav_reminders) {

    } else if (id == R.id.nav_contacts) {

    } else if (id == R.id.nav_account) {

    } else if (id == R.id.nav_about) {

    }

    if(fragment !=null)
    {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();

        ft.replace(R.id.dashboardArea, fragment);
        ft.commit();
    }

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

这是我的计时器片段:

public class Timer_Fragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       return inflater.inflate(R.layout.fragment_timer, null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }
}

这是片段XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
    android:id="@+id/timerFrag"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    android:text="Timer Frag"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.181" />

这是仪表板XML:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/dashboardArea"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.a517086.carefree.MainActivity"
tools:showIn="@layout/app_bar_main">

   <CalendarView
    android:id="@+id/calendarView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
   </FrameLayout>

2 个答案:

答案 0 :(得分:0)

您应该将CalendarView放在与日历片段相关的另一个xml片段中,就像使用定时器片段一样:

fragment_calendar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

 <CalendarView
    android:id="@+id/calendarView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

CalendarFragment.java

public class CalendarFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       return inflater.inflate(R.layout.fragment_calendar, null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }
}

您的信息中心xml文件应为空,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/dashboardArea"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.a517086.carefree.MainActivity"
tools:showIn="@layout/app_bar_main">
</FrameLayout>

然后,在您的主要活动中初始化日历片段,就像那​​样:

FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();

        ft.add(R.id.dashboardArea, CalendarFragment);
        ft.commit(); 

答案 1 :(得分:0)

问题是您要替换R.id.dashboardArea(位于CalendarView片段布局中),但您需要替换View中的MainActivity片段。我为此示例调用了content_container

if(fragment !=null)
    {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();

        ft.replace(R.id.content_container, fragment);
        ft.commit();
    }

例:
我使用DrawerLayout,但您可以使用任何布局。这是我的MainActivity布局。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <RelativeLayout
    android:id="@+id/content_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    </RelativeLayout>


    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer"/>

</android.support.v4.widget.DrawerLayout>

当我想换掉我的片段时,我只是得到了我希望看到的片段对象并替换了容器 - 在本例中是“content_container”。