Hello, I made in the code below a ViewPager with Tabs: In the Main code:
private TabLayout tabLayout;
private ViewPager viewPager;
SwipeRefreshLayout refresh;
ParseUser currentUser;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
viewPager = (ViewPager) findViewById(R.id.container);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
}
final int[] tabIcons = new int[]{
R.drawable.mainazul,
R.drawable.lupaazul,
R.drawable.menuazul
};
private void setupTabIcons() {
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new FragmentoMain());
adapter.addFragment(new PesquisaFrag());
adapter.addFragment(new Menu());
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment) {
mFragmentList.add(fragment);
}
@Override
public CharSequence getPageTitle(int position) {
return null;
}}
In my xml:
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
app:tabMode="fixed"
android:background="@color/branca"
android:layout_height="40dp"
android:layout_gravity="bottom|fill_vertical"
android:fillViewport="true" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
Tabs are only with icons, without text.
It's working fine, but I have the following questions:
2. The icons are always the same color. You can change them as the page is selected.
When I'm on page 1 or 2 and press the back button on the phone, exit. I would like you to return to page 0 instead of exiting the application.
I found that with "app: layout_behavior=" @ string / appbar_scrolling_view_behavior "" the toolbar (where the app name is) would shrink. But this is not happening. How to shrink the toolbar?
Thank you.