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?
I wanna like this:
This is the current result. I want to put viewpager
in the red box.
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) {
}
});
You need to put view pager on Activity. Make a view pager Adapter. Connect your different fragment in your Adapter. After that you can set your Adapter on your Activity example is given below. O sorry we need to put your tab layout before vewPager. Connect your tab layout on your View Pager Adapter.
activity_main.xml
<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"
android:id="@+id/assignment_activitly"
>
<android.support.design.widget.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
android:background="@color/colorPrimary"
app:tabTextColor="@color/textPrimary"
app:tabSelectedTextColor="@color/colorAccent"
app:tabTextAppearance="@style/Tab"
/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
/>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
ViewPager viewpager;
TabLayout tabLayout;
public void onCreate(Bundle savedInstanceState) {
viewpager = findViewById(R.id.viewpager);
tabLayout = findViewById(R.id.tablayout);
super.onCreate(saveedInstanceState);
.........
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewpager.setViewPager(adapter);
tabLayout.setupWithViewPager(viewPager);
}
}
ViewPagerAdapter
public class ViewPagerAdapter extends FragmentPagerAdapter {
private String tabTitles [] = new String[] {"title first", "title Second"};
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
if (position == 0) {
return new YourFragmentFirst();
} else if (....){
return new YourFragmentSeconde();
}
........
}
@Override
public int getCount() {
return //return your number of Fragment;
}
@Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
}