[英]How to have different icon button in action bar swipe tab?
I have 4 swipe tab
in my app. 我的应用程序中有4个
swipe tab
。 I have successfully to create a home icon on action bar
. 我已经成功在
action bar
上创建了一个主页图标。 Now I wanted to have a pen icon beside the home icon when comes to work details tab. 现在,当要进入“工作详细信息”选项卡时,我想在主页图标旁边有一个笔形图标。 How can I achieve this ?
我该如何实现? Thanks.
谢谢。
TabsFragmentAdapter 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;
}
}
Activity B 活动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 tab_1_菜单
<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>
Should I add the icon in Edit_WorkDetails fragment
or inside Activity B ? 我应该在Edit_WorkDetails
fragment
还是在活动B中添加图标?
First, add the pen icon with visible=false
in your menu. 首先,在菜单中添加带有
visible=false
的钢笔图标。
Then, create a global MenuItem
variable. 然后,创建一个全局
MenuItem
变量。
MenuItem mPenIcon;
and in onPrepareOptionMenu
method 并在
onPrepareOptionMenu
方法中
public boolean onPrepareOptionsMenu(Menu menu)
{
mPenIcon = menu.findItem(R.id.penicon);
return true;
}
when the tab you want the pen icon to appear is selected, 当您选择要显示笔形图标的标签时,
@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);
}
when the tab is unselected, 当未选择标签时,
@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);
}
Hope this helps :) 希望这可以帮助 :)
Edited 已编辑
The first method I posted isn't the actual way of implementing that. 我发布的第一种方法不是实际的实现方法。
Here's the actual method. 这是实际的方法。
Put hasOptioinMenu(true)
in onCreateView
of Edit_WorkFragment
把
hasOptioinMenu(true)
在onCreateView
的Edit_WorkFragment
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
//other codes
setHasOptionMenu(true);
//other codes
}
and create menu for that fragment 并为该片段创建菜单
@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) {
}
As I got your point, you want to show specific menu option for specific fragment only. 如您所知,您只想显示特定片段的特定菜单选项。 Here I have one solution for you.
在这里,我为您提供一种解决方案。
1.Add your menu item in menu xml file ex. 1.将菜单项添加到菜单xml文件ex中。 action_edit
action_edit
2.Add this in onCreateView() method of each fragment. 2.将此添加到每个片段的onCreateView()方法中。
setHasOptionsMenu(true);
3.Than override onPrepareOptionsMenu() in each fragment like this: 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);
}
}
Hope this will help you :) 希望这个能对您有所帮助 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.