简体   繁体   English

onOptionsItemSelected挂钩因错误片段而被调用

[英]onOptionsItemSelected hook called for wrong fragment

I've got a simple activity with FragmentPagerAdapter displaying some charts in Fragments . 我有一个简单的活动FragmentPagerAdapter显示在一些图表Fragments I can navigate through them by clicking the tab in ActionBar or swiping on one or the other side. 我可以通过单击ActionBar的标签或在一侧或另一侧滑动来浏览它们。 That's how a Fragment looks like: Fragment就是这样的:

public static class ChartFragment extends Fragment {

    private static final String ARG_SECTION_NUMBER = "section_number";
    private int section;

    /**
     * Returns a new instance of this fragment for the given section number.
     */
    public static ChartFragment newInstance(int sectionNumber) {
        ChartFragment fragment = new ChartFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public ChartFragment() {
        // enable menu for fragment
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        int arg = getArguments().getInt(ARG_SECTION_NUMBER);
        section = arg;
        return generateChart(arg);
    }

    private View generateChart() {
        // generate the chart
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
       // handle item selection
       switch (item.getItemId()) {
          case R.id.action_button:
             Log.e(TAG, "Section: " + section);
             return true;
          default:
             return super.onOptionsItemSelected(item);
       }
    }
}

(for your information: the code sample is based on this one built in ADT) (供您参考:该代码示例基于ADT内置的此示例)

I've enabled the menu in ActionBar for each fragment and the click gets handled in onOptionsItemSelected hook. 我已经为每个片段启用了ActionBar的菜单,并且在onOptionsItemSelected挂钩中处理了单击。 When selecting a tab manually in ActionBar everything works fine, but when swiping through the fragments and clicking on the ActionBar button action_button shortly after navigating to the Fragment , the sectionnumber of the previously displayed Fragment is shown! 当手动选择一个标签ActionBar一切工作正常,但通过片段滑动和点击时ActionBar按钮action_button导航到后不久Fragment ,以前显示的sectionnumber Fragment显示!
What can I do to make this behave "normally" also when swiping? 如何在刷卡时使其也“正常”运行?

In the time the swiping is taking place the last fully shown fragment handles the onOptionsItemSelected hook. 在进行刷卡时,最后一个完整显示的片段将处理onOptionsItemSelected挂钩。 To avoid that the user clicks the button for the wrong fragment, I simply block the action triggered by a button click while the transition. 为了避免用户单击错误片段的按钮,我只是阻止了在过渡期间由按钮单击触发的操作。 Therefore I use this listener: 因此,我使用此侦听器:

mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrollStateChanged(int arg0) {
            if (arg0 == 0) {
                isScrolling = false;
            } 
            else {
                isScrolling = true;
            }               
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }           
});

2nd method: 第二种方法:

Keeping track of all Fragment instances and handling the hook in the activity. 跟踪所有Fragment实例并处理活动中的钩子。

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

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