工具栏菜单项单击片段

时间:2015-05-06 10:53:47

标签: android android-toolbar

我知道这个问题已被提出,但在查看答案后我无法解决问题。我有一个带片段的活动。 在片段中,我添加了工具栏,我想处理片段中的工具栏菜单项单击事件。

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

有些人可以指出我在这里做错了什么。

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> 

我在片段布局中添加了工具栏。 我的活动代码 -

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


}

My Fragments代码 -

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

5 个答案:

答案 0 :(得分:18)

来自Android文档,Fragment

  

public void onCreateOptionsMenu(菜单菜单,MenuInflater inflater)

     

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

所以你想控制Fragment中的actionbar菜单,然后你必须在Fragment的setHasOptionsMenu中更好地调用onCreate(...)方法:

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

同样重要的是,您不应将inflateMenuonCreateOptionsMenu用于同一ToolBarinflateMenu用于独立,而不将ToolBar设置为{{ 1}}。


建议:

ActionBar中的ToolBar作为Activity用于您的应用,将另一个ActionBar单独留在ToolBar内。

希望这有帮助!

答案 1 :(得分:9)

import android.support.v7.widget.Toolbar;
import android.view.MenuItem;


public class MyFragment extends Fragment implements Toolbar.OnMenuItemClickListener {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_list, container, false);

        Toolbar toolbar= (Toolbar) getActivity().findViewById(R.id.toolbar);
        toolbar.inflateMenu(R.menu.menu_main);
        toolbar.setOnMenuItemClickListener(this);

        return rootView;
    }


    @Override
    public boolean onMenuItemClick(MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case R.id.action_menu:
                //do sth here
                return true;
        }
        return false;
    }

答案 2 :(得分:6)

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
 {
   View v = inflater.inflate(R.layout.training1_fragment, container, 
              false);

       setHasOptionMenu(true);

       return v;
 }

@Override
 public boolean onOptionsItemSelected(MenuItem item) {
 switch (item.getItemId()) {
    case R.id.action_save : 
       {
       //Write here what to do you on click
        return true;
     }
   }
  return super.onOptionsItemSelected(item);
 }

答案 3 :(得分:2)

你应该从你的活动类中获取putPointer工具栏并在fragment类中扩充菜单。

你可以看看这个:

 @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        activity = ((DescriptionActivity ) getActivity());
        Toolbar tollbar=getView.findViewById(R.id.your_toolbar);
        activity.setSupportActionBar(toolbar);
        activity.toolbar.inflateMenu(R.menu.menu_search); 
        activity.toolbar.setOnMenuItemClickListener(this);     

    }



@Override
public boolean onMenuItemClick(MenuItem menuItem) {
    switch (menuItem.getItemId()) {
        case R.id.action_search:              
            break;
    }
    return false;
}

答案 4 :(得分:0)

点击片段选项菜单点击 link,请参考这里。