Android MVVM - ViewPager什么都不显示

时间:2018-01-16 21:48:30

标签: android mvvm data-binding android-viewpager android-livedata

一些背景 - 我正在尝试创建一个简单的应用程序,以了解有关mvvm,数据绑定,房间等的更多信息。该应用程序将有2个主要视图和设置,所以我决定使用导航抽屉是矫枉过正,并决定转而使用ViewPager。 不幸的是,我无法让ViewPager显示使用ViewModel的片段,即使它只是一个页面。我的代码:

适配器#

(只加一页)

public class MainSectionsAdapter extends FragmentPagerAdapter {

private static final int SUMMARY = 0;
private static final int ENTRIES = 1;
private static final int ENTRIES2 = 2;


private static final int[] TABS = new int[]{SUMMARY};

private Context mContext;

public MainSectionsAdapter(final Context context, final FragmentManager fm) {
    super(fm);
    mContext = context.getApplicationContext();
}

@Override
public Fragment getItem(int position) {
    switch (TABS[position]) {
        case SUMMARY:
            return MainScreenFragment.newInstance();
        case ENTRIES:
            return EntriesFragment.newInstance();
        case ENTRIES2:
            return PlusOneFragment.newInstance();
    }
    return null;
}

@Override
public int getCount() {
    return TABS.length;
}

@Override
public CharSequence getPageTitle(int position) {
    switch (TABS[position]) {
        case SUMMARY:
            return mContext.getResources().getString(R.string.summary);
        case ENTRIES:
            return mContext.getResources().getString(R.string.data);
        case ENTRIES2:
            return mContext.getResources().getString(R.string.data);
    }
    return null;
}

}

活动

public class MainScreenActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActivityMainScreenBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main_screen);
    binding.setHandler(this);
    binding.setManager(getSupportFragmentManager());
}

@BindingAdapter({"bind:handler"})
public static void bindViewPagerAdapter(final ViewPager view, final MainScreenActivity activity) {
    final MainSectionsAdapter adapter = new MainSectionsAdapter(view.getContext(), activity.getSupportFragmentManager());
    view.setAdapter(adapter);
}

@BindingAdapter({"bind:pager"})
public static void bindViewPagerTabs(final TabLayout view, final ViewPager pagerView) {
    view.setupWithViewPager(pagerView, true);
}

}

片段

public class MainScreenFragment extends Fragment {

private MainScreenViewModel mainScreenVievModel;
private FragmentMainScreenBinding fragmentMainScreenBinding;

public MainScreenFragment() {
    // Required empty public constructor
}

public static MainScreenFragment newInstance() {
    return new MainScreenFragment();
}

@Override
public void onResume() {
    super.onResume();
    mainScreenVievModel.start();
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    fragmentMainScreenBinding = FragmentMainScreenBinding.inflate(inflater, container, false);

    fragmentMainScreenBinding.setViewmodel(mainScreenVievModel);

    setHasOptionsMenu(true);

    mainScreenVievModel = ViewModelProviders.of(this).get(MainScreenViewModel.class);

    return fragmentMainScreenBinding.getRoot();
}

活动布局

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fab="http://schemas.android.com/apk/res-auto">

<data>

    <import type="android.view.View" />

    <variable
        name="handler"
        type="horatii.realtdee.view.MainScreenActivity" />

    <variable
        name="manager"
        type="android.support.v4.app.FragmentManager" />
</data>

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:animateLayoutChanges="true"
        app:title="@string/app_name"
        app:titleMarginStart="8dp" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:pager="@{(pager)}">
    </android.support.design.widget.TabLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:handler="@{handler}" />
    </LinearLayout>

    <com.github.clans.fab.FloatingActionMenu
        android:id="@+id/fab_menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_marginBottom="8dp"
        android:layout_marginRight="8dp"
        android:src="@drawable/ic_add_black_24dp">

        <com.github.clans.fab.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_assessment_black_24dp"
            fab:fab_size="mini"
            fab:fab_label="Both" />
    </com.github.clans.fab.FloatingActionMenu>

</android.support.design.widget.CoordinatorLayout>

片段布局

<layout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>

    <import type="android.view.View" />

    <variable
        name="viewmodel"
        type="horatii.realtdee.vievmodel.MainScreenViewModel" />

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

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:text="@{viewmodel.testText}"/>
    </android.support.constraint.ConstraintLayout>
</RelativeLayout>

Fragment ViewModel

public class MainScreenViewModel extends AndroidViewModel {
public final ObservableField<String> testText = new ObservableField<>();
private final SnackbarMessage mSnackbarText = new SnackbarMessage();
private final Context mContext; // To avoid leaks, this must be an Application Context.

public MainScreenViewModel(
        Application context) {
    super(context);
    testText.set("cipson");
    mContext = context.getApplicationContext(); // Force use of Application Context.
}

}

应用程序运行正常,但页面似乎总是空的,我感觉像是在遗漏某些东西。 preview

2 个答案:

答案 0 :(得分:0)

在片段类中:

fragmentMainScreenBinding = FragmentMainScreenBinding.inflate(inflater, container, false);

替换此行:

fragmentMainScreenBinding = DataBindingUtil.inflate(inflater, R.layout.<fragmentlayoutname>, container, false);

答案 1 :(得分:0)

您必须像这样在ViewPagerAdapter的binding.getRoot()方法中将View instantiateItem添加到您的容器中:

   container.addView(binding.getRoot());