Android:导航抽屉片段内的Viewpager

时间:2014-04-09 16:15:47

标签: android android-layout android-fragments navigation android-viewpager

我目前正在开发Android应用程序,我需要的是在导航抽屉的片段中实现Viewpager或Tabs。我已经在本教程中实现了导航抽屉:Navigation Drawer Tutorial

现在我的导航抽屉由3个片段组成。 片段A,片段B,片段C

片段A 如何为此片段添加 Viewpager?

4 个答案:

答案 0 :(得分:10)

这绝对是可能的。您只需要为viewpager使用子片段。除此之外,实施是直截了当的。创建自定义寻呼机适配器,使用标准viewpager api

像这样扩展FragmentPagerAdapter:

private class MyPagerAdapter extends FragmentPagerAdapter
{
    public MyPagerAdapter (FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        switch (i) {
            case 0:
                return Fragment1;
            case 1:
                return Fragment2;

        }
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return FRAGMENT_1_NAME;
            case 1:
                return FRAGMENT_2_NAME;



        }
    }

当然,你需要一个带有viewpager的布局,然后确保在片段A中将它连接起来:

myPagerAdapter = new MyPagerAdapter(this.getChildFragmentManager());
myPager = (ViewPager) mRoot.findViewById(R.id.pager);
myPager.setAdapter(myPagerAdapter);

请注意,除非您的最低SDK为4.2或更高版本,否则您需要使用支持库和支持片段,因为子片段是API 17

答案 1 :(得分:1)

这适用于Android< 4.2。 在您的片段A onCreateView方法中,当您对根View进行充气时,请对包含android.support.v4.view.ViewPager的广告进行充气:

View rootView = inflater.inflate(R.layout.your_viewpager_layout, container, false);

your_viewpager_layout.xml类似于以下内容:

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.yourapp.YourActivity" />

然后在FragmentPagerAdapter.getItem(int position)方法中,返回您要在Fragment中显示的ViewPager,就像在&#34;普通&#34;中一样。 ViewPager。

答案 2 :(得分:0)

ViewPager类是支持库的一部分。您应该能够将其与PagerAdapter一起使用。

答案 3 :(得分:0)

这里你去..首先是tabbed或view pager片段类..

public class TabbedFragment extends Fragment {
private SectionsPagerAdapter mSectionsPagerAdapter;
public static final String TAG = TabbedFragment.class.getSimpleName();

/**
 * The {@link ViewPager} that will host the section contents.
 */
ViewPager mViewPager;


public static TabbedFragment newInstance() {
    return new TabbedFragment();
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_viewpager_network, container, false);
    mSectionsPagerAdapter = new SectionsPagerAdapter(
            getChildFragmentManager());

    mViewPager = (ViewPager) v.findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    return v;
}


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.
        if (position == 0)
            return FragmentOne.getInstance();
        else
            return FragmentTwo.getInstance();
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
            case 0:
                return "Fragment 1";
            case 1:
                return "Fragment 2";
        }
        return null;
    }
  }
}

xml文件tabbed_fragment.xml

<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!--
This title strip will display the currently visible page title, as well as the page
titles for adjacent pages.
-->

<android.support.v4.view.PagerTitleStrip
    android:id="@+id/pager_title_strip"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:background="#33b5e5"
    android:paddingBottom="4dp"
    android:paddingTop="4dp"
    android:textColor="#fff" />

</android.support.v4.view.ViewPager>

在使用导航抽屉的活动类中的用法,注意:我使用了android studio 1.2提供的导航抽屉代码

@Override
public void onNavigationDrawerItemSelected(int position) {
    boolean isChild = getSharedPreferences(Constansts.PREFERENCE_NAME,0).getBoolean(Constansts.IS_CHILD,false);
    Fragment fragment = null;
    if (lastSelectedPosition == position)
        return;
    lastSelectedPosition = position;
    switch (position) {
        case 0:
            fragment = HomeFragment.getInstance();
            break;
        case 1:
            fragment = HomeFragment.getInstance();
            break;
        case 2:
            fragment = UserProfileFragment.getInstance();
            break;
        case 3:
            fragment = TabbedFragment.getInstance();
            break;
    }
    if (fragment == null) {
        fragment = HomeFragment.getInstance();
    }
    fragmentManager = getSupportFragmentManager();
    fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.container, fragment).addToBackStack(mTitle.toString());
    fragmentTransaction.commit();
}