gsueagle2008
gsueagle2008

Reputation: 4663

Dismiss Context Actionbar when viewpager changes

Right now I have an activity using a viewpager to display a several fragments. each of those fragments has implemented a Context Action Bar in the fragment. I am trying to make it where if the user scrolls or tabs between the fragments, the ActionBar for the previously selected fragment is dismissed.

I think I need to make some sort of call to the CAB .finish()in the fragment from my OnPageChangeListener() however I am not entirely sure how or where to do that.

Upvotes: 3

Views: 1248

Answers (2)

Febin Mathew
Febin Mathew

Reputation: 1021

You don't have to create individual Contextual Action Bar(CAB) for every fragments. You can simply create a single STATIC CAB in the TabbedActivity(Activity with pager) and use them anytime when needed.

Here is my code snippet of TabbedActivity.java

public class TabbedActivity extends AppCompatActivity {

public static ActionMode uniActionMode=null;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
       mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int 
         positionOffsetPixels) {
          if(uniActionMode!=null)
             uniActionMode.finish();
        }

        @Override
        public void onPageSelected(int position)
        {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

}

The mViewPager.addOnPageChangeListener is used to check if the user moves through the fragments. Now all you have to do is create an ActionMode from anywhere and then pass the value to the static uniActionMode variable.

mActionModeCallback = new ActionMode.Callback() {

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            // Inflate a menu resource providing context menu items
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.menu_selection, menu);
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
           return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {

            switch (item.getItemId()) {
                case R.id.action_delete:
                    mode.finish();
                    return true;

                case R.id.action_mark_read:

                    mode.finish();
                    return true;
            }
            return false;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {

        }
    };

    TabbedActivity.uniActionMode=  v.startActionMode(mActionModeCallback);

Here the TabbedActivity.uniActionMode references the ActionMode Object of the Tabbed Activity. Thus the callbacks are passed to the TabbedActivity.uniActionMode.

Upvotes: 0

Jakob Harteg
Jakob Harteg

Reputation: 9747

Create should a public function in each fragment which can be called to finish the ActionMode. Could look like this

public void finishActionMode(){
    [YOUR_ACTIONMODE_VARIABLE].finish();
}

Then in the activity you keep your ViewPager you should somehow keep or get a reference to the fragment in the ViewPager which should close its ActionMode, and then in your onPageSelected you can call `[YOUR_FRAGMENT_REFERENCE].finishActionMode();

Like this:

    mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            [YOUR_FRAGMENT_REFERENCE].finishActionMode();
        }

        ...
    });

For keeping a reference to a ViewPagers fragments look here: Retrieve a Fragment from a ViewPager

Upvotes: 1

Related Questions