我的应用中有4个swipe tab
。我已成功在action bar
上创建主页图标。现在,我想在工作详细信息选项卡的主页图标旁边有一个笔图标。我怎样才能做到这一点?感谢。
TabsFragmentAdapter
public class TabsFragmentPagerAdapter extends FragmentPagerAdapter {
public TabsFragmentPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int index) {
// TODO Auto-generated method stub
if(index == 0) {
Fragment fragment=new EditInformation();
Bundle bundle = new Bundle();
bundle.putString("ID", Edit.ID);
fragment.setArguments(bundle);
return fragment;
}
if(index == 1) {
Fragment fragment = new Edit_WorkForce();
Bundle bundle = new Bundle();
bundle.putString("ID", Edit.ID);
fragment.setArguments(bundle);
return fragment;
}
if(index == 2) {
Fragment fragment = new Edit_WorkDetails();
Bundle bundle = new Bundle();
bundle.putString("ID", Edit.ID);
fragment.setArguments(bundle);
return fragment;
}
if(index == 3) {
Fragment fragment = new Edit_Staff_Benefit_ListView();
Bundle bundle = new Bundle();
bundle.putString("ID", Edit.ID);
fragment.setArguments(bundle);
return fragment;
}
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 4;
}
}
活动B
public class ActivityB extends ActionBarActivity implements ActionBar.TabListener {
private ViewPager viewPager;
private ActionBar actionBar;
private TabsFragmentPagerAdapter tabsAdapter;
private String[] item = new String[]{"Information","Work Force","Work Details","Staff Benefit"};
private String id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
// id=getIntent().getExtras().getString("ID");
viewPager = (ViewPager) findViewById(R.id.viewPager);
tabsAdapter = new TabsFragmentPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(tabsAdapter);
actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for(int i=0; i<4; i++){
actionBar.addTab(actionBar.newTab().setText(item[i]).setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int arg) {
// TODO Auto-generated method stub
actionBar.setSelectedNavigationItem(arg);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.tab_1_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.homePage: // create homePage icon here ?
Intent i=new Intent(getApplicationContext(),HomePage.class);
startActivity(i);
}
return super.onOptionsItemSelected(item);
}
@Override
public void onTabReselected(ActionBar.Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction arg1) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
}
tab_1_menu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:icon="@mipmap/home"
android:id="@+id/homePage"
android:orderInCategory="100"
android:title="Tab"
app:showAsAction="always" />
</menu>
我应该在 Edit_WorkDetails fragment
中还是在活动B 中添加图标?
答案 0 :(得分:1)
首先,在菜单中添加带有visible=false
的笔图标。
然后,创建一个全局MenuItem
变量。
MenuItem mPenIcon;
和onPrepareOptionMenu
方法
public boolean onPrepareOptionsMenu(Menu menu)
{
mPenIcon = menu.findItem(R.id.penicon);
return true;
}
选择要显示笔图标的选项卡时,
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction arg1) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(tab.getPosition());
if(tab.getPosition()==1)//Replace 1 with your tab position
pen.setVisible(true);
}
取消选中标签时,
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction arg1) {
// TODO Auto-generated method stub
if(tab.getPosition()==1)//Replace 1 with your tab position
pen.setVisible(false);
}
希望这会有所帮助:)
被修改
我发布的第一种方法并不是实现它的实际方法。
这是实际的方法。
将hasOptioinMenu(true)
放在onCreateView
Edit_WorkFragment
中
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
//other codes
setHasOptionMenu(true);
//other codes
}
并为该片段创建菜单
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.edit_work, menu);
//You should create new menu for Edit_work fragment with both home and pen icons
super.onCreateOptionsMenu(menu, inflater);
}
@Override public boolean onOptionsItemSelected(MenuItem item) {
}
答案 1 :(得分:1)
当我明白您的意思时,您只想显示特定片段的特定菜单选项。 在这里,我有一个解决方案。
1.在菜单xml文件中添加菜单项。 action_edit
2.在每个片段的onCreateView()方法中添加。
setHasOptionsMenu(true);
3.在每个片段中覆盖onPrepareOptionsMenu(),如下所示:
@Override
public void onPrepareOptionsMenu(Menu menu)
{
super.onPrepareOptionsMenu(menu);
MenuItem item = menu.findItem(R.id.action_edit);
if(item != null) {
item.setVisible(true);
//in fragment where you want to hide this menu item
//item.setVisible(false);
}
}
希望这会对你有所帮助:)。