Android TabWidget detect click on current tab

前端 未结 9 2106
庸人自扰
庸人自扰 2020-11-28 05:34

I am trying to find way to be able to fire an onclick event on a tab when this tab is the current tab.

I did try this way (among several other) with no success thou

9条回答
  •  粉色の甜心
    2020-11-28 06:35

    After a lot of thinking about this, the solution ended up being easier than I thought. What I did was just create a new child class that extends TabHost and override the setCurrentTab method like so:

    package com.mycompany.Views;
    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.widget.TabHost;
    import android.widget.Toast;
    
    public class ReclickableTabHost extends TabHost {
    
        public ReclickableTabHost(Context context) {
            super(context);
        }
    
        public ReclickableTabHost(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        public void setCurrentTab(int index) {
            if (index == getCurrentTab()) {
                // FIRE OFF NEW LISTENER
            } else {
                super.setCurrentTab(index);
            }
        }
    }
    

    To use your new class instead of the typical TabHost just edit your layout xml file with:

    
    
        
    
            
    
                
                
                
    
            
    
        
    

    Hope this helps...

提交回复
热议问题