Android TabWidget检测单击当前选项卡

0m4*_*m4r 36 android listener tabwidget android-tabhost

当此选项卡是当前选项卡时,我正试图找到能够在选项卡上触发onclick事件的方法.

我确实尝试过这种方式(在其他几种方式中)并没有成功.

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();
        }
    }   
}
Run Code Online (Sandbox Code Playgroud)

关键是我设置为onClickListener,null所以下次单击选项卡时没有任何反应,但我希望有正常的选项卡行为.

外面有什么想法吗?

Chi*_*CID 50

通过很多针对标签监听器的解决方案,我发现了非常简单的解决方案......

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

  }
});
Run Code Online (Sandbox Code Playgroud)

  • 这不能解决OP问题:只有在选项卡发生更改时才会调用此选项,而不是在单击相同选项卡时调用. (23认同)

Ste*_*man 24

经过大量的思考,解决方案最终比我想象的更容易.我所做的只是创建一个新的子类,它扩展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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

要使用新类而不是典型的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>
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助...

  • 很好的答案.我添加了`public interface OnReClickListener {public void onReClick(int index); }`an和ivar`invate OnReClickListener onReClickListener;`(加上getter和setter)以及这个代码来触发监听器:`if(null!= onReClickListener){onReClickListener.onReClick(index);}`希望有所帮助. (4认同)

0m4*_*m4r 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;
            }
        });
    }       
Run Code Online (Sandbox Code Playgroud)

可能有可能改进它,但这对我有用!


Lyu*_*rov 14

丑陋的修复:在onClickListener中: tabHost.SetCurrentTab(OtherTab); tabHost.SetCurrentTab(CurrentTab); 在其他选项卡的索引位置我在选项卡下使用我最简单的视图.

PS客户总是希望他们的应用程序不同:)

这是我使用的代码(我只有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);
                }
            } 
        });
Run Code Online (Sandbox Code Playgroud)


ElY*_*nte 5

这里的问题是,setOnTabChangedListener单击所选选项卡时不会触发,如果OnClickListener在选项卡上设置,则会丢失正常的选项卡行为.

因此,一个简单的解决方案是放在OnClickListener选项卡上,在其中,以编程方式设置这是当前选项卡.

TabHost:

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

TabLayout

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