Java Android中数据绑定时替换片段时工具栏消失

Java Android中数据绑定时替换片段时工具栏消失,java,android,android-fragments,android-databinding,Java,Android,Android Fragments,Android Databinding,为了保持相同的导航菜单,我将用片段替换主要活动的布局。然而,我已经将数据绑定到fragment\u home.xml,现在工具栏不再显示,即使我仍然切换到预期的片段。经过一些调试,我相信这与HomeFragment.java中的setContentView有关 有什么想法吗 这是我的活动\u main.xml <?xml version="1.0" encoding="utf-8"?> <androidx.drawerlayout.widget.DrawerLayout

为了保持相同的导航菜单,我将用片段替换主要活动的布局。然而,我已经将数据绑定到
fragment\u home.xml
,现在工具栏不再显示,即使我仍然切换到预期的片段。经过一些调试,我相信这与
HomeFragment.java
中的setContentView有关

有什么想法吗

这是我的
活动\u main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity"
    tools:openDrawer="start">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="#000"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <FrameLayout
            android:id="@+id/frag_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/side_navbar"
        app:itemTextColor="@color/black"
        app:itemIconTint="@color/black"/>

</androidx.drawerlayout.widget.DrawerLayout>
<?xml version="1.0" encoding="utf-8"?>
<layout 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"
    tools:context=".HomeFragment">
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/viewCities"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:padding="4dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
</layout>
以及
fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity"
    tools:openDrawer="start">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="#000"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <FrameLayout
            android:id="@+id/frag_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/side_navbar"
        app:itemTextColor="@color/black"
        app:itemIconTint="@color/black"/>

</androidx.drawerlayout.widget.DrawerLayout>
<?xml version="1.0" encoding="utf-8"?>
<layout 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"
    tools:context=".HomeFragment">
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/viewCities"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:padding="4dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
</layout>

您不必从片段中再次设置内容视图。从使用数据绑定的
HomeFragnent
,只需执行以下操作

FragmentHomeBinding binding; // declare the binding variable

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    binding = DataBindingUtil
            .inflate(inflater, R.layout.fragment_home, container, false);

    /* FragmentHomeBinding activityMainBinding =
            DataBindingUtil.setContentView(getActivity(), R.layout.fragment_home); */ 

    RecyclerView recyclerView = binding.viewCities; // use the binding here
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    recyclerView.setHasFixedSize(true);

    cityViewModel = new ViewModelProvider(requireActivity()).get(CityViewModel.class);

    retrofitAdapter = new RetrofitAdapter();
    recyclerView.setAdapter(retrofitAdapter);

    return binding.getRoot();
}

您不必从片段中再次设置内容视图。从使用数据绑定的
HomeFragnent
,只需执行以下操作

FragmentHomeBinding binding; // declare the binding variable

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    binding = DataBindingUtil
            .inflate(inflater, R.layout.fragment_home, container, false);

    /* FragmentHomeBinding activityMainBinding =
            DataBindingUtil.setContentView(getActivity(), R.layout.fragment_home); */ 

    RecyclerView recyclerView = binding.viewCities; // use the binding here
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    recyclerView.setHasFixedSize(true);

    cityViewModel = new ViewModelProvider(requireActivity()).get(CityViewModel.class);

    retrofitAdapter = new RetrofitAdapter();
    recyclerView.setAdapter(retrofitAdapter);

    return binding.getRoot();
}

你能用截图更新吗?你能用截图更新吗?