ABS + ViewPager +管理方向变化

时间:2013-07-23 16:02:39

标签: android android-fragments actionbarsherlock android-viewpager android-orientation

我目前正在尝试使用多个标签来实施一个应用程序,我也必须使用ViewPager。

我面临的困难是管理屏幕方向的变化......让我详细解释一下......

在肖像中有一个玩家全屏(1片段),在lanscape我希望它改为左边的列表和右边的玩家(2片段)

现在viewPager阻止了我,我通过互联网搜索,并没有发现abs + viewpager的这个发布。

我认为问题可能与适配器有关。现在它需要一个“Class”变量并从中实现片段。

你有什么想法吗?

欢迎任何建议!谢谢!


这是我的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);
                }
            }
        }
    }

这是 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;
    }
}

班级 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;
    }
}

布局 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>

班级 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;
    }
}

布局content_radio是普通布局

0 个答案:

没有答案