Android Tabview中捕获tab点击事件

发布于 2024-09-13 02:38:43 字数 251 浏览 3 评论 0原文

我的 Android 应用程序中有一个带有 3 个选项卡的选项卡视图。选项卡都工作正常。

现在,我想在单击当前活动选项卡的选项卡(顶部)时执行一些附加逻辑。

这是一个示例:

在我的一个选项卡中,我为用户提供了一个选项,可以按不同的顺序对内容进行排序。当按下当前活动选项卡的选项卡时,我想重置所有这些排序。

是否可以捕获 tabview 中的选项卡单击事件并执行一些额外的逻辑?

编辑:为了清晰起见进行了编辑。

I have a tabview in my android app with 3 tabs. The tabs are all working fine.

Now I want to perform some additional logic when the tab (on the top) of the currently active tab is clicked.

Here is an example:

In one of my tabs, I provide an option for the user to sort things in different order. When the press the tab of the currently active tab, I want to reset all these sorting.

Is it possible to capture the tab click event in tabview and perform some additional logic?

Edit: Edited for clarity.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

莫相离 2024-09-20 02:38:43

这就是您的代码应该如何工作:

getTabWidget().getChildAt(getTabHost().getCurrentTab()).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
        //do whatever you need

        }
});

This is how your code should work :

getTabWidget().getChildAt(getTabHost().getCurrentTab()).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
        //do whatever you need

        }
});
野味少女 2024-09-20 02:38:43

如果您还在寻找解决方案,我可能已经找到了。
看一下这里: ​​Android TabWidget 检测点击当前选项卡

If you ares till looking for a solution, I might have found one.
Take a look here: Android TabWidget detect click on current tab

凶凌 2024-09-20 02:38:43

我找到了一种干净且简单的解决方案来检测对选定选项卡

步骤的点击:

1:在班级中扩展 TabActivity。
2:在 onResume() 方法中实现以下方法

对于每个选项卡 (i) 实现此方法:

TabHost tabHost = getTabHost();

public void onResume() {

 super.onResume();
     tabHost.getTabWidget().getChildAt(0).setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                   count++;
                   tabHost.setCurrentTab(0);
    //based on your count value..you can do anything...like going back to homepage...
    //    similar thing exist on iphone (double tab a tab..it takes back to homepage)
     }
   });
 }      

由于我们总是有固定数量的选项卡,因此单独实现它不是问题。

I found one clean and easy solution for Detecting clicks on selected Tab

Steps:

1: Extend TabActivity in your class.
2: In the onResume() method implement the following method

For each tab (i) implement this:

TabHost tabHost = getTabHost();

public void onResume() {

 super.onResume();
     tabHost.getTabWidget().getChildAt(0).setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                   count++;
                   tabHost.setCurrentTab(0);
    //based on your count value..you can do anything...like going back to homepage...
    //    similar thing exist on iphone (double tab a tab..it takes back to homepage)
     }
   });
 }      

Since we always have a fixed number of tabs, implementing it separately is not a problem.

荒人说梦 2024-09-20 02:38:43
    for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
    {
        getTabWidget().getChildAt(i).setOnClickListener(new OnClickListener() { 

            @Override 
            public void onClick(View v) { 

                if (getTabHost().getCurrentTabTag().equals(v.getTag()))
                {
                    int nextTab = getTabHost().getCurrentTab();
                    tabHost.setCurrentTab(prevTab);
                    tabHost.setCurrentTab(nextTab);
                    prevTab = nextTab;
                }
                else
                    tabHost.setCurrentTabByTag((String) v.getTag());
            } 
        });
    }

你需要一个全局变量;

    private int prevTab = 1;   //any tab except the initial one.

这段代码对我有用。有点丑陋的是你必须为选项卡和视图设置相同的标签
例如;

    intent = new Intent().setClass(this, AnaSayfa.class);
    spec = tabHost.newTabSpec("firstTab").setIndicator(makeTabIndicator(R.drawable.firstTab, "First Tab" , "firstTab"))
                  .setContent(intent);
    tabHost.addTab(spec);

makeTabIndicator 方法是这样的,

    private View makeTabIndicator(int drawable, String text, String viewTag){ 

        View view = LayoutInflater.from(this).inflate(R.layout.tab_layout, null);

        ImageView image = (ImageView) view.findViewById(R.id.imageView1);       
        image.setImageResource(drawable);
        image.setAdjustViewBounds(true);

        TextView tv = (TextView) view.findViewById(R.id.textView1);
        tv.setText(text);

        view.setTag(viewTag);               
        return view;
    }
    for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
    {
        getTabWidget().getChildAt(i).setOnClickListener(new OnClickListener() { 

            @Override 
            public void onClick(View v) { 

                if (getTabHost().getCurrentTabTag().equals(v.getTag()))
                {
                    int nextTab = getTabHost().getCurrentTab();
                    tabHost.setCurrentTab(prevTab);
                    tabHost.setCurrentTab(nextTab);
                    prevTab = nextTab;
                }
                else
                    tabHost.setCurrentTabByTag((String) v.getTag());
            } 
        });
    }

You need a global variable;

    private int prevTab = 1;   //any tab except the initial one.

This code works for me. A little ugly thing is you must set same tag for tab and view
For example;

    intent = new Intent().setClass(this, AnaSayfa.class);
    spec = tabHost.newTabSpec("firstTab").setIndicator(makeTabIndicator(R.drawable.firstTab, "First Tab" , "firstTab"))
                  .setContent(intent);
    tabHost.addTab(spec);

and makeTabIndicator method is like that,

    private View makeTabIndicator(int drawable, String text, String viewTag){ 

        View view = LayoutInflater.from(this).inflate(R.layout.tab_layout, null);

        ImageView image = (ImageView) view.findViewById(R.id.imageView1);       
        image.setImageResource(drawable);
        image.setAdjustViewBounds(true);

        TextView tv = (TextView) view.findViewById(R.id.textView1);
        tv.setText(text);

        view.setTag(viewTag);               
        return view;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文