Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Create Android Tabbed style with Page Swipe View

I want an example of tab like this

google play tab

I searched but just got this.

viewpageindicator

I couldn't use this source. Can someone tell me another example of tab with sliding option.
I think the viewpageindicator is not the same as google plays tabs.

cause when i'm scrolling in google plays page the line below tabs moves while scrolling, But in viewpageindicator it's not.

Thank you

like image 709
Person Avatar asked Sep 10 '25 23:09

Person


2 Answers

First you need to create an xml layout file with viewpager android.support.v4.view.ViewPager

Then inflate this layout in your main activity, make sure your activity implements

android.support.v7.app.ActionBar.TabListener Lets say, you want to have 4 tabs, then create

4 different fragments. Create a common adapter to facilitate these fragments based on the

tabs you select.

like image 57
Naruto Avatar answered Sep 12 '25 11:09

Naruto


When I had implemented something similar, I had done it in a nicer way, in the sense of classes. I had created the adapter in a package called working.

Below is the code:

public class TabsPagerAdapter extends FragmentPagerAdapter {

    public TabsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int index) {

        switch (index) {
        case 0:
          return new WorkingFragment();
        //You can add as many fragments as you wish here by adding the cases and calling the different fragments.
        }      
        return null;
    }

    @Override
    public int getCount() {
        // get item count - equal to number of tabs. 4 is only the number of fragments I had used.
        return 4;
    }
}

You then need to create a new class called WorkingFragment in the example I am giving. For the sake of clean coding, I created this class in the main package.

Below is the code for the fragment:

public class NewEventFragment extends Fragment {

    View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    //reference the xml file containing the view with all the code here.
    }
}

In the MainActivity, you then need to make the functionality to change form one tab to another. In my case I had included this in the main package.

Below is the code for the MainActivity class:

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

    private ViewPager ViewPager;
    private TabsPagerAdapter SectionsPagerAdapter;
    private ActionBar actionBar;

    //Include the name for the tabs in this array. Make sure the number of elements in this string matches the number of views you will have in your app. 
    private String[] tabs = {};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

     // Initialisation
        ViewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        SectionsPagerAdapter = new TabsPagerAdapter(getSupportFragmentManager());

        ViewPager.setAdapter(SectionsPagerAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

     // Adding Tabs
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
        }

        ViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }

    @Override
    public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {

    }

    @Override
    public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
        ViewPager.setCurrentItem(tab.getPosition());

    }

    @Override
    public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
    }
} 

Hope this has helped you achieve the necessary funtionality for your app :)

like image 33
Michele La Ferla Avatar answered Sep 12 '25 13:09

Michele La Ferla