我目前正在尝试使用多个选项卡实现应用程序,并且我还必须使用 ViewPager。
我面临的困难是管理屏幕方向的变化......让我更详细地解释......
在纵向有一个播放器全屏(1片段),在横向我希望它更改为左侧的列表和右侧的播放器(2片段)
现在 viewPager 阻止了我,我在互联网上搜索并没有发现这个问题与 abs + viewpager。
我认为问题可能与适配器有关。现在它需要一个“类”变量并从中实例化片段。
你有想法吗 ?
欢迎任何建议!谢谢 !
这是我的 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 是普通布局