YJ_C
YJ_C

Reputation: 21

ActionBar.Tablistener deprecated

I'm trying to build a swipe tabs view by using fragment activity and actionbar.tablistener. I have referenced other people tutorial and do it, however, it keep on deprecated and do not successful run it. Then I changed to AppComBatActivity and it do not works, show viewgroup error which I don't know why. THen I changed to ActionBarActivity, it worked finally, but no matter how I edit my tab activity, in java class and xml too, it does not show anything. I have actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS) deprecated as well.

I have attached my code as following...

fragmentmenu.class

public class FragmentMenu extends FragmentActivity implements ActionBar.TabListener {

    ActionBar actionBar;
    ViewPager viewPager;
    FragmentPageAdapter ft;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_menu);
        viewPager = (ViewPager)findViewById(R.id.pager);
        ft = new FragmentPageAdapter(getSupportFragmentManager());

        actionBar = getActionBar();
        viewPager.setAdapter(ft);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.addTab(actionBar.newTab().setText("Menu").setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText("Report").setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText("Setting").setTabListener(this));

    }


    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

    }

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

    }

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

    }
}
</code>
FragmentPagerAdapter 
<code>
public class FragmentPageAdapter extends FragmentPagerAdapter {
    public FragmentPageAdapter(FragmentManager fm) {
        super(fm);
    }


    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0:
                return new MyActivity();
            case 1:
                return new Report();
            case 2:
                return new Setting();
        }
        return null;
    }

    @Override
    public int getCount() {
        return 3;
    }
}
</code>
One of the fragment
<code>
public class Report extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.report, container, false);
    }
}

}} 

Please help!

Upvotes: 2

Views: 6130

Answers (1)

Mohammed Aouf Zouag
Mohammed Aouf Zouag

Reputation: 17132

ActionBar.Tablistener is deprecated. You need to use the TabLayout instead.

Check this tutorial for more infos : http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/

Upvotes: 4

Related Questions