Actionbar和Tabs以及Viewpager和Fragments以及Screenorientation

时间:2012-07-04 15:47:29

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

我正在努力完成这项工作。我想要一个带Tabs和Viewpager的Actionbar。在标签中,根据屏幕方向,应有一个或两个片段。

一个或两个片段必须是(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一起工作了。 TitlesFragment:

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(res / layout):

    <?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>

0 个答案:

没有答案