contextual action mode in fragment - close if not focused?

前端 未结 5 808
小蘑菇
小蘑菇 2021-02-04 15:08

i implemented a contextual action mode bar in a nested fragement. This fragment is part of a view pager and the view pager is also a fragment and part of a navigation drawer.

5条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-04 16:05

    Here's what worked for me --

    1. Hold a static reference to the action mode in MyFragment:

    public static ActionMode mActionMode;

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            mActionMode = mode;
            return true;
        }
    
        @Override
        public void onDestroyActionMode(ActionMode mode) {
            mActionMode = null;
        }
    
    1. Set ViewPager.OnPageChangeListener in MyViewPagerActivity.

     mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    
                ....
    
                @Override
                public void onPageScrollStateChanged(int state) {
                    if(MyFragment.mActionMode != null) MyFragment.mActionMode.finish();
                }
    
    
            });

提交回复
热议问题