how to detecting a click on an already selected tab button

主宰稳场 提交于 2019-11-29 19:08:14

问题


I am trying to get the Click event when clicking on currently selected tab of my TabActivity.

I tried below code but when i click on one tab the other tabs are not working/clicking properly.

    setupTab(new TextView(this), "Map");
    setupTab(new TextView(this), "Attacks");
    setupTab(new TextView(this), "Profile");
    setupTab(new TextView(this), "Headquater");

    int numberOfTabs = tabHost.getTabWidget().getChildCount();
    for(int t=0; t<numberOfTabs; t++){
    getTabWidget().getChildAt(t).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if(tabHost.getCurrentTab() == 0){
                    Toast.makeText(TabContext, ""+"i m in on clickkkkk" ,1500).show();
                    getTabHost().setCurrentTab(0);
                }
                if(tabHost.getCurrentTab() == 1){
                    Toast.makeText(TabContext, ""+"i m in on clickkkkk....$#@$#$" ,1500).show();
                    getTabHost().setCurrentTab(1);
                }

            }
        });
    }

回答1:


I needed to detect second click only for one tab, so this answer worked for me. The trick is setting the OnClick for the child and not for the TabHost itself.

getTabWidget().getChildAt(0).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.i("TAG", "Clicked tab : "+getTabHost().getCurrentTabTag());
        tabHost.setCurrentTab(0);                                    
    }
});



回答2:


I used theczechsensation solution but with minor modifications because adding a listener to child and not to the tab host itself will cause confusion to tabhost listener to read comments in the code for better classifications

 mTabHost.getTabWidget().getChildAt(0).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // re enforce selecting targeted tab to re apply main listener to default behaviour
                mTabHost.setCurrentTab(0);
                // display current tab tag in console
                Log.i("MainActivity", "Clicked tab : "+mTabHost.getCurrentTabTag());
                // identify targeted fragment
                Fragment currentFragment = new "FragmentClassName"();
                // execute navigation (fragment transaction)
                fragmentManager.beginTransaction()
                        .replace(R.id.content_frame, currentFragment)
                        .commit();
                // re enforce selecting targeted tab to re apply main listener to default behaviour
                mTabHost.setCurrentTab(0);
            }
        });



回答3:


Inside your onCreate method:

for(int i = 0; i < TOTAL_TAB; i++) {  
    tabHost.getTabWidget().getChildAt(i).setOnTouchListener(this);
    tabHost.getTabWidget().getChildAt(i).setTag(i+"");  
}  

your listener:
@Override

public boolean onTouch(View v, MotionEvent event) {
    if(MotionEvent.ACTION_DOWN == event.getAction()) {
        previousTab = tabHost.getCurrentTab();
        currentTab = Integer.parseInt((String)v.getTag());
        if(previousTab == currentTab) {
            if(currentTab == 0){
                Log.i("log", "0th same tab selected");
            }else if(currentTab == 1){
                Log.i("log", "1st same tab selected");
            }else if(currentTab == 2){
                Log.i("log", "2nd same tab selected");
            }else if(currentTab == 3){
                Log.i("log", "3rd same tab selected");
            }
            return true;
        }
    }
    return false;
}


来源:https://stackoverflow.com/questions/9372851/how-to-detecting-a-click-on-an-already-selected-tab-button

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