本文介绍了标签更改侦听器android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 HomeActivity 扩展了使用 2 个选项卡的 AppCompatActivity.

My HomeActivity extends AppCompatActivity that uses 2 tabs.

public class HomeActivity extends AppCompatActivity {

    private SectionsPagerAdapter mSectionsPagerAdapter;
    private TabLayout tabLayout;
    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        ...
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);
    }

    ...

如何监听标签更改事件?让我知道是否需要添加更多代码以保持清晰.

How to listen to tab change event?Let me know if I need to add any more code for clarity.

推荐答案

可以使用OnTabChangeListener.见下

You can use OnTabChangeListener.See below

    TabLayout tabLayout = new TabLayout(this);
    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            //do stuff here
        }

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

        }

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

        }
    });

希望能帮到你.

这篇关于标签更改侦听器android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-28 18:52