viewPager不响应标签点击

时间:2016-08-05 03:02:18

标签: android android-viewpager android-tablayout

这是我的活动

public class MainActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private TabLayout tablayout;
    private int[] tableIcons = {android.R.drawable.ic_menu_camera,
        android.R.drawable.ic_menu_add, android.R.drawable.ic_media_next};

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

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

        tablayout = (TabLayout) findViewById(R.id.tab);
        setUpwithviewpager(viewPager);
        tablayout.setupWithViewPager(viewPager);
        tablayout.getTabAt(0).setIcon(tableIcons[0]);
        tablayout.getTabAt(1).setIcon(tableIcons[1]);
        tablayout.getTabAt(2).setIcon(tableIcons[2]);
        tablayout.setTabMode(TabLayout.MODE_FIXED);
    }

    public void setUpwithviewpager(ViewPager viewpager) {
        ViewPageradapter adapter = new       ViewPageradapter(getSupportFragmentManager());
        adapter.addFragment(OneFragment.newInstance("1"), "one");
        adapter.addFragment(TwoFragment.newInstance("2"), "two");
        adapter.addFragment(ThreeFragment.newInstance("3"), "three");
        viewpager.setAdapter(adapter);
    }
}

这是布局xml文件

<android.support.design.widget.CoordinatorLayout   
    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.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">    
    </android.support.design.widget.AppBarLayout>

    <android.support.design.widget.TabLayout
        android:id="@+id/tab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="fill"
        app:tabMode="fixed"></android.support.design.widget.TabLayout>

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

当我更改viewpager时,我的tablayout没有响应,并且当我单击选项卡时,viewpager也没有响应。我不知道为什么,我找不到解决方案来解决这个问题。

3 个答案:

答案 0 :(得分:1)

您需要向选项卡布局添加选项卡侦听器,并向viewPager添加onPageChangeListener。现在通常使用ActionBar.TabListenerViewPager.OnPageChangeListener完成此操作。请参阅Creating Swipe Views with Tabs上的Android文章。

你需要像

这样的东西
// Create a tab listener that is called when the user changes tabs.
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        // When the tab is selected, switch to the
        // corresponding page in the ViewPager.
        viewpager.setCurrentItem(tab.getPosition());
    }

viewpager = (ViewPager) findViewById(R.id.pager);
viewpager.setOnPageChangeListener(
        new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                // When swiping between pages, select the
                // corresponding tab.
                getActionBar().setSelectedNavigationItem(position);
            }
        });

答案 1 :(得分:1)

尝试使用以下代码:

viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

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

            }

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

            }

        });

答案 2 :(得分:0)

这个问题也影响了我几个小时。对我有用的是使我的ViewPagerAdapter扩展FragmentStatePagerAdapter而不是FragmentPagerAdapter。我希望这会有所帮助。