2010-12-02 33 views
35

我想找到方法能夠觸發一個標籤上的onclick事件當這個標籤是當前選項卡。Android的TabWidget檢測點擊當前標籤

我確實嘗試過這種方式(其他幾種),但沒有成功。

public void onTabChanged(String tabId) { 
    Log.d(this.getClass().getName(), ">>>>>>>>>>>>>>>>>>>>>>>> tabId: " + tabId); 

    int tabs = getTabWidget().getChildCount(); 
    Log.d(this.getClass().getName(), "tabs: " + tabs); 
    for(int i=0; i<tabs; i++){ 
     View tab = getTabWidget().getChildAt(i); 
     if(i==tabHost.getCurrentTab()){ 
      Log.d(this.getClass().getName(), "tab: " + i); 
      tab.setOnClickListener(this); 
     }else{ 
      tab.setOnClickListener(null); 
      tab.getOnFocusChangeListener(); 
     } 
    } 
} 

的一點是,我設置了onClickListenernull所以,下一次我點擊一個標籤什麼也沒有發生,但我想有正常標籤的行爲。

外面有什麼想法嗎?

+0

「我試圖找到方法能夠在此選項卡是當前選項卡上觸發選項卡上的onclick事件。」 - 這不是一種非常容易發現的UI模式。用戶不希望在當前選項卡上點按任何內容。我鼓勵你重新考慮你的方法。 – CommonsWare 2010-12-02 18:03:01

+3

我同意你的意見,我的客戶不會收到:/ – 0m4r 2010-12-03 07:16:41

+0

我有類似的情況,每個標籤都有用戶的說明,我希望他們能夠再次單擊標籤,以便再次顯示說明。我想這是一個不尋常的模式,但它是我認爲唯一可以輕鬆查看的方向 – 2011-09-20 22:05:46

回答

20

我想我已經找到了解決辦法,這裏有個示例代碼:

intent = new Intent(this, HomeGroup.class); 
    View tab1 = _inflater.inflate(R.layout.custom_tab_1,null); 
    homeTab.setTag("Tab1"); 
    spec = tabHost.newTabSpec("Tab1").setIndicator(tab1).setContent(intent); 
    tabHost.addTab(spec); 

    View tab2 = _inflater.inflate(R.layout.custom_tab_2,null); 
    homeTab.setTag("Tab2"); 
    spec = tabHost.newTabSpec("Tab2").setIndicator(tab2).setContent(intent); 
    tabHost.addTab(spec); 

    View tab3 = _inflater.inflate(R.layout.custom_tab_3,null); 
    homeTab.setTag("Tab3"); 
    spec = tabHost.newTabSpec("Tab3").setIndicator(tab3).setContent(intent); 
    tabHost.addTab(spec); 

    tabHost.setOnTabChangedListener(this); 

    //click on seleccted tab 
    int numberOfTabs = tabHost.getTabWidget().getChildCount(); 
    for(int t=0; t<numberOfTabs; t++){ 
     tabHost.getTabWidget().getChildAt(t).setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       if(event.getAction()==MotionEvent.ACTION_UP){ 

        String currentSelectedTag = MainTab.this.getTabHost().getCurrentTabTag(); 
        String currentTag = (String)v.getTag(); 
        Log.d(this.getClass().getSimpleName(), "currentSelectedTag: " + currentSelectedTag + " currentTag: " + currentTag); 
        if(currentSelectedTag.equalsIgnoreCase(currentTag)){ 
         MainTab.this.getTabHost().setCurrentTabByTag(currentTag); 
         String newSelectedTabTag = MainTab.this.getTabHost().getCurrentTabTag(); 
         if(newSelectedTabTag.toLowerCase().indexOf("tab1")!=-1){ 
          //do smthg 
         }else if(newSelectedTabTag.toLowerCase().indexOf("tab1")!=-1){ 
          //do smthg 
         }else if(newSelectedTabTag.toLowerCase().indexOf("tab3")!=-1){ 
          //do smthg 
         } 
         return true; 
        } 
       } 
       return false; 
      } 
     }); 
    }  

也許有可能改善它,但這對我來說可行!

14

醜陋的修復:在onClickListener放: tabHost.SetCurrentTab(OtherTab); tabHost.SetCurrentTab(CurrentTab); 其他選項卡的索引我在選項卡下使用我最簡單的視圖。

P.S.客戶總是希望他們的應用程序是不同的:)

這是我使用的(我只有2個選項卡TAB1和TAB2)代碼:

getTabWidget().getChildAt(1).setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       Log.d(TAG,"1"+getTabHost().getCurrentTabTag()); 

       if (getTabHost().getCurrentTabTag().equals("Tab2")) { 
        Log.d(TAG,"2"); 

        tabHost.setCurrentTab(0);          
        tabHost.setCurrentTab(1); 

       } else { 
        tabHost.setCurrentTab(1); 
       } 
      } 
     }); 
+0

你是什麼意思與「其他標籤索引我在標籤下使用我最簡單的視圖。」? – 0m4r 2010-12-04 18:11:26

+0

我一直在嘗試你的解決方案,但我仍然有* overrriding *選項卡上的默認onClick事件*覆蓋*默認行爲...並將其設置爲null刪除onClick事件......所以,感謝您的幫助,但我認爲我不能使用您的解決方案,除非我重寫了該標籤的整個onClick事件:/ – 0m4r 2010-12-05 11:35:54

+0

我已經添加了一些示例代碼。 – 2010-12-10 09:17:01

50

gothrough的標籤聽衆許多解決方案之後,我發現很簡單的解決方案...

getTabHost().setOnTabChangedListener(new OnTabChangeListener() { 

@Override 
public void onTabChanged(String tabId) { 

int i = getTabHost().getCurrentTab(); 
Log.i("@@@@@@@@ ANN CLICK TAB NUMBER", "------" + i); 

    if (i == 0) { 
      Log.i("@@@@@@@@@@ Inside onClick tab 0", "onClick tab"); 

    } 
    else if (i ==1) { 
      Log.i("@@@@@@@@@@ Inside onClick tab 1", "onClick tab"); 
    } 

    } 
}); 
23

很多思考這樣的後,溶液結束了,比我想象中的簡單。我所做的只是創建一個擴展TabHost一個新的子類,並重寫setCurrentTab方法,像這樣:

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

要改用典型TabHost的新類只需編輯與您的佈局xml文件:

<FrameLayout 
    android:layout_height="match_parent" 
    android:layout_width="0dip" 
    android:layout_weight=".8"> 

    <com.myCompany.Views.ReclickableTabHost 
     android:id="@android:id/tabhost" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:visibility="gone"> 

     <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 

      <TabWidget 
       android:id="@android:id/tabs" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:background="@drawable/tab_unselected_holo"/> 
      <FrameLayout 
       android:id="@android:id/tabcontent" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"> 
      </FrameLayout> 

     </LinearLayout> 

    </com.myCompany.Views.ReclickableTabHost> 

希望這有助於...

1
mTabHost.setOnTabChangedListener(new OnTabChangeListener() { 

     @Override 
     public void onTabChanged(String tabId) { 

      int i = mTabHost.getCurrentTab(); 
      mTabHost.getTabWidget().getChildAt(i) 
        .setBackgroundColor(Color.parseColor("#014a68")); 

      //int m = mTabHost.getChildCount(); 
      for (int j = 0; j <=3; j++) { 
       if (j != i) 
        mTabHost.getTabWidget() 
          .getChildAt(j) 
          .setBackgroundColor(Color.parseColor("#000000")); 
      } 


     } 
    }); 
5

這裏的問題是,setOnTabChangedListener不選定的選項卡上單擊時如果設置火了,在選項卡上,您將失去正常的選項卡行爲。

所以一個簡單的解決方案是將OnClickListener放在選項卡上,並在其內部以編程方式設置這是當前選項卡。

隨着TabHost

getTabWidget().getChildAt(0).setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     // custom code 
     tabHost.setCurrentTab(0);          
    } 
}); 

隨着TabLayout

tabLayout.getTabAt(0)setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
        // custom code 
        TabLayout.Tab tab = tabLayout.getTabAt(0); 
        tab.select(); 
       } 
      } 
     }); 
3

如果你想嘗試android.support.design.widget。TabLayout,就可以實現這樣的:

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

    } 

    @Override public void onTabUnselected(Tab tab) { 

    } 

    @Override public void onTabReselected(Tab tab) { 

    } 
}); 
0

如果你有一個自定義標籤佈局,您可能需要在您的自定義視圖中添加這種,它解決了我的問題:

android:duplicateParentState="true" 
0

這是爲我工作......

TabHost host = (TabHost)findViewById(R.id.tabHost); 

host.setOnTabChangedListener(new OnTabChangeListener() { 
    @Override 
    public void onTabChanged(String tabId) { 

     int i = host.getCurrentTab(); 

     if (i == 0) { 
      // your method 1 
     } 
     else if (i ==1) { 
      // your method 2 
     } 
    } 
});