我正在努力完成这项工作。我想要一个带有选项卡和 Viewpager 的操作栏。在选项卡中,应有一个或两个片段,具体取决于屏幕方向。
一或两个片段必须是 (Sherlock)ListFragments。
到目前为止,这有效。但是片段中没有任何内容。甚至没有调用第一个片段的 onCreate 或 onCreateView 。
这里主要活动:
public class tabActivity extends SherlockFragmentActivity
{
private ViewPager mViewPager;
private TabsAdapter mTabsAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
setTheme(R.style.Theme_Sherlock);
super.onCreate(savedInstanceState);
mViewPager = new ViewPager(this);
//mViewPager.setId(R.id.pager);
setContentView(R.layout.main);
ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
mTabsAdapter = new TabsAdapter(this, mViewPager);
mTabsAdapter.addTab(bar.newTab().setText("Advanced"),TitlesFragment.class, null);
}
其次是强制性的 TabsAdapter。我已经使它与 ABS 一起工作。标题片段:
public static class TitlesFragment extends SherlockListFragment
{
boolean mDualPane;
int mCurCheckPosition = 0;
@Override
public void onCreate(Bundle savedInstanceState)
{
View detailsFrame = getActivity().findViewById(R.id.details);
mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;
if (savedInstanceState != null) {
mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
}
if (mDualPane) {
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
DetailsFragment details = (DetailsFragment)getFragmentManager().findFragmentById(R.id.details);
if (details == null)
{
details = DetailsFragment.newInstance();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.details, details);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View mView = inflater.inflate(R.layout.list_entervalues, container, false);
Activity mFracmentActivity = super.getSherlockActivity();
setListAdapter(new SpecListAdapter(mFracmentActivity, Shakespeare.TITLES));
return mView;
}
}
如果我使用 SpecListAdapter 或尝试调用 setListAdapter(new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_activated_1, Shakespeare.TITLES)); 在 TitlesFragment 的 onCreate() 方法中。
main.xml(资源/布局):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
main.xml (res/layout-land):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
<FrameLayout android:id="@+id/details" android:layout_weight="1"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:background="?android:attr/detailsElementBackground" />
</LinearLayout>