不同片段的动作栏中的不同动作按钮

use*_*702 1 android android-fragments

我正在我的应用程序中实现自定义操作栏.在一个片段我也使用Tab导航.为此,我有一个父片段,我在其中发出Web请求和其他五个片段用于Tab导航.我想在Action Bar中为不同的Fragmnet及其监听器添加不同的Action Button.我只有一个Activity正在给我的自定义操作栏充气.

这是我的活动代码.

public class MainActivity extends ActionBarActivity {
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerListLeft;
    private ListView mDrawerListRight;

    private ActionBarDrawerToggle mDrawerToggle;

    private ArrayList<NavDrawerItem> navDrawerItemsLeft;
    private ArrayList<NavDrawerItem> navDrawerItemsRight;
    private NavDrawerListAdapter adapterLeftDrawer;
    private NavDrawerListAdapter adapterRightDrawer;

    public static ActionBar myCustomActionBar;

    ImageButton imgLeftMenu;
    ImageButton imgRightMenu;

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

        LayoutInflater inflator=(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflator.inflate(R.layout.header, null);

        getSupportActionBar().setHomeButtonEnabled(true);

        getSupportActionBar().setDisplayShowTitleEnabled(false);

        getSupportActionBar().setDisplayUseLogoEnabled(false);

        getSupportActionBar().setDisplayShowCustomEnabled(true);

        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#93b636")));

        getSupportActionBar().setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent)));

        getSupportActionBar().setCustomView(v);

        myCustomActionBar = getSupportActionBar();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是My Frgment Listner的代码

public class Shops extends Fragment implements ActionBar.TabListener {

    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
    // Tab titles
    private String[] tabs = new String[] {"Tab 1", "Tab 2", "Tab 3", "Tab 4"};
    public Shops(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_shop, container, false);

        viewPager = (ViewPager) rootView.findViewById(R.id.pager);
        actionBar = MainActivity.myCustomActionBar;
        mAdapter = new TabsPagerAdapter(getActivity().getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

        // Adding Tabs
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
        }

        /**
         * on swiping the viewpager make respective tab selected
         * */
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });

        return rootView;
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction arg1) {
        viewPager.setCurrentItem(tab.getPosition());        
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction arg1) {
        // TODO Auto-generated method stub

    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的Tab片段

public class ShopFragment extends Fragment {
    public ShopFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_home, container, false);
        return rootView;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.sort_button, menu);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的排序按钮XML是

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/edit_item"
        android:icon="@drawable/icon_sort"
        android:orderInCategory="10"
        android:showAsAction="ifRoom"
        android:title="@string/Shops">
    </item>

</menu>
Run Code Online (Sandbox Code Playgroud)

任何帮助或有用的教程将被赞赏.

谢谢

And*_*dyN 6

你需要调用setHasOptionsMenu(true);onCreate()你的片段.这将允许您在片段上设置不同的选项菜单.从内部设置选项菜单onCreateOptionsMenu(Menu menu, MenuInflater inflater)

此外,要获取Action bar实例,请编写getSuppportActionBar()然后进行任何您想要执行的更改.

为了向后兼容,最好尽可能晚地拨打此电话,onCreate()或者甚至更晚onActivityCreated()或类似.

有关详细信息,请参阅

希望这对你有所帮助.