无法在Android标签页中点击水龙头

时间:2018-10-05 12:04:29

标签: android android-studio tabs fragment

我在选项卡内部使用了选项卡布局。主选项卡通过单击可以很好地运行,而子选项卡通过幻灯片可以运行,但是我不能通过单击其来更改子选项卡。 我一直在搜索整个Internet几天,但没有发现任何东西。 标签片段代码:

public class main_MenuFragment extends Fragment {

    private main_menuFragment_PagerAdapter m_m_frg_pageradapter;
    private ViewPager main_menu_ViewPager;

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

        TabLayout tabLayout = (TabLayout) view.findViewById(R.id.fragmnet_main_menu_tabs);

        m_m_frg_pageradapter = new main_menuFragment_PagerAdapter(getChildFragmentManager());
        main_menu_ViewPager = (ViewPager) view.findViewById(R.id.fragmnet_main_menu_container);

        main_menu_ViewPager.setAdapter(m_m_frg_pageradapter);
        tabLayout.setupWithViewPager(main_menu_ViewPager);


        main_menu_ViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(main_menu_ViewPager));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

                Toast.makeText(G.context, "Position " + tab.getPosition(), Toast.LENGTH_SHORT).show();

            }

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

            }

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

            }
        });

        return view;
    }
}

我的xml文件代码:

<FrameLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.nikosoft.orderonline.main_MenuFragment">

    <android.support.design.widget.TabLayout
        android:id="@+id/fragmnet_main_menu_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:elevation="6dp">

<android.support.v4.view.ViewPager
android:id="@+id/fragmnet_main_menu_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />


</FrameLayout>

和我的适配器类:

public class main_menuFragment_PagerAdapter extends FragmentStatePagerAdapter {
    final int PAGE_COUNT = 3;
    private String tabTitles[] = new String[] { "Tab1", "Tab2", "Tab3" };

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

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

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

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

请帮助我
谢谢大家

0 个答案:

没有答案