工具栏菜单项单击片段

时间:2022-11-03 15:50:38

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.

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

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


}

My Fragments code -

我的片段代码 -

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

4 个解决方案

#1


17  

From Android Docs, for Fragment:

来自Android Docs,for Fragment:

public void onCreateOptionsMenu (Menu menu, MenuInflater inflater)

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

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).

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

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

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

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

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.

同样重要的是你不应该将inflateMenu和onCreateOptionsMenu一起用于同一个ToolBar,inflateMenu用于独立而不将ToolBar设置为ActionBar。


Suggestion:

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

将一个ToolBar保存在Activity中,作为AppB的ActionBar,另一个ToolBar独立于Fragment中。

Hope this help!

希望这有帮助!

#2


6  

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

#3


4  

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

#4


2  

You should getPointer toolbar from your activity class and inflate menu in fragment class.

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

You can look this ex:

你可以看看这个:

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

#1


17  

From Android Docs, for Fragment:

来自Android Docs,for Fragment:

public void onCreateOptionsMenu (Menu menu, MenuInflater inflater)

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

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).

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

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

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

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

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.

同样重要的是你不应该将inflateMenu和onCreateOptionsMenu一起用于同一个ToolBar,inflateMenu用于独立而不将ToolBar设置为ActionBar。


Suggestion:

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

将一个ToolBar保存在Activity中,作为AppB的ActionBar,另一个ToolBar独立于Fragment中。

Hope this help!

希望这有帮助!

#2


6  

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

#3


4  

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

#4


2  

You should getPointer toolbar from your activity class and inflate menu in fragment class.

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

You can look this ex:

你可以看看这个:

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