Swipe Viewpager Tabs - alignment, icone color change, back to page 0 and shrink to Toolbar

Asked

Viewed 403 times

0

Hello, I made in the code below a Viewpager with Tabs: In the code of Main:

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 icon-only, no text.

It’s working fine, but I’m having the following questions:

  1. When the phone is vertical, the icons are well distributed on the screen. When it is horizontal they align in the center. How to change this?

2.Icons are always the same color. You can change them as the page is selected.

  1. When I’m on page 1 or 2 and I press the back button of the mobile phone, it leaves. I’d like it to go back to page 0, instead of leaving the application.

  2. 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 Toolbar?

Thank you.

  • You will need to rewrite your code: 1- here, you need to create a layout to display the Activity in Landscape mode, for that, create a layout-land folder and copy the.xml layout in that folder, it’s basically the same layout. 2- Here, Voce needs to create a different image to set as the icon for the selected tab and change the icons programmatically. 3- in this, Voce needs to detect the back key of the device, and instantiate the method @override ... onKeyDown(int keyCode, KeyEvent event) ... and instead of closing the app, switch to another tab. 4- Need to explain better what Voce wants.

  • @Armandomarquesnephew, hello Armando, can explain me better how to get back to page 1. I do not understand.

  • Voce must have a variable set with the tab counter in which it is in focus, then instantiates the Onkeydown method of its Activity and checks if the user pressed "back", if that and the variable with the tab nr in focus is > 0 and <= nrTabs, decreases the variable and commands the tabmanager to focus one of its tabs with this new value, and if it is zero and you want q the program to close, tracks the flow of the method and exits the program or does what you want with your screen.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.