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

时间:2014-07-07 05:39:00

标签: android android-fragments

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

以下是我的活动代码。

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();
    }
}

以下是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

    }
}

这是我的标签碎片

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);
    }
}

我的排序按钮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>

任何帮助或有用的教程都将得到赞赏。

由于

1 个答案:

答案 0 :(得分:5)

您需要在片段的setHasOptionsMenu(true);中致电onCreate()。这将允许您在片段上设置不同的选项菜单。从onCreateOptionsMenu(Menu menu, MenuInflater inflater)

中设置选项菜单

此外,要获取操作栏实例,请编写getSuppportActionBar(),然后进行任何您想要做的更改。

为了向后兼容,最好在onCreate()结束时尽可能晚地拨打此电话,或者在onActivityCreated()或类似的情况下尽快拨打此电话。

请参阅this for more info.

希望这会对你有所帮助。