我试图为我的应用程序包括平板电脑模式寻找一些帮助。它使用带有 4 个片段的 ViewPager,我想做的是为 7" 显示器和 10" 显示器提供平板电脑模式。我想在 10" 上以 4 方形布局而不是大视图寻呼机显示我的片段。在 7" 上,我想显示 2 方形布局。我只是想找到一个很好的例子来说明如何做到这一点。这是我下面的 viewpager 源代码和片段示例。此源使用库 ActionBarSherlock 和 ViewPagerIndicator。
主要活动
public class MainActivity extends SherlockFragmentActivity {
private ViewPager mPager;
private TitlePageIndicator mIndicator;
private MainPagerAdapter mAdapter;
private List<Fragment> mFragments;
public static final String PREFS_NAME = "MyPrefsFile1";
public CheckBox dontShowAgain;
private static final String FRAGMENT1 = EnlightenFragment.class.getName();
private static final String FRAGMENT2 = CommunityFragment.class.getName();
private static final String FRAGMENT3 = SupportFragment.class.getName();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_main);
getSupportActionBar().setDisplayShowTitleEnabled(false);
// add fragments
mFragments = new ArrayList<Fragment>();
mFragments.add(Fragment.instantiate(this, FRAGMENT1));
mFragments.add(Fragment.instantiate(this, FRAGMENT2));
mFragments.add(Fragment.instantiate(this, FRAGMENT3));
// adapter
mAdapter = new MainPagerAdapter(getSupportFragmentManager(), mFragments);
// pager
mPager = (ViewPager) findViewById(R.id.view_pager);
mPager.setOffscreenPageLimit(4);
mPager.setAdapter(mAdapter);
// indicator
mIndicator = (TitlePageIndicator) findViewById(R.id.title_indicator);
mIndicator.setViewPager(mPager);
}
}
主页面适配器
public class MainPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> mFragments;
private String[] titles = new String[] {"System", "Community", "Support" };
private int mCount = titles.length;
public MainPagerAdapter(FragmentManager fm, List<Fragment> f) {
super(fm);
mFragments = f;
}
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
@Override
public int getCount() {
return mCount;
}
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
}
支持片段
public class SupportFragment extends SherlockFragment {
WebView web;
static ProgressBar progressBar;
Context context;
@Override
public View onCreateView(LayoutInflater inf, ViewGroup grp, Bundle icicle) {
View v = inf.inflate(R.layout.activity_main, grp, false);
//mList = (ListView) v.findViewById(R.id.listView1);
web = (WebView) v.findViewById(R.id.webView);
progressBar = (ProgressBar) v.findViewById(R.id.progressBar1);
setHasOptionsMenu(true);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null)
((WebView) getSherlockActivity().findViewById(R.id.webView))
.restoreState(savedInstanceState);
web.setWebViewClient(new SpecialWebView(progressBar));
web.setWebChromeClient(new SpecialWebChromeClient(progressBar));
// Progress Bar - End
web.setWebViewClient(new myWebClient());
//progressBar = (ProgressBar) getSherlockActivity().findViewById(R.id.progressBar1);
web.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
}
});
WebSettings websettings = web.getSettings();
websettings.setBuiltInZoomControls(false);
web.setBackgroundColor(Color.parseColor("#f2f2f2"));
// websettings.setUseWideViewPort(position == 0 ? true : false);
websettings.setUseWideViewPort(true);
websettings.setJavaScriptEnabled(true);
websettings.setAllowFileAccess(true);
websettings.setDomStorageEnabled(true);
websettings.setLoadWithOverviewMode(true);
websettings.setSavePassword(true);
websettings.setBuiltInZoomControls(true);
web.loadUrl(url);
setHasOptionsMenu(true);
}