我目前正在构建一个使用导航抽屉的安卓应用程序。当用户从抽屉中选择项目时,将创建一个与所选项目对应的片段。例如,如果用户从导航抽屉中选择联系人项,则 UI 应从仪表板更改为联系人布局。
我已成功创建片段,但是片段保留了仪表板中的日历视图。
以下是仪表板布局:
这是仪表板布局
这是用户从导航抽屉中选择计时器后的布局在此处输入图像描述
问题是当片段充气时,日历视图仍然存在。理想情况下,它只是此片段中可见的"计时器碎片"按钮。
这是我的主要活动代码:
//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>
问题是您要替换R.id.dashboardArea
(位于CalendarView
片段布局中(,但您需要替换MainActivity
中将保存片段的View
。对于这个例子,我称之为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"。
您应该将 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" />
日历片段.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();