简体   繁体   English

如何在下属片段中使用ActionBar项

[英]How to Use ActionBar items in subordinate fragments

I have an Activity that has a tabhost and a ViewPager that support 3 fragments. 我有一个活动,它具有Tabhost和支持3个片段的ViewPager。 I want to use the ActionBar (which resides in the Activity) to use and modify menu items selectable from the ActionBar directly in each of the fragments. 我想使用ActionBar(位于Activity中)来直接在每个片段中使用和修改可从ActionBar选择的菜单项。

What is the best way to send a message from the activity to the fragment to tell it to implement one of the actionbar items? 将消息从活动发送到片段以告诉它实现操作栏项之一的最佳方法是什么?

I have tried the following: 我尝试了以下方法:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        FragmentManager manager = getSupportFragmentManager();
        List<Fragment> fs = new ArrayList<>(manager.getFragments());
        int fragId = fs.get(0).getId();
        PlayFragment fragment = (PlayFragment) manager.findFragmentById(fragId);
        fragment.settingsMenu();
        return true;

This is not working, but also is bad form to need to know the order of the fragments to get it to work. 这是行不通的,但也是坏形式,需要知道片段的顺序才能使其正常工作。 This way didn't work for me. 这种方式对我不起作用。 I know I could use a broadcastSender to make it work, but that is also bad form. 我知道我可以使用broadcastSender使其工作,但这也是不好的形式。 The REAL problem here is that since the fragments are sitting on top of a ViewPager within a TabHost, there is no way to get the id or tag reliably, so 真正的问题在于,由于片段位于TabHost内的ViewPager顶部,因此无法可靠地获取ID或标签,因此

manager.findFragmentById(fragId) and manager.findFragmentByTag("MyFragmentName") are not useful. manager.findFragmentById(fragId)和manager.findFragmentByTag(“ MyFragmentName”)无效。

When I try to attach the toolBar to an OnClickListener within the fragment, it doesn't work, and fragment has no support for @Override public boolean onOptionsItemSelected(MenuItem item); 当我尝试将ToolBar附加到片段内的OnClickListener时,它不起作用,并且片段不支持@Override public boolean onOptionsItemSelected(MenuItem item);

I'll keep looking for a solution. 我将继续寻找解决方案。 Thanks. 谢谢。

I found the answer on another post. 我在另一篇文章中找到了答案。 The trick is to get the toolbar from the activity but not to use it as an actionbar directly. 诀窍是从活动中获取工具栏,而不是直接将其用作操作栏。 You need to inflate your own menu, and have access to modifying title etc ; 您需要膨胀自己的菜单,并有权修改标题等;

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.play_fragment, container, false); @Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){return inflater.inflate(R.layout.play_fragment,container,false); } }

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    app = JamSessionApplication.getInstance();

    toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
    toolbar.setTitle(song);
    // Set an OnMenuItemClickListener to handle menu item clicks
    toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            if (id == R.id.action_reverb) {
                Toast.makeText(getActivity(), "Reverb", Toast.LENGTH_SHORT).show();
                return true;
            }
            return true;
        }
    });
   toolbar.inflateMenu(R.menu.menu_play);

} }

I still encountered problems with ViewPager/Tabhost toobars working properly. 我仍然遇到ViewPager / Tabhost toobars正常工作的问题。 I found an article which was much more helpful in fixing this. 我发现有一篇文章对解决此问题有很大帮助。 https://www.numetriclabz.com/android-tabs-with-fragments-and-viewpager-tutorial/ https://www.numetriclabz.com/android-tabs-with-fragments-and-viewpager-tutorial/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM