Android上无法显示View pager上的标题。怎么解决?

时间:2018-12-16 09:48:33

标签: android android-recyclerview android-sqlite

我想用视图寻呼机创建选项卡布局,但是当我运行程序时,选项卡没有标题。这是onCreate中的代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_trans, container, false);

    TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabs);
    tabLayout.addTab(tabLayout.newTab().setText("Haloo"));
    tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_layout_calender));
    tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_layout_Monthly));


    final ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager);


    mTextMessage = (TextView) view.findViewById(R.id.message);

    textViewName = (AppCompatTextView) view.findViewById(R.id.textViewName);
    recyclerViewUsers = (RecyclerView) view.findViewById(R.id.recyclerViewUsers);

    LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
    mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

    viewPager.setAdapter(new PagerAdapter(getFragmentManager(), tabLayout.getTabCount()));
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setupWithViewPager(viewPager);
    tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(1, true);

        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }

    });

    initObjects();

    return view;

}

和这个代码片段状态寻呼机适配器。

public class PagerAdapter extends FragmentStatePagerAdapter {
        int mNumOfTabs;

        public PagerAdapter(FragmentManager fm, int NumOfTabs) {
            super(fm);
            this.mNumOfTabs = NumOfTabs;
        }


        @Override
        public Fragment getItem(int position) {

            switch (position) {
                case 0:
                    return new FragmentTabDaily();
                case 1:
                    return new FragmentTabCalender();
                case 2:
                    return new FragmentTabMontly();

                default:
                    return null;
            }
        }

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

我看不到每个选项卡布局的名称。您能帮我解决这个问题吗? 运行后我没有看到任何按摩错误,但是标题viewpager没有显示。

0 个答案:

没有答案