簡體   English   中英

導航抽屜和滑動選項卡:碎片和碎片活動問題

[英]Navigation drawer and swipe tabs: problems with fragment and fragmentactivity

我正在使用navigationDrawer,它具有3個片段。

在一個片段中,我要使用滑動選項卡,其中包含2個片段。

問題是navDrawer的片段擴展了Fragment

public class Verses extends Fragment {
}

但是它滑動選項卡它需要擴展FragmentActivity

public class Verses extends FragmentActivity {
}

所以我不知道如何解決。

        At their I/O 2015 conference, Google announced a new design support library, which can resolve your problem.

        you just need to add the following dependency to your app.gradle file

        compile 'com.android.support:design:22.2.0'

        **below is the complete code:**

        swipe_tab.xml file

        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <android.support.design.widget.TabLayout
                android:id="@+id/sliding_tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabMode="scrollable"
               />

            <android.support.v4.view.ViewPager
                android:id="@+id/viewpager"
                android:layout_width="match_parent"
                android:layout_height="0px"
                android:layout_weight="1"
                android:background="@android:color/white" />

        </LinearLayout>

        **fragment_page.xml** file

            <?xml version="1.0" encoding="utf-8"?>
            <TextView xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center" />

        **ViewPagerFragment.java**

            public class ViewPagerFragment extends Fragment {

                @Nullable
                @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

                    View view=inflater.inflate(R.layout.swipe_tabs,container,false);
                    ViewPager viewPager = (ViewPager)view. findViewById(R.id.viewpager);
                    viewPager.setAdapter(new SampleFragmentPagerAdapter(getChildFragmentManager(),getActivity()));

                    // Give the TabLayout the ViewPager
                    TabLayout tabLayout = (TabLayout) view.findViewById(R.id.sliding_tabs);
                    tabLayout.setupWithViewPager(viewPager);
                    return view;
                }
                public static ViewPagerFragment newInstance(int sectionNumber) {
                    ViewPagerFragment fragment = new ViewPagerFragment();
                    /*Bundle args = new Bundle();
                    args.putInt(ARG_SECTION_NUMBER, sectionNumber);
                    fragment.setArguments(args);*/
                    return fragment;
                }
            }


        **PageFragment.java**


            public class PageFragment extends Fragment {
                public static final String ARG_PAGE = "ARG_PAGE";

                private int mPage;

                public static PageFragment newInstance(int page) {
                    Bundle args = new Bundle();
                    args.putInt(ARG_PAGE, page);
                    PageFragment fragment = new PageFragment();
                    fragment.setArguments(args);
                    return fragment;
                }

                @Override
                public void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    mPage = getArguments().getInt(ARG_PAGE);
                }

                @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                         Bundle savedInstanceState) {
                    View view = inflater.inflate(R.layout.fragment_page, container, false);
                    TextView textView = (TextView) view;
                    textView.setText("Fragment #" + mPage);
                    return view;
                }
            } 

            **SampleFragmentPagerAdapter.java**

            public class SampleFragmentPagerAdapter extends FragmentPagerAdapter {
                final int PAGE_COUNT = 10;
                private String tabTitles[] = new String[] { "Tab1", "Tab2", "Tab3" ,"Tab4","Tab5","Tab6","Tab7" ,"Tab8","Tab9","Tab10"};
                private Context context;

                public SampleFragmentPagerAdapter(FragmentManager fm, Context context) {
                    super(fm);
                    this.context = context;
                }

                @Override
                public int getCount() {
                    return PAGE_COUNT;
                }

                @Override
                public Fragment getItem(int position) {
                    return PageFragment.newInstance(position + 1);
                }

                @Override
                public CharSequence getPageTitle(int position) {
                    // Generate title based on item position
                    return tabTitles[position];
                }
            }


    **At last you need to make following changes in your navigation drawer's MainActivity.java class**



    public void onNavigationDrawerItemSelected(int position) {
            // update the main content by replacing fragments
            FragmentManager fragmentManager = getSupportFragmentManager();
            switch (position) {
                case 0:

                fragmentManager.beginTransaction()
                        .replace(R.id.container, ViewPagerFragment.newInstance(position + 1))
                        .commit();
                    break;

                default: fragmentManager.beginTransaction()
                        .replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
                        .commit();
            }
        }



 **Don't forgot to add dependency in your build.gradle: 
     compile 'com.android.support:design:22.2.0'**

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM