ViewPagerでフラグメントを設定し、ViewPagerページを切り替えるだけです。
これを試してみてください:
viewPager = (ViewPager) findViewById(R.id.viewpager);
FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager());
adapter.addFragment(new ExplorerFragment(), "Fragment1", false);
adapter.addFragment(new NotificationFragment(), "Fragment2", false);
adapter.addFragment(new HistoryFragment(), "Fragment3", false);
adapter.addFragment(new AccountFragment(), "Fragment4", false);
viewPager.setAdapter(adapter);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.action_explorer:
viewPager.setCurrentItem(0);
final Menu bottomMenu = bottomNavigationView.getMenu();
final MenuItem targetMenuItem = bottomMenu.getItem(0);
targetMenuItem.setChecked(true);
break;
case R.id.action_notification:
viewPager.setCurrentItem(1);
final Menu bottomMenu1 = bottomNavigationView.getMenu();
final MenuItem targetMenuItem1 = bottomMenu1.getItem(1);
targetMenuItem1.setChecked(true);
break;
case R.id.action_history:
viewPager.setCurrentItem(2);
final Menu bottomMenu2 = bottomNavigationView.getMenu();
final MenuItem targetMenuItem2 = bottomMenu2.getItem(2);
targetMenuItem2.setChecked(true);
break;
case R.id.action_account:
viewPager.setCurrentItem(3);
final Menu bottomMenu4 = bottomNavigationView.getMenu();
final MenuItem targetMenuItem4 = bottomMenu4.getItem(3);
targetMenuItem4.setChecked(true);
break;
case R.id.action_search:
break;
}
return true;
}
});
EDIT1:
public class NonSwipeableViewPager extends ViewPager {
public NonSwipeableViewPager(Context context) {
super(context);
setMyScroller();
}
public NonSwipeableViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
setMyScroller();
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
// Never allow swiping to switch between pages
return false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// Never allow swiping to switch between pages
return false;
}
//down one is added for smooth scrolling
private void setMyScroller() {
try {
Class<?> viewpager = ViewPager.class;
Field scroller = viewpager.getDeclaredField("mScroller");
scroller.setAccessible(true);
scroller.set(this, new MyScroller(getContext()));
} catch (Exception e) {
e.printStackTrace();
}
}
public class MyScroller extends Scroller {
public MyScroller(Context context) {
super(context, new DecelerateInterpolator());
}
@Override
public void startScroll(int startX, int startY, int dx, int dy, int duration) {
super.startScroll(startX, startY, dx, dy, 350 /*1 secs*/);
}
}
}
してからXMLに:
<com.project.Components.NonSwipeableViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/bottom_navigation"/>
は私だけスワイプページを実装することにより、ViewPagerを知っている私の更新の答え –