1

In my android application, I layout the main activity using ViewPager with Action Bar. The PagerAdapter is FragmentStatePagerAdapter.

My first fragment contains a ListView with batch contextual actions enabled. When CAB is on the screen, I swipe to the next fragment, it is still on. Actually, it is kept in all fragments. How can I remove it when swiping?

There may be other states keeping when swiping. How can remove them all?

Alessandro Minoccheri
  • 32,975
  • 19
  • 110
  • 159
wang
  • 98
  • 6

1 Answers1

0
  1. Make your activity implement ViewPager.OnPageChangeListener
  2. Set the OnPageChangeListener of your ViewPager to be the activity
  3. In your overridden onPageSelected(int position) method, dissmiss your CAB
Neilers
  • 411
  • 5
  • 11
  • I have written this code and it works. `= 0 && mLastPositon != position && mTabs.get(mLastPositon).fragment instanceof BaseViewPagerFragment) { ((BaseViewPagerFragment)mTabs.get(mLastPositon).fragment).clear(); } mActionBar.setSelectedNavigationItem(position); }>` I dismiss CAB in the clear method, but how can I restore the CAB when I swipe back? – wang Nov 16 '12 at 12:27
  • Well, you've already got the logic to dismiss the CAB when the selected page is anything either than an instance of `BaseViewPagerFragment`, correct? Put an else in your if and implement a method that returns the CAB when selected page is an instance of `BaseViewPagerFragment` – Neilers Nov 17 '12 at 05:44
  • I don't know how to show CAB. I have simply tried setItemChecked method on the list view in the onCreateView callback, but it just make the item checked(CAB not showing) when I frist load my fragment. My Code is `mTaskView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); mTaskView.setMultiChoiceModeListener(onTaskLongClickListener); mTaskView.setItemChecked(0, true);` I roughly read the setItemChecked source code `if (value && mChoiceMode == CHOICE_MODE_MULTIPLE_MODAL && mChoiceActionMode == null) { mChoiceActionMode = startActionMode(mMultiChoiceModeCallback);}` – wang Nov 18 '12 at 05:42
  • and I think the startActionMode method should be invoked. Do I miss something or something wrong ? – wang Nov 18 '12 at 05:51
  • I have just tried to move the setItemChecked mothod to onStart callback and it works. I can't figure out the reason. – wang Nov 18 '12 at 06:09
  • Yes, in order for the CAB to be shown, startActionMode should be called somewhere and in your case, when an item is selected in your list. You don't explicitly call startActionMode since the setItemChecked method does that for you. I would have advised you to explicitly call startActionMode in your onPageChangedListener but it seems that you have already found a method that solves your problem. – Neilers Nov 18 '12 at 11:20