【问题标题】:Blank screen when trying to implement MVVM / ViewModel and data binding architecture尝试实现 MVVM / ViewModel 和数据绑定架构时出现空白屏幕
【发布时间】:2018-11-29 12:16:32
【问题描述】:

我正在尝试在我的 Android Java 应用程序中实现 Model-View-ViewModel 架构模式和 XML 数据绑定。就我所见,代码似乎都是正确的,但是当我运行应用程序时,我得到了一个空白屏幕(甚至没有显示底部导航视图)。我正在使用 ViewModel 来存储/持久化名为 AllTimeBestFragment 的片段的数据。具体来说,AllTimeBestFragment 是 Viewpager-tabLayout 中的三个片段之一。 希望能深入了解我做错了什么!

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();
    }
}

fragment_all_time_best.xml

<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" />

...

...

...

Gradle

implementation "android.arch.lifecycle:extensions:1.1.1"
    implementation "android.arch.lifecycle:viewmodel:1.1.1"

【问题讨论】:

    标签: android-fragments mvvm android-view viewmodel android-databinding


    【解决方案1】:

    我在这里找到了答案:How to use data-binding with Fragment 我正在绑定数据,就好像我在处理一个活动一样——我需要将它绑定到一个片段中。诀窍是使用 DataBindingUtil.inflate 而不是 DataBindingUtil.setContentView。下面是正确的代码sn-p:

    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);
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2011-12-06
      • 1970-01-01
      • 2011-03-15
      • 2011-03-23
      • 2015-06-21
      • 1970-01-01
      • 2021-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多