0

我正在尝试使用片段创建带有滑动视图的选项卡。我找到了一个解决方案,但它为每个选项卡提供了一个虚拟文本布局,而不是使用单独的布局。我想用三种不同的布局创建三个不同的类,并在选项卡中使用它们,如果可能的话,然后为每个选项卡使用自定义背景(选择/未选择时)。我可以在没有片段的情况下完成所有这些事情,但使用片段似乎具有挑战性。

public class MainActivity extends FragmentActivity {

    SectionsPagerAdapter mSectionsPagerAdapter;

    ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the app.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a DummySectionFragment (defined as a static inner class
            // below) with the page number as its lone argument.
            Fragment fragment = new DummySectionFragment();
            Bundle args = new Bundle();
            args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
            case 0:
                return getString(R.string.title_section1).toUpperCase(l);
            case 1:
                return getString(R.string.title_section2).toUpperCase(l);
            case 2:
                return getString(R.string.title_section3).toUpperCase(l);
            }
            return null;
        }
    }

    /**
     * A dummy fragment representing a section of the app, but that simply
     * displays dummy text.
     */
    public static class DummySectionFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        public static final String ARG_SECTION_NUMBER = "section_number";

        public DummySectionFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false);
            TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
            dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
            return rootView;
        }
    }

}
4

2 回答 2

0

要实现您的目标,您必须使用ActionBarSherlock库,并且要调用其他片段,您不需要使用 Intent,

几个月前我编写了相同类型的程序来调用我在代码下面使用的不同片段

 @Override
    public Fragment getItem(int arg0) {
        switch (arg0) {

        // Open FragmentTab1.java
        case 0:
            FragmentTab1 fragmenttab1 = new FragmentTab1();
            return fragmenttab1;

        // Open FragmentTab2.java
        case 1:
            FragmentTab2 fragmenttab2 = new FragmentTab2();
            return fragmenttab2;

        // Open FragmentTab3.java
        case 2:
            FragmentTab3 fragmenttab3 = new FragmentTab3();
            return fragmenttab3;
        }
        return null;
    }

并调用显示不同的 - 每个片段的不同布局,你应该调用不同的 - 不同的片段并且还需要编写 XML .....

FragmentTab1.java :-

public class FragmentTab1 extends SherlockFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Get the view from fragmenttab1.xml
        View view = inflater.inflate(R.layout.fragment1, container, false);
        return view;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        setUserVisibleHint(true);
    }
  }

片段1.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Your Layout Goes Here for Fragment1" />

</RelativeLayout>

以同样的方式,您必须为所有需要的片段编写类和 xmls....

有关更多详细信息或尝试示例,请使用:

  http://www.androidbegin.com/tutorial/android-actionbarsherlock-viewpager-tabs-tutorial/
于 2013-11-12T07:00:39.050 回答
0

在以下链接中,该链接可帮助您如何在分散的意图中创建 Tab 片段:

标签片段

于 2013-11-12T06:48:56.480 回答