Android fragments 尝试实现MVVM/ViewModel和数据绑定架构时出现空白屏幕

Android fragments 尝试实现MVVM/ViewModel和数据绑定架构时出现空白屏幕,android-fragments,mvvm,android-view,viewmodel,android-databinding,Android Fragments,Mvvm,Android View,Viewmodel,Android Databinding,我试图在我的Android Java应用程序中实现模型-视图-模型体系结构模式和XML数据绑定。就我所见,代码似乎都是正确的,但是当我运行应用程序时,我得到了一个空白屏幕(甚至没有显示bottomNavigationView)。我使用ViewModel存储/持久化名为AllTimeBestFragment的片段的数据。具体来说,AllTimeBestFragment是Viewpager表格布局中的三个片段之一。 如果能了解我做错了什么,我将不胜感激 AllTimeBestFragment pub

我试图在我的Android Java应用程序中实现模型-视图-模型体系结构模式和XML数据绑定。就我所见,代码似乎都是正确的,但是当我运行应用程序时,我得到了一个空白屏幕(甚至没有显示bottomNavigationView)。我使用ViewModel存储/持久化名为AllTimeBestFragment的片段的数据。具体来说,AllTimeBestFragment是Viewpager表格布局中的三个片段之一。 如果能了解我做错了什么,我将不胜感激

AllTimeBestFragment

public class AllTimeBestFragment extends Fragment {
    private ImageViewCallback imageViewCallback;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d(Constants.SkiCompanionDebug, "AllTimeBestFragment");

        bindToViewModel();

        return inflater.inflate(R.layout.fragment_all_time_best, container, false);
    }

    //imageview callback
    @Override
    public void setUserVisibleHint(boolean visible) {
        super.setUserVisibleHint(visible);

        imageViewCallback.setImage(R.drawable.all_time_best);
    }

    public void addCallback(ImageViewCallback imageViewCallback) {
        this.imageViewCallback = imageViewCallback;
    }

    //binding the view (fragment) to the asoociated viewModel
    public void bindToViewModel(){
        FragmentAllTimeBestBinding binding = DataBindingUtil.setContentView(getActivity(), R.layout.fragment_all_time_best);
        AllTimeBestViewModel viewModel = ViewModelProviders.of(this).get(AllTimeBestViewModel.class);

        AllTimeStats allTimeStats = BCCDatabase.getInstance(getActivity()).getAllTimeStats();
        viewModel.init(allTimeStats);
        binding.setAllTimeBestViewModel(viewModel);
    }

}
AllTimeBestViewModel

public class AllTimeBestViewModel extends ViewModel {
        private AllTimeStats allTimeStats;

    public void init(AllTimeStats allTimeStats) {
        this.allTimeStats = allTimeStats;
    }

    public String getDurationTotal(){
        return ""+allTimeStats.getDurationTotal();
    }
}
<data>

    <variable
        name="allTimeBestViewModel"
        type="skicompanion.skicompanion.viewmodels.AllTimeBestViewModel" />
</data>

<ScrollView 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/track_scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout 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="wrap_content"
        android:orientation="vertical"
        tools:context="skicompanion.skicompanion.fragments.AllTimeBestFragment">

        <android.support.v7.widget.CardView
            android:id="@+id/duration_cardview"
            android:layout_width="match_parent"
            android:layout_height="180dp"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">

            <android.support.constraint.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:id="@+id/duration_total"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@{allTimeBestViewModel.durationTotal}"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    app:layout_constraintEnd_toEndOf="@+id/duration_total_tv"
                    app:layout_constraintStart_toStartOf="@+id/imageView2"
                    app:layout_constraintTop_toBottomOf="@+id/duration_total_tv" />

...

...

...
fragment\u all\u time\u best.xml

public class AllTimeBestViewModel extends ViewModel {
        private AllTimeStats allTimeStats;

    public void init(AllTimeStats allTimeStats) {
        this.allTimeStats = allTimeStats;
    }

    public String getDurationTotal(){
        return ""+allTimeStats.getDurationTotal();
    }
}
<data>

    <variable
        name="allTimeBestViewModel"
        type="skicompanion.skicompanion.viewmodels.AllTimeBestViewModel" />
</data>

<ScrollView 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/track_scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout 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="wrap_content"
        android:orientation="vertical"
        tools:context="skicompanion.skicompanion.fragments.AllTimeBestFragment">

        <android.support.v7.widget.CardView
            android:id="@+id/duration_cardview"
            android:layout_width="match_parent"
            android:layout_height="180dp"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">

            <android.support.constraint.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:id="@+id/duration_total"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@{allTimeBestViewModel.durationTotal}"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    app:layout_constraintEnd_toEndOf="@+id/duration_total_tv"
                    app:layout_constraintStart_toStartOf="@+id/imageView2"
                    app:layout_constraintTop_toBottomOf="@+id/duration_total_tv" />

...

...

...

我在这里找到了答案: 我在绑定数据,就像我在处理一个活动一样——我需要将它绑定为一个片段。诀窍是使用DataBindingUtil.inflate而不是DataBindingUtil.setContentView。下面是正确的代码段:

public class AllTimeBestFragment extends Fragment {
    private ImageViewCallback imageViewCallback;
    private FragmentAllTimeBestBinding binding;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d(Constants.SkiCompanionDebug, "AllTimeBestFragment");

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

        bindToViewModel();

        return binding.getRoot();
    }

    //imageview callback
    @Override
    public void setUserVisibleHint(boolean visible) {
        super.setUserVisibleHint(visible);

        imageViewCallback.setImage(R.drawable.all_time_best);
    }

    public void addCallback(ImageViewCallback imageViewCallback) {
        this.imageViewCallback = imageViewCallback;
    }

    //binding the view (fragment) to the asoociated viewModel
    public void bindToViewModel(){
        AllTimeBestViewModel viewModel = ViewModelProviders.of(this).get(AllTimeBestViewModel.class);

        AllTimeStats allTimeStats = BCCDatabase.getInstance(getActivity()).getAllTimeStats();
        viewModel.init(allTimeStats);
        binding.setAllTimeBestViewModel(viewModel);
    }

}