给tabhost加上点击监听,不是onTabChanged(String)监听

   最近在项目中遇到点击tabhost里的tab项时做出监听,注意不是onTabChanged(String str)的监听,因为该监听是只有当你切换tab时才会发生动作事件。

   TabHost的使用:

1:首先定义tabhost的布局

 <TabHost
            android:id="@android:id/tabhost"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
             >  

<LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

<TabWidget
                        android:id="@android:id/tabs"
                        android:layout_width="wrap_content"
                        android:layout_height="50dp"
                         />

FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:background="@android:color/transparent"
                   />

  </LinearLayout>

  </TabHost>


这样你有了布局,

接下来,让你的类继承TabActivity,然后定义成员变量:

 private TabHost mTabHost;

 private TabWidget mTabWidget;


接着在OnCreate()里面进行初始化,

  mTabHost = getTabHost();
  mTabHost.setup(getLocalActivityManager());
  mTabWidget = mTabHost.getTabWidget();


然后进行setTab1(),setTab2()······

mTabHost.setOnTabChangedListener(this);
     
setTab(TAB_1, true);
//默认设置第一个选项卡

setTab1()方法就是创建新的标签tab代码如下:

 

private void setTab1() {
  View view = mInflater.inflate(R.layout.xxx, null);
  ((TextView) view.findViewById(R.id.x)).setText(getResources()
    .getString(R.string.tab_forum_1));
  Intent newsList = new Intent(this, AAAivity.class);
  TabSpec mTabSpec1 = mTabHost.newTabSpec(getResources().getString(
    R.string.tab_forum_1));
  mTabSpec1.setIndicator(view);
  mTabSpec1.setContent(newsList);
  mTabHost.addTab(mTabSpec1);
 }

setTab(TAB_1,true);方法其中里面的TAB_1=1;

该方法为:监听测试为点击第二个tab选项卡时出现点击事件。

private void setTab(int id, boolean flag) {
  switch (id) {
  case TAB_1:

   mTabWidget.getChildAt(TAB_1).setBackgroundResource(R.drawable.select_group_bg2);

  abWidget.getChildAt(TAB_2).setBackgroundDrawable(null);

case TAB_2:
   mTabWidget.getChildAt(TAB_2).setOnClickListener(new OnClickListener() {   
    @Override
    public void onClick(View v) {
      if (mTabHost.getCurrentTab() != TAB_2) {//一定要判断这个是为了防止阻碍切换事件
                         mTabHost.setCurrentTab(TAB_2);
                     }else{

                     //做你要做的事情
                       }
    }
   });
   mTabWidget.getChildAt(TAB_1).setBackgroundDrawable(null);

}

好了,接着写onTabChanged(String tabId)方法

  

 if (tabId.equalsIgnoreCase(getResources().getString(
    R.string.tab_forum_1))) {
   setTab(TAB_1, true);
  } else if (tabId.equalsIgnoreCase(getResources().getString(
    R.string.tab_forum_2))) {
   setTab(TAB_2, true);

}

}


好了监听已经实现。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值