OnClickListener on Tabs not working

血红的双手。 提交于 2019-12-17 15:46:43

问题


Greetings,

I am trying to get the Click - event when clicking on the currently selected tab of my TabActivity. The onTabChangedHandler is only called whenever the tab is changed, not if the currently active Tab is clicked. The debugger tells me i have the onClickListener Registered for the TabWidget within my TabHost.

Am i registering for the wrong View?

Also, I am unable to create a Context Menu for the Tabs, only for its content, is this problem related?

public class TestDroidViewTab extends TabActivity 
                              implements TabContentFactory
                              , OnTabChangeListener, OnClickListener {

  private static final String LOG_KEY = "TEST";
  ListView listView;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      final TabHost tabHost = getTabHost();


      TabHost.TabSpec ts = tabHost.newTabSpec("ID_1");
      ts.setIndicator("1"); 
      ts.setContent(this);
      tabHost.addTab(ts);

      ts = tabHost.newTabSpec("ID_2");
      ts.setIndicator("2"); 
      ts.setContent(this);
      tabHost.addTab(ts);

      ts = tabHost.newTabSpec("ID_3");
      ts.setIndicator("3"); 
      ts.setContent(this);
      tabHost.addTab(ts);
      tabHost.setOnClickListener(this);
      tabHost.setOnTabChangedListener(this);
  }
  public void onClick(View v) {
      Log.d(LOG_KEY, "OnClick");
  }

  public void onTabChanged(String tabId) {
      Log.d(LOG_KEY, "OnTabChanged");
  }

回答1:


If you want to see that a particular tab is clicked, you need to add your listener to the tab itself, not the TabHost.

The hierarchy of views in a tab implementation is:

  • TabHost
    • TabWidget
      • (tab)
      • (tab)
    • FrameLayout

The tabs are added at runtime by calling: tabHost.addTab(tabHost.newTabSpec(""));

You can then get a handle to the individual tabs by calling: getTabWidget().getChildAt(4);

In essence, you are adding your OnClickListener to a child of the TabWidget. You can now pick up the clicks on your individual tab. However, this will override the default behavior which changes the content when a tab is clicked. So, to get your content to change, your OnClickListener will need to do that for you.

Here is a full example, which lets you intercept the click event, and change the content below the tab:

final String myTabTag = "My Tab";
final int myTabIndex = 3;

getTabHost().addTab( getTabHost().newTabSpec(myTabTag) );

getTabWidget().getChildAt(myTabIndex).setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        if (getTabHost().getCurrentTabTag().equals(myTabTag)) {
            getTabHost().setCurrentTab(myTabIndex );
        }
    }
});



回答2:


use setOnTabChangedListener instead of OnClickListener ;)

    static TabHost tabHost;

    tabHost = getTabHost();


    tabHost.setOnTabChangedListener(new OnTabChangeListener() {
       @Override
      public void onTabChanged(String arg0) {
       Log.i("******Clickin Tab number ... ", "" + tabHost.getCurrentTab());
      }     
});  



回答3:


Your clause is wrong, use:

...

if (getTabHost().getCurrentTabTag().equals(myTabTag) == false) {
            getTabHost().setCurrentTab(myTabIndex );
   }

...




回答4:


into my code, it shows some errors and ask me to create new methods in those names like getTabWidget(), getTabHost(), etc. Waiting for your response.

Try this

 tabHost.getTabHost().setCurrentTab(myTabIndex);


来源:https://stackoverflow.com/questions/1062476/onclicklistener-on-tabs-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!