TabActivity中子Activity相互跳转,及某个Tab需弹出窗的解决方案

这两天上论坛,发现有几个人问类似的问题,想想有空整理一个demo发上来。。。
现在正处在前个项目正要走,后一个项目赶着来的那么一点点空隙中。。。
这个是我项目中遇到的问题,也在论坛中发帖求助过,虽然没有得到完整的答案吧,不过还是有很多朋友提供思路了。在这里谢谢帮助过我的兄弟姐妹们了。

言归正传!
在iphone开发中貌似有个UITabBarController,(我以为是toolbar,四楼的兄弟更正的),UITabBarController在底部,也有对应的切换效果,都封装好了。但是在android的中,这个东西它在顶部。。。我也不明白为什么这么设计,标新立异?我觉得在底部方便很多,我们的设计也是这样设计的,所以我也只有改咯。
个人认为设计不太好的tabhost,单手拿手机不好操作,不过下面有个兄弟提醒因为有menu的存在,呵呵,我光想不方便了。

tabhost.png


整体的思想就是不进行startactivity,而是通过广播发送数据,发送之前切换到对应的tab。从而避免很多startactivity出现的麻烦,比如tabwidget不见了,跳转黑屏等等。。。
弹出窗可以获取到tabwidget对应的标签位置,设置其监听事件,阻塞掉原本tabwidget的切换就可以了。
先把这些tab挪到底部去。项目代码是在论坛里这个兄弟的基础上改的( 传送门),不想从项目里整理了,麻烦。谢谢这个兄弟了。布局代码如下:
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"

   android:id="@android:id/tabhost" android:layout_width="fill_parent"

     android:layout_height="fill_parent">

   <LinearLayout android:orientation="vertical"

    android:layout_width="fill_parent" android:layout_height="fill_parent">

   <FrameLayout android:id="@android:id/tabcontent"

      android:layout_width="fill_parent" android:layout_height="0dip"

      android:layout_weight="1" />

    <TabWidget android:id="@android:id/tabs"

      android:layout_width="fill_parent" android:layout_height="wrap_content"

        />

    </LinearLayout>

</TabHost>


注意每个控件的相对位置,位置对了,那个鬼东西才能跑底部去,自定义的tabhost就是如此了。。。
device.png
然后上代码。。。
public class TabManager extends TabActivity 

网上很多人说不要继承 tabactivity ,否则不能自定义 tabhost ,但在这就是这样做了,也没错。还是自己实践比较好,别盲目转帖。
5楼提醒的,多谢: 楼主误解了,其实网上说的是不能继承tabactivity,否则不能自定义tabhost名
你这边继承了Tabactivity, 同时布局文件中又用了android:id="@android:id/tabhost" ,所以你没错。你可以改下这个ID试试,一改就错了。
如果想要自定义这个ID,就不能继承TabActivity。
我也没试了,大家有空去试试。。。

Constant.tabHost = (TabHost) findViewById(android.R.id.tabhost);
LayoutInflater.from(this).inflate(R.layout.tabcontent,
Constant.tabHost.getTabContentView(), true);
tabWidget = Constant.tabHost.getTabWidget();
Constant.tabHost.addTab(Constant.tabHost.newTabSpec("tab1")
.setIndicator("Tab1",th.getResources().getDrawable(R.drawable.ic_menu_home_tab)).setContent(new Intent(this, Tab1.class)));


View v = tabWidget.getChildAt(i);
// 设置tab背景颜色 外层有个循环,每个tab都要设置一次
v.setBackgroundResource(R.drawable.tab_indicator);


tab_indicator文件:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@color/tab_unselected" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@color/tab_selected" />

<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@color/tab_focus" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@color/tab_focus" />

<!-- Pressed -->
<item android:state_pressed="true" android:drawable="@color/tab_press"/>
</selector>


View view = tabWidget.getChildAt(4);

view.setOnClickListener(new OnClickListener() {


获取到你要弹出窗口的标签,设置setOnClickListener事件,就可以阻塞掉tabhost原本的事件。达到弹出窗口的效果。
device1.png



Constant.tabHost = (TabHost) findViewById(android.R.id.tabhost);
Constant.tabHost.setCurrentTab(0);跳转就可以是这样做了。
Constant.tabHost 是全局变量,这个是关键,为了切换tab不会出现黑屏跳转效果。

txTextView.setText("点击切换到tab3");

txTextView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Constant.tabHost.setCurrentTab(2);//这里是全局变量的tabhost

Intent intent = new Intent("com.niushu.recbroad");
intent.putExtra("id","从tab1过来的牛叔");
Tab1.this.sendBroadcast(intent);
}
});


这个是在tab1中点击文字, 首先切换到对应的tab,Constant.tabHost.setCurrentTab(2);,启动对应的activity,然后发送广播。用这个方法Constant.tabHost.setCurrentTab切换,就没有那些乱七八糟的麻烦。
如果发送广播过去之后,接下来要用线程操作的话,最好在 建一个标志位,全局变量的。避免一些线程跟广播资源之间的冲突。


下面是接受广播的代码: 需要注意在生命周期方法中注册和注销广播接收器

public class Broad extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

disid_Broad = intent.getStringExtra("id");
Toast.makeText(Tab3.this, disid_Broad, 1).show();
Log.i("Broad", "onReceive");

}

}


device5.png


终于写完了,如果有什么不好的地方,请指教。。。欢迎回帖交流。。。
补充一下,还有些人碰到想替换子activity的问题,这个我也不知道。
我是用布局做的
按逻辑显示对应的布局。。。
如果想实现返回的效果,自己写个stack,每次弹出对应布局的id,显示出来就可以了。。。
哪位能知道如何替换activity,麻烦告知,谢谢。。。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值