简体   繁体   English

ViewPager中片段的onOptionsItemSelected未调用

[英]onOptionsItemSelected of fragment inside ViewPager not called

I have a problem with developing an app for both tablets and smartphones with fragments. 我在为带有片段的平板电脑和智能手机开发应用程序时遇到问题。

When app is executed on smartphone, I show a fragment with a list (categories). 当应用在智能手机上执行时,我会显示一个带有列表(类别)的片段。 When clicking on an item, I start a new activity that holds a ViewPager that inflates Fragments with another list (detail). 单击一个项目时,我将启动一个新活动,该活动包含一个ViewPager,该ViewPager用另一个列表(详细信息)对Fragments进行膨胀。

When app is executed on tablet I have a layout with two fragments inside. 在平板电脑上执行应用程序时,我的布局里面有两个片段。 The left one is the categories list and the right one is the detail list. 左边的是类别列表,右边的是详细信息列表。

Up to here there's no problem, but when setting onOptionsItemSelected I have problems. 到目前为止,没有问题,但是在设置onOptionsItemSelected时出现了问题。

When executing app on smartphone, everything is working well. 在智能手机上执行应用程序时,一切运行良好。 This works: 这有效:

Activity containing ViewPager 包含ViewPager的活动

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.icShare:
            sharePortal();
            break;
        default:
            break;
    }

    return super.onOptionsItemSelected(item);
}

Fragment inflated by ViewPager 由ViewPager膨胀的片段

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.icFavourite:
            Utils.setFavouritePortal(mContext, mPortal);
            getActivity().invalidateOptionsMenu();
            return true;
        case R.id.icShowFavourites:
            showFavouriteArticles();
            return true;
        default:
            break;
    }

    return super.onOptionsItemSelected(item);
}

But when executing the app in tablet, onOptionsItemSelected is not called. 但是在平板电脑中执行应用程序时,不会调用onOptionsItemSelected。 This is the code: 这是代码:

Fragment containing ViewPager 包含ViewPager的片段

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.icShowFavourites:
            Log.e("asdf", "asdfasdf - test tablet");
            break;
        default:
            break;
    }

    return super.onOptionsItemSelected(item);
}

Fragment inflated by ViewPager 由ViewPager膨胀的片段

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.icFavourite:
            Utils.setFavouritePortal(mContext, mPortal);
            getActivity().invalidateOptionsMenu();
            return true;
        case R.id.icShowFavourites:
            showFavouriteArticles();
            return true;
        default:
            break;
    }

    return super.onOptionsItemSelected(item);
}

The only difference is that when executing on smartphone, the first onOptionsItemSelected is hold by an Activity and in tabled is hold by a fragment. 唯一的区别是,在智能手机上执行时,第一个onOptionsItemSelected由一个Activity保留,而在表中则由一个片段保留。 I also tried to execute the onOptionsItemSelected from the Activity containing the fragment that contains the ViewPager inflating other fragments with no luck. 我还尝试从包含以下片段的Activity中执行onOptionsItemSelected,该片段包含ViewPager使其他片段没有运气。

How can I get it working? 我该如何运作?

Thank you in advance! 先感谢您!

Try to put setHasOptionsMenu(true); 尝试放置setHasOptionsMenu(true); inside the fragment where you want onOptionsItemSelected() to be called. 在您要调用onOptionsItemSelected()的片段中。

Documentation 文献资料

该片段应调用setHasOptionsMenu(true) ,并应实现onCreateOptionsMenu()onOptionsItemSelected()

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

相关问题 在 AppCompatActivity 中使用 setDisplayHomeAsUpEnabled 在片段中未调用 onOptionsItemSelected - onOptionsItemSelected not called in fragment with setDisplayHomeAsUpEnabled in AppCompatActivity 在Fragment上不调用onOptionsItemSelected()方法 - onOptionsItemSelected() method not get called on Fragment (已弃用)片段 onOptionsItemSelected 未被调用 - (Deprecated) Fragment onOptionsItemSelected not being called 片段xamarin android中未调用OnOptionsItemSelected - OnOptionsItemSelected is not called in fragment xamarin android onOptionsItemSelected挂钩因错误片段而被调用 - onOptionsItemSelected hook called for wrong fragment 片段setUserVisibleHint未在另一个ViewPager内的Viewpager中调用 - fragment setUserVisibleHint is not called in Viewpager inside anotherViewPager viewPager内的OnPageChange片段在onCreateView之前调用的片段内 - OnPageChange fragment inside viewpager inside fragment called before onCreateView 活动中的OnOptionsItemSelected在fragment中的onOptionsItemSelected之前调用。 其他方式可能吗? - OnOptionsItemSelected in activity is called before onOptionsItemSelected in fragment. Other way possible? 不调用onOptionsItemSelected按下片段上的导航图标 - onOptionsItemSelected not called pressing up navigation icon on fragment 片段中未调用onOptionsItemSelected - onOptionsItemSelected not being called from within fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM