Refresh on swiping tabs android

664 views Asked by At

I am using view pager and fragments. I have three swipable tabs. Each extend Fragment. I want them to refresh them on swipe. How to achieve this? Now when I swipe, the tab does not get refreshed.

public class Orders extends FragmentActivity implements
    ActionBar.TabListener {

private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Pending", "Completed", "Rejected" };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_orders);
    this.setTitle((Html.fromHtml("<font color=\"#ffffff\">" + "pharmeazy-mobile" + "</font>")));
    // Initilization
    viewPager = (ViewPager)findViewById(R.id.pager1);
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
    actionBar=getActionBar();
    getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#69acdf")));
    viewPager.setAdapter(mAdapter);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

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

    /**
     * on swiping the viewpager make respective tab selected
     * */
    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, 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) {
}

Please help

1

There are 1 answers

0
EE66 On

Dont use ActionBar. Take a look at TabLayout this is the new layout for ViewPager with Tabs that Google released in the last support update. It will solve your issue