工具栏菜单项在片段中单击 [英] toolbar menu item click in fragments

查看:91
本文介绍了工具栏菜单项在片段中单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有人问过这个问题,但是看完这些答案后我无法解决我的问题.我有一个带有片段的活动. 在片段中,我添加了工具栏,我想处理片段中的工具栏菜单项单击事件.

I know this question has been asked and but I am unable to solve my problem after looking at those answer. I have an Activity with a Fragment. In fragment, I have added the toolbar and I want to handle toolbar menu item click events from the fragments.

在菜单中,我添加了一个共享按钮.我正在获取Up导航(箭头主页按钮)的点击事件回调,但是在片段中却没有共享按钮的点击事件回调.

In menu, I have added a single share button. I am getting the click event callback for the Up navigation (arrow home button) but I am not getting click event callback for the share button in my fragment.

能不能指出我在这里做错了什么.

Can some points me at what I am doing wrong here.

menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools" 
     tools:context="com.rahul.haridasasampada.details.DescriptionActivity">
     <item
         android:id="@+id/action_share"
         android:title="Share"
         android:orderInCategory="100"
         app:showAsAction="always"           
         app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>
 </menu> 

我已将工具栏添加到片段布局. 我的活动的代码-

I have added toolbar to the fragment layout. My Activity's code -

public class DescriptionActivity extends ActionBarActivity {



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

        if (savedInstanceState == null) {
            Article articleExtra = (Article) getIntent().getParcelableExtra(DescriptionFragment.ARGS_EXTRA);
            DescriptionFragment descriptionFragment = DescriptionFragment.newInstance(articleExtra);
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, descriptionFragment)
                    .commit();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        getSupportActionBar().setTitle(R.string.app_name_in_kannada);
    }

    @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_description, menu);
        Log.d("debug", "activity : onCreateOptionsMenu");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                Log.d("debug","activity: action home has clicked");
                onBackPressed();
                return false;
            case R.id.action_share:
                Log.d("debug","activity: action share has clicked");
                return false;
        }

        return super.onOptionsItemSelected(item);
    }


}

我的片段代码-

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    // some code
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    ActionBarActivity actionBarActivity = (ActionBarActivity) getActivity();
    Toolbar toolbar = (Toolbar) getView().findViewById(R.id.toolbar_actionbar);
    actionBarActivity.setSupportActionBar(toolbar);
    toolbar.setOnMenuItemClickListener(this);
    toolbar.inflateMenu(R.menu.menu_description);
}

@Override
public boolean onMenuItemClick(MenuItem menuItem) {
    switch (menuItem.getItemId()) {
        case R.id.action_share:
            if (menuItem.getItemId() == R.id.action_share)
                Log.d("debug","action share has clicked");
            return true;
    }
    return false;
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    Log.d("debug", "fragment : onCreateOptionsMenu");
    MenuItem shareMenuItem = menu.findItem(R.id.action_share);
    mShareActionProvider =(ShareActionProvider)MenuItemCompat.getActionProvider(shareMenuItem);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case android.R.id.home:
            Log.d("debug","fragment : action home has clicked");
            return true;
        case R.id.action_share:
            Log.d("debug","fragment: action share has clicked");
            return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View detailFragmentView = inflater.inflate(R.layout.fragment_description, null);

    return detailFragmentView;
}

推荐答案

从Android文档中获取Fragment:

From Android Docs, for Fragment:

onCreateOptionsMenu(菜单菜单,MenuInflater充气机)上的public void

public void onCreateOptionsMenu (Menu menu, MenuInflater inflater)

初始化活动"的标准选项菜单的内容.您应该将菜单项放在菜单中.要调用此方法,必须首先调用setHasOptionsMenu(boolean).

Initialize the contents of the Activity's standard options menu. You should place your menu items in to menu. For this method to be called, you must have first called setHasOptionsMenu(boolean).

所以您想控制Fragment中的操作栏菜单,那么您必须在Fragment的onCreate(...)中更好地调用setHasOptionsMenu方法:

so you want to control actionbar menu in Fragment, then you have to call setHasOptionsMenu method better in Fragment's onCreate(...):

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

同样重要的是,对于同一个ToolBar,您不应同时使用inflateMenuonCreateOptionsMenuinflateMenu是独立的,而无需将ToolBar设置为ActionBar.

also important is that you should not use inflateMenu and onCreateOptionsMenu both together for same ToolBar, inflateMenu is for standalone without setting ToolBar as ActionBar.


建议:

Activity中将一个ToolBar保留为您的App的ActionBar,将另一个ToolBar独立保存在Fragment中.

keep one ToolBar in Activity as ActionBar for your App, and another ToolBar standalone inside Fragment.

希望获得帮助!

这篇关于工具栏菜单项在片段中单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆