how to detecting a click on an already selected tab button

后端 未结 3 984
既然无缘
既然无缘 2021-01-02 14:21

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 ar

相关标签:
3条回答
  • 2021-01-02 14:53

    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);
                }
            });
    
    0 讨论(0)
  • 2021-01-02 14:56

    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);                                    
        }
    });
    
    0 讨论(0)
  • 2021-01-02 14:58

    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;
    }
    
    0 讨论(0)
提交回复
热议问题