Android TabLayout setOnTabSelectedListener无效

时间:2015-12-03 14:40:27

标签: java android android-tablayout

假设我有MainActivity课程,其中TabLayoutViewPage r初始化为相应的4页。

MainActivity.class

public class MainActivity extends AppCompatActivity {
    private SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        // TabLayout
        TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.setupWithViewPager(mViewPager);
    }

    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 PlaceholderFragment (defined as a static inner class below).
            // return PlaceholderFragment.newInstance(position + 1);
            switch (position) {
                case 0:
                    ActivityZero tab0 = new ActivityZero();
                    return tab0;
                case 1:
                    ActivityOne tab1 = new ActivityOne();
                    return tab1;
                case 2:
                    ActivityTwo tab2 = new ActivityTwo();
                    return tab2;
                case 3:
                    ActivityThree tab3 = new ActivityThree();
                    return tab3;
                default:
                    return null;
            }
        }
        @Override
        public int getCount() {
            // Show 4 total pages.
            return 4;
        }
        //Setting the ICON
        private int[] imageResId = {
                R.drawable.ic_zero
                R.drawable.ic_one,
                R.drawable.ic_two,
                R.drawable.ic_three
        };
        @Override
        public CharSequence getPageTitle(int position) {
            Drawable image = ContextCompat.getDrawable(getApplicationContext(), imageResId[position]);
            image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
            SpannableString sb = new SpannableString(" ");
            ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
            sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            return sb;
        }
    }
}

使用上面的代码,一切正常,但是当我在setOnTabSelectedListener中添加onCreate时:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        // TabLayout
        TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.setupWithViewPager(mViewPager);

        //This is what i'm trying to add
        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mViewPager.setCurrentItem(tab.getPosition());
                tab.getIcon().setAlpha(255);
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.getIcon().setAlpha(127);
            }
            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }

当我跑步时,一切正常,但当我滑动更改标签时,应用程序不再响应并开始崩溃。

我正在尝试获取所选标签position并在icon上设置setAlpha(127) selected page,但没有运气。

注意:我正在使用支持库,我目前的API是8。

更新

12-04 11:32:12.342 11884-11884/com.sample.app.mysampleapp E/InputEventReceiver: Exception dispatching input event.
12-04 11:32:12.342 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI: java.lang.NullPointerException
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at com.sample.app.mysampleapp.MainActivity$1.onTabUnselected(MainActivity.java:53)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.support.design.widget.TabLayout.selectTab(TabLayout.java:867)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.support.design.widget.TabLayout$TabLayoutOnPageChangeListener.onPageSelected(TabLayout.java:1759)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.support.v4.view.ViewPager.dispatchOnPageSelected(ViewPager.java:1794)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:569)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:553)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:2102)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.View.dispatchTouchEvent(View.java:7396)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2462)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2195)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2177)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1482)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.app.Activity.dispatchTouchEvent(Activity.java:2470)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2125)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.View.dispatchPointerEvent(View.java:7581)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3571)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3503)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4614)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4592)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4696)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.os.MessageQueue.nativePollOnce(Native Method)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.os.MessageQueue.next(MessageQueue.java:125)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.os.Looper.loop(Looper.java:138)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at android.app.ActivityThread.main(ActivityThread.java:4947)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at java.lang.reflect.Method.invokeNative(Native Method)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at java.lang.reflect.Method.invoke(Method.java:511)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
12-04 11:32:12.402 11884-11884/com.sample.app.mysampleapp E/MessageQueue-JNI:     at dalvik.system.NativeStart.main(Native Method)
12-04 11:32:12.412 11884-11884/com.sample.app.mysampleapp D/AndroidRuntime: Shutting down VM
12-04 11:32:12.412 11884-11884/com.sample.app.mysampleapp W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x4131e2a0)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime: FATAL EXCEPTION: main
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime: java.lang.NullPointerException
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at com.sample.app.mysampleapp.MainActivity$1.onTabUnselected(MainActivity.java:53)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.support.design.widget.TabLayout.selectTab(TabLayout.java:867)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.support.design.widget.TabLayout$TabLayoutOnPageChangeListener.onPageSelected(TabLayout.java:1759)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.support.v4.view.ViewPager.dispatchOnPageSelected(ViewPager.java:1794)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:569)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:553)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:2102)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.View.dispatchTouchEvent(View.java:7396)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2462)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2195)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2177)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1482)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.app.Activity.dispatchTouchEvent(Activity.java:2470)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2125)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.View.dispatchPointerEvent(View.java:7581)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3571)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3503)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4614)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4592)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4696)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.os.MessageQueue.nativePollOnce(Native Method)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.os.MessageQueue.next(MessageQueue.java:125)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:138)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:4947)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
12-04 11:32:12.502 11884-11884/com.sample.app.mysampleapp E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)

有人可以帮我吗?非常感谢你。

2 个答案:

答案 0 :(得分:4)

好的,我想我现在明白了,您使用的是getIcon()方法,但实际上您从未通过setIcon()设置图标,因此您将在null上返回getIcon()。您正在使用getPageTitle()来显示图标,因此标签甚至不知道图标。

我建议您修改getPageTitle()以便始终返回null,然后在主要活动类中创建一个标签设置方法setupTabs(),它应该类似于:

private void setupTabs() {
    mTabLayout.getTabAt(0).setIcon(mTabIcons[0]);
    mTabLayout.getTabAt(1).setIcon(mTabIcons[1]);
    mTabLayout.getTabAt(2).setIcon(mTabIcons[2]);
    mTabLayout.getTabAt(2).setIcon(mTabIcons[3]);
}

同时将imageResId阵列从适配器移出到活动:

private int[] mTabIcons = {
    R.drawable.ic_zero
    R.drawable.ic_one,
    R.drawable.ic_two,
    R.drawable.ic_three
};

您还应该将setOnTabSelectedListener更改为使用TabLayout.ViewPagerOnTabSelectedListener,以便为您处理保持标签同步:

    tabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager) {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            super.onTabSelected(tab);
            tab.getIcon().setAlpha(255);
        }
        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
            super.onTabUnselected(tab);
            tab.getIcon().setAlpha(127);
        }
    });

然后,您可以在setupTabs()中致电onCreate()

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    // TabLayout
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.setupWithViewPager(mViewPager);

    // Set up our tab icons
    setupTabs();

    //This is what i'm trying to add
    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager) {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            super.onTabSelected(tab);
            tab.getIcon().setAlpha(255);
        }
        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
            super.onTabUnselected(tab);
            tab.getIcon().setAlpha(127);
        }
    });
}

答案 1 :(得分:0)

使用addOnTabSelectedListener

示例代码

tabLayout.addOnTabSelectedListener(object : OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab) {
                viewPager.setCurrentItem(tab.position)
            }

            override fun onTabUnselected(tab: TabLayout.Tab) {}
            override fun onTabReselected(tab: TabLayout.Tab) {}
        })