0

I'm relatively new to android programming and I've encountered problem to do with API 21. This code below was working perfectly. But when I updated to API 21, setSelectedNavigationItem and addTab are deprecated. I've read a fair amount about the problem and seen a good few examples, but I still can get my head around it. Any Help would be very much appreciated. Thanks

JAVA CODE:

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

ActionBar actionBar;
ViewPager viewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    viewPager = (ViewPager) findViewById(R.id.pager);
    viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    });
    actionBar = getActionBar();
    assert actionBar != null;
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    ActionBar.Tab tab1 = actionBar.newTab();
    tab1.setText("TAB1");
    tab1.setTabListener(this);

    ActionBar.Tab tab2 = actionBar.newTab();
    tab2.setText("TAB2");
    tab2.setTabListener(this);

    actionBar.addTab(tab1);
    actionBar.addTab(tab2);
}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
    viewPager.setCurrentItem(tab.getPosition());
}

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

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {
    Fragment fragment = null;
    if (position == 0) {
        fragment = new FragmentA();
    }
    if (position == 1) {
        fragment = new FragmentB();
    }
    return fragment;
}

@Override
public int getCount() {
    return 2;
}

One of my Fragments:

public class FragmentB extends Fragment {

public FragmentB() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_b, container, false);
}

}

ouflak
  • 2,458
  • 10
  • 44
  • 49
Darragh O'Flaherty
  • 995
  • 3
  • 13
  • 30

0 Answers0