How to change tabs when scrolling on screen?

1

I have an Activity that has 2 tabs and I wanted to know how to change tab not only by clicking on one of them but also when sliding (left and right) on the screen.

My activity:

public class MainActivity extends Activity {
    private Fragment1 frag1;
    private Fragment2 frag2;
    ActionBar bar;
    Tab tab1;
    Tab tab1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bar = getActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        frag1 = new Fragment1();
        frag2 = new Fragment2();

        tab1 = bar.newTab();
        tab1.setText("Tab1");
        tab1.setTabListener(new ConfigTab(frag1, R.id.frag1));
        bar.addTab(tab1);

        tab2 = bar.newTab();
        tab2.setText("Tab2");
        tab2.setTabListener(new ConfigTab(frag2, R.id.frag1));
        bar.addTab(tab2);

    }
}

And the configTab class:

public class ConfigTab implements TabListener {
    private Fragment fragment;
    private int placeholder;

    public ConfigTab(Fragment fragment, int placeholder) {
        this.fragment = fragment;
        this.placeholder = placeholder;
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        ft.add(placeholder, fragment);
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        ft.remove(fragment);
    }
}

Error print in mainActivity, highlighted below:

Printerroroccurred:

    
asked by anonymous 21.10.2014 / 22:33

1 answer

4

When I implemented this, I did the following. In the layout of the activity, I put it like this:

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</android.support.v4.view.ViewPager>

So the only View element in my activity is the ViewPager. To initialize the ViewPager, in the onCreate method:

ViewPager viewPager = (ViewPager) findViewById(R.id.pager);

For this viewPager assigns an adapter, in this case a class I called TabsPagerAdapter:

package br.com.exemplo.application.adapters;

import android.app.Fragment;
import android.app.FragmentManager;
import android.support.v13.app.FragmentPagerAdapter;
import br.com.exemplo.presentation.views.fragments.PrimeiroFragment;
import br.com.exemplo.presentation.views.fragments.SegundoFragment;

public class TabsPagerAdapter extends FragmentPagerAdapter {
public PrimeiroFragment primeiroFragment;
public SegundoFragment segundoFragment;


public TabsPagerAdapter(FragmentManager fm) {
    super(fm);
    primeiroFragment = new PrimeiroFragment();
    segundoFragment = new SegundoFragment();
}

@Override
public Fragment getItem(int index) {

    switch (index) {
    case 0:
        // Seu primeiro fragment
        return primeiroFragment;
    case 1:
        // Seu segundo fragment
        return segundoFragment;
    default:
        return null;
    }

}

@Override
public int getCount() {
    // get item count - quantidade de tabs
    return 2;
}

}

Returning to Activity, assign the adapter to the viewPager, create the objects Tab and assign them to ActionBar :

viewPager.setAdapter(mAdapter);
ActionBar actionBar = getActionBar();
String[] tabs = {"Lista Publicações", "Nova Publicação"};
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        
// Adicione aqui as tabs:
for (String tab_name : tabs) {
    actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));

}

And also add OnPageChangeListener to your viewPager:

        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) {
        }
    });

Well, I now believe that with viewPager works. This OnPageChangeListener may be useful to you, but it is not required. For TabListeners I implemented the ActionBar.TabListener interface in my activity:

...
public class MainActivity extends Activity implements ActionBar.TabListener{
...

And with this are created the methods you use in ConfigTab in activity. (If you prefer, assign ConfigTab as TabListener for each Tab object and do not implement the ActionBar.TabListener interface):

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // on tab selected
    // show respected fragment view
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
    
21.10.2014 / 23:39