关于TabHost切换的时候刷新(更新)页面的问题

今天应老板的要求在却换TabHost的item的时候需要实现即时刷新,即每进入一个activity里面都必须重新更新ui,刚开始一听我就认为很简单,到做的时候才发现原来不走OnResume()生命周期,好家伙可把我给坑惨了,我在老板面前说分分钟搞定,哎,没办法继续研究代码看能不能解决呗天无绝人之路,总算让我找到方法实现这个功能了,没在老板面前丢脸啊。

首先是这个方法写在TabHostActivity里面:tabHost.setOnTabChangedListener(new OnTabChangeListener(){
       @Override
       public void onTabChanged(String tabId) {
           // TODO Auto-generated method stub
        if(tabId.equals("tab1")){
       
        }else if(tabId.equals("tab2")){
       
        }else if(tabId.equals("tab3")){
        sendBroadcast(new Intent("cn.etzmico.broadcastreceiverregister.SENDBROADCAST"));  
        }else if(tabId.equals("tab4")){
       
        }
       }
        
   });



其次就是在你需要更新的activity里面注册一个动态广播:

private BroadcastReceiver myReceiver = new BroadcastReceiver() {    
        @Override  
        public void onReceive(Context context, Intent intent) {  
//            Toast.makeText(context, "myReceiver receive", Toast.LENGTH_SHORT)  
//                    .show(); 

   //更新ui的方法
            initData();
        }  
  
    };  



好了就是这么简单,但却让我忙活了一下午啊!!!,有不明白的可以给我留言,我经常在线!!!!!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
TabHostAndroid 中的一个容器,用于实现多个标签页之间的切换。下面是 TabHost 的使用方法详解: 1. 在 XML 布局文件中添加 TabHost 控件: ```xml <TabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout> </TabHost> ``` 2. 在 Activity 中初始化 TabHost: ```java TabHost tabHost = findViewById(android.R.id.tabhost); tabHost.setup(); ``` 3. 添加标签页: ```java TabHost.TabSpec tabSpec1 = tabHost.newTabSpec("Tab1"); tabSpec1.setIndicator("Tab 1"); tabSpec1.setContent(R.id.tab1_content); TabHost.TabSpec tabSpec2 = tabHost.newTabSpec("Tab2"); tabSpec2.setIndicator("Tab 2"); tabSpec2.setContent(R.id.tab2_content); tabHost.addTab(tabSpec1); tabHost.addTab(tabSpec2); ``` 4. 创建标签页的布局: 在布局文件中创建用于显示每个标签页内容的 View,例如: ```xml <LinearLayout android:id="@+id/tab1_content" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- 在这里添加 Tab 1 的内容 --> </LinearLayout> <LinearLayout android:id="@+id/tab2_content" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- 在这里添加 Tab 2 的内容 --> </LinearLayout> ``` 以上就是 TabHost 的基本使用方法。通过添加不同的标签页和对应的布局,可以实现多个标签页之间的切换和显示不同的内容。你可以根据自己的需求来定制标签页的样式和内容。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值