0

while switching tabs in tablayout position value tab is not changing.see the code below.

tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            tab.getIcon().setColorFilter(Color.parseColor("#ffffff"), PorterDuff.Mode.SRC_IN);
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
            tab.getIcon().setColorFilter(Color.parseColor("#40FFFFFF"), PorterDuff.Mode.SRC_IN);
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

onTabSelected() method i took its position value as reference but while switching tabs it's position value was not changing. so i tried assign below code to take reference this also not worked with me.

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            tabposition=position;
        }

        @Override
        public void onPageScrollStateChanged(int state) {

            // Hide the keyboard.
            ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE))
                    .hideSoftInputFromWindow(viewPager.getWindowToken(), 0);


        }
    });

i want to use position value here

 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);
    menu.clear();
    Log.d("position", "--------- "+tabposition);
    if (tabposition == 0) {
        getMenuInflater().inflate(R.menu.main, menu);  //  menu for alert.
    } else if (tabposition ==1){
        getMenuInflater().inflate(R.menu.transactionmenu, menu);  // menu for transactiontab
    }else {
        getMenuInflater().inflate(R.menu.notificationsmenu, menu);  // menu for notificationtab
    }
    return super.onPrepareOptionsMenu(menu);
    //return true;
}
vinay
  • 149
  • 7

1 Answers1

0

If you want to use different menu for different page , You can use the following code in each fragment

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    menu.clear();
    inflater.inflate(R.menu.menu, menu);//menu for that fragment
    super.onCreateOptionsMenu(menu, inflater);
}

And also in each fragment , add setHasOptionsMenu(true);

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

And use Fragment pager adapter like

public class MyAdapter extends FragmentPagerAdapter {
    static int NUM_ITEMS = 3;
    final String[] a={"one" ,"two" ,"three"};
    public MyAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);

    }
    @Override
    public int getCount() {
        return NUM_ITEMS;
    }
    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new Fragmentone();
            case 1:
                return new Fragmenttwo();
            case 2:
                return new Fragmentthree();
            default:
                return null;
        }
    }
    @Override
    public CharSequence getPageTitle(int position) {
        return a[position];
    }
}

And in your activity , use this

mViewPager = (ViewPager) findViewById(R.id.container);
        final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        mSectionsPagerAdapter = new MyAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mSectionsPagerAdapter);
        tabLayout.setupWithViewPager(mViewPager);

Change tab text color using tabTextColor and tabSelectedTextColor,

<android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            app:tabIndicatorColor="#fff"
            app:tabTextColor="#80ffffff"
            app:tabSelectedTextColor="#fff"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
Jyoti JK
  • 2,141
  • 1
  • 17
  • 40