Я впервые пытаюсь использовать привязку данных и mvvm. Я хочу реализовать ViewPager с TabLayout в своем приложении в основном действии. У меня есть 3 фрагмента, которые будут содержать некоторые детали. Я пытаюсь использовать привязку данных с MVVM, но когда я запускаю свое приложение, у меня возникает ошибка:
D:\Project files\movieswishlist\app\build\generated\ap_generated_sources\debug\out\com\enjoyapps\movieswishlist\DataBinderMapperImpl.java:9: error: cannot find symbol
import com.enjoyapps.movieswishlist.databinding.ActivityMainBindingImpl;
^
symbol: class ActivityMainBindingImpl
location: package com.enjoyapps.movieswishlist.databinding
Вот мое основное действие
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding activityMainBinding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
activityMainBinding.setLifecycleOwner(this);
activityMainBinding.setHandler(this);
activityMainBinding.setPager(this);
activityMainBinding.setManager(getSupportFragmentManager());
}
@BindingAdapter({"bind:handler"})
public static void bindViewPagerAdapter(final ViewPager view, final MainActivity activity) {
final TabAdapters adapter = new TabAdapters(view.getContext(), activity.getSupportFragmentManager());
view.setAdapter(adapter);
}
@BindingAdapter({"bind:pager"})
public static void bindViewPagerTabs(final TabLayout view, final ViewPager pagerView) {
view.setupWithViewPager(pagerView, true);
}
}
И мой TabsAdapter
public class TabAdapters extends FragmentPagerAdapter {
private static final int POPULAR = 0;
private static final int TOP_RATED = 1;
private static final int UP_COMING = 2;
private static final int[] TABS = new int[]{POPULAR, UP_COMING};
private Context mContext;
public TabAdapters(final Context context, final FragmentManager fm) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
mContext = context.getApplicationContext();
}
@Override
public Fragment getItem(int position) {
switch (TABS[position]) {
case POPULAR:
return new PopularMoviesFragment();
case TOP_RATED:
return new TopRatedMoviesFragment();
case UP_COMING:
return new UpComingMoviesFragment();
}
return null;
}
@Override
public int getCount() {
return TABS.length;
}
@Override
public CharSequence getPageTitle(int position) {
switch (TABS[position]) {
case POPULAR:
return "POPULAR";
case TOP_RATED:
return "TOP RATED";
case UP_COMING:
return "UP COMING";
}
return null;
}
}
И 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"
xmlns:bind="http://schemas.android.com/apk/res-auto">
<data>
<import type="android.view.View"/>
<variable
name="handler"
type="com.enjoyapps.movieswishlist.view.activities.MainActivity" />
<variable
name="pager"
type="com.enjoyapps.movieswishlist.view.activities.MainActivity" />
<variable
name="manager"
type="androidx.fragment.app.FragmentManager" />
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.activities.MainActivity">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?actionBarSize"
app:tabGravity="fill"
bind:handler="@{handler}"
app:tabIndicatorColor="@color/white"
app:tabIndicatorHeight="4dp"
app:tabBackground="@color/colorPrimary"
app:tabMode="fixed">
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager.widget.ViewPager
android:layout_below="@id/tabLayout"
android:id="@+id/viewPager"
bind:handler="@{pager}"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.viewpager.widget.ViewPager>
</RelativeLayout>
</layout>