I am currently trying to implement an application with several Tabs and i must use also ViewPager.
The difficulty i'm facing is to manage screen orientation changes... Let me explain more in detail..
In portrait there is a player full screen (1fragment), in lanscape i want it to change to a list at the left and a player at the right (2fragment)
For now viewPager is blocking me, i've search through the internet and found nothing for this issu with abs + viewpager.
I think the problem can be related to the adapter. For now it takes a "Class" variable and instanciate the fragment from it.
Do you have an idea ?
Any suggestion is welcome ! Thanks !
Here is the code of my FragmentPagerAdapter :
public static class TabsAdapter extends FragmentPagerAdapter implements ActionBar.TabListener, ViewPager.OnPageChangeListener { private final Context _context; private final ActionBar _actionBar; private final ViewPager _viewpager; private final ArrayList<TabInfo> _tabs = new ArrayList<TabInfo>(); static final class TabInfo { private final Class<?> clss; private final Bundle args; TabInfo(Class<?> _class, Bundle _args) { clss = _class; args = _args; } } public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) { super(activity.getSupportFragmentManager()); _context = activity; _actionBar = activity.getSupportActionBar(); _viewpager = pager; _viewpager.setAdapter(this); _viewpager.setOnPageChangeListener(this); } public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) { TabInfo info = new TabInfo(clss, args); tab.setTag(info); tab.setTabListener(this); _tabs.add(info); _actionBar.addTab(tab); notifyDataSetChanged(); } @Override public int getCount() { return _tabs.size(); } @Override public Fragment getItem(int position) { TabInfo info = _tabs.get(position); return Fragment.instantiate(_context, info.clss.getName(), info.args); } @Override public void onPageSelected(int position) { _actionBar.setSelectedNavigationItem(position); } @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { Object tag = tab.getTag(); for (int i = 0; i < _tabs.size(); i++) { if (_tabs.get(i) == tag) { _viewpager.setCurrentItem(i); } } } }
Here is the SherlockFragmentActivity :
public class MainActivity extends SherlockFragmentActivity { private ViewPager _viewPager; private PagerTitleStrip _pagerStrip; private ActionBar _actionBar; private TabsAdapter _tabsAdapter; private Tab _radioTab; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); _viewPager = (ViewPager) findViewById(R.id.viewpager); _actionBar = getSupportActionBar(); _tabsAdapter = new TabsAdapter(this, _viewPager); _radioTab = _actionBar.newTab().setCustomView(getTabIndicator(getString(R.string.radio_tab), android.R.drawable.ic_menu_manage)); _tabsAdapter.addTab(_radioTab, FragmentRadio.class, null); } private View getTabIndicator(String text, int drawable) { View indicator = _inflater.inflate(R.layout.tabs, null); ((TextView) indicator.findViewById(R.id.tab_title)).setText(text); ((ImageView) indicator.findViewById(R.id.tab_icon)).setImageResource(drawable); return indicator; } }
The class FragmentRadio :
public class FragmentRadio extends SherlockFragment { private View _view; public FragmentRadio() { super(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { _view = inflater.inflate(R.layout.fragment_radio, container, false); return _view; } }
The layout fragment_radio :
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_radio_container" android:layout_width="match_parent" android:layout_height="match_parent" > <fragment android:id="@+id/fragment_radio" android:name="com.egdigital.testing.ContentRadioFragment" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
The class ContentRadioFragment :
public class ContentRadioFragment extends SherlockFragment { // log private static final String TAG = ContentRadioFragment.class.getSimpleName(); // ihm private View _view; private Button _btnPlay; private Button _btnPause; private ImageView _imageViewCover; private ScrollingTextView _textViewName; private TextView _textViewState; private SeekBar _seekBar; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { _view = inflater.inflate(R.layout.content_radio, container, false); _btnPlay = (Button) _view.findViewById(R.id.btn_play); _btnPause = (Button) _view.findViewById(R.id.btn_pause); _textViewName = (ScrollingTextView) _view.findViewById(R.id.tv_title); _textViewState = (TextView) _view.findViewById(R.id.tv_buffering); _seekBar = (SeekBar) _view.findViewById(R.id.seekBar1); return _view; } }
And the layout content_radio is a normal layout