我创建了一个带有底部导航栏
的活动.当我想切换到另一个片段时,我想要 tablayout
.而tablayout
可以控制viewpager
.我可以把viewpager
放到指定的fragment吗?
I created an activity with a bottom navigation bar
.
I want tablayout
when I want to switch to another fragment.
And tablayout
can control viewpager
. Can I put viewpager
to the specified fragment?
我想要这样:
这是当前结果.我想把 viewpager
放在红框里.
This is the current result. I want to put viewpager
in the red box.
试试这个来同步你的 BottomNavigationView
和 TabLayout
与 ViewPager
Try this to sync your BottomNavigationView
and TabLayout
with ViewPager
myBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_one) {
myViewPager.setCurrentItem(0);
}else if (id == R.id.action_two) {
myViewPager.setCurrentItem(1);
}else if (id == R.id.action_three) {
myViewPager.setCurrentItem(2);
}else if (id == R.id.action_four) {
myViewPager.setCurrentItem(3);
}
return false;
}
});
myViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if (position == 0) {
myBottomNavigationView.setSelectedItemId(R.id.action_one);
}else if (position == 1) {
myBottomNavigationView.setSelectedItemId(R.id.action_two);
}else if (position == 2) {
myBottomNavigationView.setSelectedItemId(R.id.action_three);
}else if (position == 3) {
myBottomNavigationView.setSelectedItemId(R.id.action_four);
}
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
这篇关于如何使用带有底部导航和 viewpager 的 tablayout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!