标签更改监听器android

时间:2017-03-27 11:52:54

标签: android android-fragments android-tablayout

My HomeActivity扩展了使用2个标签的AppCompatActivity。

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);
    }

    ...

如何收听标签更改事件? 如果我需要添加更多代码以便清楚,请告诉我。

8 个答案:

答案 0 :(得分:37)

您可以使用OnTabChangeListener.See

    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) {

        }
    });
希望它有所帮助。

答案 1 :(得分:4)

使用ViewPager.onPageChangeListener

mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });
}

答案 2 :(得分:4)

您可以使用addOnTabSelectedListener方法:

tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
            if (tab.getPosition() == 0) {
                toolBarTitle.setText("Tab one");
            } else if (tab.getPosition() == 1) {
                toolBarTitle.setText("Tab two");
            } else {
                toolBarTitle.setText("Tab three");
            }
        }

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

        }

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

        }
    });

答案 3 :(得分:1)

来自Documentation

您需要实施TabLayout.OnTabSelectedListener

    public class HomeActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener

覆盖以下监听事件的方法:

 @Override
    public void onTabSelected(TabLayout.Tab tab) {
          int position = tab.getPosition();
    }

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

    }

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

    }

答案 4 :(得分:1)

您应该将OnTabChangeListener实施到TabActivity类,而不是选项卡的内容。

在TabActivity工具中OnTabChangeListener

然后设置TabHost mTabHost.setOnTabChangedListener(this);

的监听器

<强>实施例1,

@Override
public void onTabChanged(String tabId) {
    Log.i("selected tab ", tabId);

}

<强>例2

  public class HelloTabWidget extends TabActivity implements OnTabChangeListener{

private TabHost mTabHost;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Resources res = getResources(); 
    TabHost tabHost = getTabHost();  
    TabHost.TabSpec spec;  
    Intent intent; 
    mTabHost = getTabHost();


    intent = new Intent().setClass(this, BarActivity.class);
    spec = tabHost.newTabSpec("Name").setIndicator("Name",res.getDrawable(R.drawable.ic_tab_name)).setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, CityActivity.class);
    spec = tabHost.newTabSpec("city").setIndicator("City",res.getDrawable(R.drawable.ic_tab_city)).setContent(intent); 
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, MapsActivity.class);
    spec = tabHost.newTabSpec("Country").setIndicator("Country",res.getDrawable(R.drawable.ic_tab_map)).setContent(intent);
    tabHost.addTab(spec);        

    tabHost.setCurrentTab(2);
    mTabHost.setOnTabChangedListener(this);
}

public void onTabChanged(String tabId) {
    Toast.makeText(getApplicationContext(), "Selected Tab "+tabId, Toast.LENGTH_LONG).show();
    Log.i("selected tab index", "Current index - "+ mTabHost.getCurrentTab());      
}} 

答案 5 :(得分:0)

取决于你想要什么。如果您只想知道标签页是否更改,或找出选择了哪个标签位置,请使用:

mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
    public void onPageScrollStateChanged(int state) {

    }
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    public void onPageSelected(int position) {

    }
});

否则,如果你想跟踪标签本身,你必须像这样实施TabLayout.OnTabSelectedListener

public class HomeActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener

并将此方法添加到HomeActivity类:

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

}

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

}

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

}

答案 6 :(得分:0)

tab_layout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
           @Override
           public void onTabSelected(TabLayout.Tab tab) {

               viewpager.setCurrentItem(tab.getPosition());

               if (tab.getPosition() == 6 || tab.getPosition() == 11 )

                   if (mInterstitialAd.isLoaded()) {
                       mInterstitialAd.show();

                   }
           }

答案 7 :(得分:0)

kotlin 爱好者的标签更改侦听器

tab_selector_layout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener{
        override fun onTabSelected(tab: TabLayout.Tab?) {
            
        }

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

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

    })