我的viewPager标签没有显示

时间:2015-07-21 17:34:29

标签: android tabs android-tablayout

你好,我的材料测试应用程序有问题,我想测试选项卡设计,但它不适合我,我跟着一个教程,我做了同样的,但它没有工作,所有片段和XML文件都很好这是行不通的 有人能帮我吗 ! 谢谢

private ViewPager mPager;
private SlidingTabLayout mTab;

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

    toolbar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment)
            getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
    drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawerLayout), toolbar);

    mPager = (ViewPager) findViewById(R.id.pager);
    mPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
    mTab = (SlidingTabLayout) findViewById(R.id.tabs);
    mTab.setViewPager(mPager);


}

class MyPagerAdapter extends FragmentPagerAdapter {

    String[] tabs;

    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
        tabs = getResources().getStringArray(R.array.tabs);
    }

    @Override
    public Fragment getItem(int position) {
        MyFragment myFragment = MyFragment.getInstance(position);

        return myFragment;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return tabs[position];
    }

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

public static class MyFragment extends Fragment {
    private TextView textView;

    public static MyFragment getInstance(int position) {
        MyFragment myFragment = new MyFragment();
        Bundle args = new Bundle();
        args.putInt("position", position);
        myFragment.setArguments(args);
        return myFragment;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View layout = inflater.inflate(R.layout.frament_my, container, false);
        textView = (TextView) layout.findViewById(R.id.position);
        Bundle bundle = getArguments();

        if (bundle != null) {
            textView.setText("The page selector is " + bundle.getInt("position") + 1);
        }

        return layout;
    }
}

}

1 个答案:

答案 0 :(得分:0)

如果您使用片段寻呼机适配器,您可以在viewPager打开和关闭之间向xml中添加一个块,它将根据您定义的getPageTitle()来自动处理选项卡。

<android.support.v4.view.ViewPager
   android:id="@+id/vpPager"
   android:layout_width="match_parent"
   android:layout_height="wrap_content">

   <android.support.v4.view.PagerTabStrip
        android:id="@+id/pager_header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:paddingBottom="4dp"
        android:paddingTop="4dp" />

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

我从这个链接得到了这个,我认为你会发现有用的:https://github.com/codepath/android_guides/wiki/ViewPager-with-FragmentPagerAdapter