ViewPager не работает, чтобы провести фрагмент.Я использую действие Drawer, затем добавил BottomNavigationView
, и в основном контенте я добавил FrameLayout
, чтобы он был контейнером фрагмента и располагался ниже TabLayout
и выше BottomNaigationView
.Затем я создал класс с именем PagerAdapter extends FragmnetPagerAdapter
.
. Я создал конструктор и реализовал методы getItem()
и getCount()
, затем в MainActivity
я инициализировал все представления по его идентификатору, но при запускеприложение все работает, за исключением размах среди фрагмента с Viewpager
.Что я должен сделать больше, чтобы это заработало.
Это мой PagerAdapter
класс:
public class PageAdapter extends FragmentPagerAdapter {
private int numberOfTabs;
public PageAdapter(FragmentManager fm, int numberOfTabs) {
super(fm);
this.numberOfTabs = numberOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position){
case(0) : return new HomeFragment();
case(1) : return new CouponFragment();
default : return null;
}
}
@Override
public int getCount() {
return numberOfTabs;
}
}
Вот мой xml-файл:
<android.support.design.widget.CoordinatorLayout 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="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
Панель инструментовс TextView, чтобы установить заголовок
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" >
<TextView
android:id="@+id/my_title"
android:text="Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_gravity="center"
android:textColor="@color/white"
/>
</android.support.v7.widget.Toolbar>
Здесь TabLayout добавлен с тремя вкладками Домой, Купоны и Уведомления.Я хочу, чтобы ViewPager проводил между тремя вкладками
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar"
app:tabGravity="fill"
app:tabIndicatorColor="@color/color"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/color"
app:tabTextColor="@color/fadeWhite">
<android.support.design.widget.TabItem
android:id="@+id/tab_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home"
style="@style/MyCustomTabLayout"
/>
<android.support.design.widget.TabItem
android:id="@+id/tab_coupons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Coupons"
style="@style/MyCustomTabLayout"
/>
<android.support.design.widget.TabItem
android:id="@+id/tab_notifiactions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notifiactions"
style="@style/MyCustomTabLayout"
/>
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@id/navBar"
app:layout_constraintTop_toTopOf="parent"/>
<include layout="@layout/content_main" />
Вот это MainActivity
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
appBarLayout = (AppBarLayout) findViewById(R.id.app_bar);
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tab_home = (TabItem) findViewById(R.id.tab_home);
tab_coupons = (TabItem) findViewById(R.id.tab_coupons);
tab_noti = (TabItem) findViewById(R.id.tab_notifiactions);
viewPager = (ViewPager) findViewById(R.id.view_pager);
adapter = new PageAdapter(getSupportFragmentManager(),tabLayout.getTabGravity());
viewPager.setAdapter(adapter);
//setupToolBar
toolbar = (Toolbar) findViewById(R.id.toolbar);
my_title = (TextView) findViewById(R.id.my_title);
setSupportActionBar(toolbar);
my_title.setText(toolbar.getTitle());
getSupportActionBar().setDisplayShowTitleEnabled(false);