viewPager 和数据绑定 android

viewPager and data binding android

我有一个带有视图寻呼机的 activity 并且我实现了 MVVM。
我无法设置查看寻呼机适配器。 我阅读了 stack Over Flow 中的所有 post,但我有错误。 我的代码的哪一部分是错误的? 我 post 编辑了我所有的代码。

This my xml codes

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:bind="http://schemas.android.com/tools">
<data>
    <variable
        name="main"
        type="ir.****.viewmodel.MainViewModel" />
</data>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar"/>

<androidx.viewpager.widget.ViewPager
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    adapter="@{main}" />

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:itemTextColor="@android:color/transparent"
    app:menu="@menu/main_menu" />

This is my viewModel codes

public class MainViewModel extends BaseObservable {
private  AppCompatActivity activity;

public MainViewModel(AppCompatActivity activity) {
    this.activity = activity;
}

@BindingAdapter("adapter")
public static void setViewPagerFragments(ViewPager pager , MainViewModel viewModel){
    pager.setAdapter(new MyFragmentAdapter(pager.getContext() , viewModel.activity.getSupportFragmentManager() ));
}
}

This is my mainActivity Codes

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    ActivityMainBinding mainBinding = DataBindingUtil.setContentView(this , R.layout.activity_main);
    MainViewModel viewModel = new MainViewModel(this);
    mainBinding.setMain(viewModel);
}
}

This is my fragment adapter codes

public class MyFragmentAdapter extends FragmentPagerAdapter {
private static final int TRADE = 0;

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

private Context mContext;

public MyFragmentAdapter(final Context context, final FragmentManager fm) {
    super(fm);
    mContext = context;
}

@Override
public Fragment getItem(int position) {
    switch (TABS[position]) {
        case TRADE:
            return TradeFragment.newInstance("first", "first");
    }
    return null;
}

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

@Override
public CharSequence getPageTitle(int position) {
    switch (TABS[position]) {
        case TRADE:
            return mContext.getResources().getString(R.string.trade);
    }
    return null;
}
}

and logcat message after crush

    java.lang.IllegalStateException: ViewPager with adapter ir.****.Adapter.MyFragmentAdapter@a0ef1f requires a view id

尝试在布局中为您的 ViewPager 添加一个 id:

<androidx.viewpager.widget.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    adapter="@{main}" />