How to display the selected fragment on the tab of the action bar

I ran into one problem regarding swipe tabs. My project is built on Android 3.2. I implement tab swipe using the support library 4.0 (android-support-v4.jar). Everything implemented works fine, but when I deploy my application on the ICS device, in portrait mode I get a counter in the action bar for selecting tabs. In portrait mode, the tab selection does not change when the swipe is executed, although the contents change, and everything works fine in landscape mode.

final ActionBar actionBar = getActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); actionBar.setDisplayHomeAsUpEnabled(true); // Set up the ViewPager with the sections adapter. ViewPager mViewPager = (ViewPager) findViewById(R.id.pager); mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageSelected(int position) { actionBar.setSelectedNavigationItem(position); } }); 

I tried to set a breakpoint actionBar.setSelectedNavigationItem(position); on this line and even in portrait mode, which he calls, but the choice does not change.

Can anyone help with this?

Edition: I found a similar problem , but I don’t see exactly how it is solved and how to integrate it into my code.

+6
source share
1 answer

Problem: Due to the lack of real state, the platform uses minimized navigation (i.e. Spinner). The system automatically determines NAVIGATION_MODE_TABS for the landscape and NAVIGATION_MODE_LIST for the portrait, changing the orientation from landscape to portrait, updates the user interface, but for some reason does not update the navigation mode property to NAVIGATION_MODE_LIST, and therefore mActionView.setDropdownSelectedPosition is not called (position). See the following ActionBarImpl code: setSelectedNavigationItem

  public void setSelectedNavigationItem(int position) { switch (mActionView.getNavigationMode()) { case NAVIGATION_MODE_TABS: selectTab(mTabs.get(position)); break; case NAVIGATION_MODE_LIST: mActionView.setDropdownSelectedPosition(position); break; default: throw new IllegalStateException( "setSelectedNavigationIndex not valid for current navigation mode"); } } 

Workaround: Through reflection, we can get the spinner tab object and call the setSelection method.

 private Spinner getTabSpinner() { try { int id = getResources().getIdentifier("action_bar", "id", "android"); View actionBarView = findViewById(id); Class<?> actionBarViewClass = actionBarView.getClass(); Field mTabScrollViewField = actionBarViewClass.getDeclaredField("mTabScrollView"); mTabScrollViewField.setAccessible(true); Object mTabScrollView = mTabScrollViewField.get(actionBarView); if (mTabScrollView == null) { return null; } Field mTabSpinnerField = mTabScrollView.getClass().getDeclaredField("mTabSpinner"); mTabSpinnerField.setAccessible(true); Object mTabSpinner = mTabSpinnerField.get(mTabScrollView); if (mTabSpinner != null) { return (Spinner)mTabSpinner; } } catch (Exception e) { return null; } return null; } 

Then call the above method in the onPageSelected event.

  public void onPageSelected(int position) { actionBar.setSelectedNavigationItem(position); Spinner spinner = getTabSpinner(); if (spinner != null) { spinner.setSelection(position); } } 

Wrote this post https://gist.github.com/2657485

+16
source

Source: https://habr.com/ru/post/926655/


All Articles