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

Toolbar disappears while replacing fragment when Data Binding in Android

我正在用片段替换主要 activity 的布局,以保持相同的导航菜单。但是,我已经完成了到 fragment_home.xml 的数据绑定,现在工具栏不再显示,即使我仍然切换到预期的片段。经过一些调试,我相信它与HomeFragment.java中的setContentView有关。

有什么想法吗?

这是我的 activity_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>

用片段替换框架布局

getSupportFragmentManager().beginTransaction().replace(R.id.frag_container, new HomeFragment()).commit(); ```

fragment_home.xml

<?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>

最后 HomeFragment.java

public class HomeFragment extends Fragment {
    private CityViewModel cityViewModel;
    private RetrofitAdapter retrofitAdapter;

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

        View view = inflater.inflate(R.layout.fragment_home, container, false);

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

        RecyclerView recyclerView = activityMainBinding.viewCities;
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setHasFixedSize(true);

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

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

        return view;
    }
}

您不必从片段中再次设置内容视图。从 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();
}