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()方法我将其位置值作为参考,但在切换标签时,它的位置值没有改变。所以我尝试分配以下代码来参考,这也不适用于我。
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);
}
});
我想在这里使用位置值
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;
}
答案 0 :(得分:0)
如果要对不同的页面使用不同的菜单,可以在每个片段中使用以下代码
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.menu, menu);//menu for that fragment
super.onCreateOptionsMenu(menu, inflater);
}
并且在每个片段中,添加 setHasOptionsMenu(true);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
并使用像
这样的Fragment寻呼机适配器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];
}
}
在您的活动中,请使用此
mViewPager = (ViewPager) findViewById(R.id.container);
final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
mSectionsPagerAdapter = new MyAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mSectionsPagerAdapter);
tabLayout.setupWithViewPager(mViewPager);
使用 tabTextColor 和 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" />