如何从片段中以编程方式在Android中切换标签页? [英] How to switch tabs programmatically in Android from fragment?

查看:62
本文介绍了如何从片段中以编程方式在Android中切换标签页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了扩展FragmentActivityTabActivity.它有5个选项卡,每个选项卡都是Fragment.我正在寻找的是以编程方式在选项卡之间切换.

I have implemented a TabActivity which extends FragmentActivity. It has 5 tabs each tab is a Fragment. What i am looking for is to switch between the tabs programmatically.

例如:如果我在tab4中.在按钮上单击我要从tab4移到tab1.尝试了很多,但找不到解决方案.

For eg: if i am in tab4. On button click I want to move from tab4 to tab1. Tried a lot but couldn't find the solution for this.

尝试以下操作,但无济于事.

Tried with the following but it doesn't help.

来自SecondTab

public void switchTabInActivity(String value){
    FirstTab parent;
    parent = (FirstTab) getActivity().getParent();
    parent.switchTab(value);
}

TabActivity

  /** To Change Tab*/
public void switchTab(String tabno){ 
    this.onTabChanged(tabno);
}

推荐答案

最后,我可以使用以下代码行从Fragments中以编程方式在选项卡之间切换

Finally i can switch between the tabs programatically from Fragments using the following line of code

  TabHost host = (TabHost) getActivity().findViewById(android.R.id.tabhost);
  host.setCurrentTab(2);

希望对您有所帮助.

这篇关于如何从片段中以编程方式在Android中切换标签页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆