在 Android Tabview 中捕获选项卡单击事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3437414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Capturing the tab click event in Android Tabview
提问by Sudar
I have a tabview in my android app with 3 tabs. The tabs are all working fine.
我的 android 应用中有一个 tabview,有 3 个标签。选项卡都工作正常。
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?
是否可以在 tabview 中捕获选项卡单击事件并执行一些额外的逻辑?
Edit: Edited for clarity.
编辑:为清楚起见进行了编辑。
采纳答案by 0m4r
If you ares till looking for a solution, I might have found one. Take a look here: Android TabWidget detect click on current tab
如果您一直在寻找解决方案,我可能已经找到了。看看这里:Android TabWidget检测当前标签上的点击
回答by 0m4r
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
}
});
回答by Vikas
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
1:在你的类中扩展TabActivity。2:在onResume()方法中实现如下方法
For each tab (i) implement this:
对于每个选项卡 (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)
}
});
}
Since we always have a fixed number of tabs, implementing it separately is not a problem.
由于我们总是有固定数量的选项卡,单独实现它不是问题。
回答by efeyc
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,
而 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;
}