gpt4 book ai didi

android - 根据 ViewPager Android 显示菜单项

转载 作者:太空宇宙 更新时间:2023-11-03 11:56:46 25 4
gpt4 key购买 nike

我的 Android 应用程序中有一个带有 fragment 的 ViewPager。我想根据 ViewPager 的 fragment 显示操作栏菜单项。我已经阅读了一些关于此的问题,但我找不到正确的解决方案。我的 ViewPager 代码是这样的:

public class My_Activity extends ActionBarActivity {

private ViewPager mViewPager;
String idioma;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_main);

ActionBar actionBar = getSupportActionBar(); // || getActionBar();
actionBar.setIcon(getResources().getDrawable(R.drawable.navbar_logo));
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#CC3333")));

actionBar.setTitle("Hello");

mViewPager = (ViewPager) findViewById(R.id.viewpager);
PagerTabStrip pagerTabStrip = (PagerTabStrip) findViewById(R.id.pagerTabStrip);
pagerTabStrip.setTabIndicatorColor(getResources().getColor(R.color.blanco));

Title_Liga_Adapter titleAdapter = new Title_Liga_Adapter(getSupportFragmentManager());
mViewPager.setAdapter(titleAdapter);

mViewPager.setCurrentItem(0);

getSupportActionBar().setIcon(R.drawable.navbar_logo);

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (mViewPager.getCurrentItem() == 4) {
getMenuInflater().inflate(R.menu.menu_comunidad,
menu);
} else {

}
return true;
}

/* *
* Called when invalidateOptionsMenu() is triggered
*/
@Override
public boolean onPrepareOptionsMenu(Menu menu) {

menu.findItem(R.id.action_search)
.setVisible(true);

return super.onPrepareOptionsMenu(menu);
}

public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
this.finish();
return true;

case R.id.action_search:

return true;

default:
return super.onOptionsItemSelected(item);
}
}

}

我希望当 CurrentItem 为 4 时显示菜单项图标。

最佳答案

首先在onCreate方法中创建addOnPageChangeListener

ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ...

mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
invalidateOptionsMenu();
}

@Override
public void onPageSelected(int position) {

}

@Override
public void onPageScrollStateChanged(int state) {

}
});
}

然后你覆盖 onCreateOptionMenu()

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
if (mViewPager.getCurrentItem()==0){
menu.findItem(R.id.action_search).setVisible(true);
} else if(mViewPager.getCurrentItem()==1){
menu.findItem(R.id.action_search).setVisible(false);
} else if(mViewPager.getCurrentItem()==2){
// configure
} else if(mViewPager.getCurrentItem()==3){
// configure
}
return super.onCreateOptionsMenu(menu);
}

关于android - 根据 ViewPager Android 显示菜单项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28342181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com