Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 493|回复: 0

Android TabHost设置setCurrentTab(index),当index!=0时,默认加载第一个tab问题解决方法。

[复制链接]
  • TA的每日心情
    奋斗
    前天 12:49
  • 签到天数: 789 天

    [LV.10]以坛为家III

    2049

    主题

    2107

    帖子

    72万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    722638
    发表于 2021-4-30 17:09:14 | 显示全部楼层 |阅读模式

    最近在用TabHost,默认希望显示第2个tab,发现总是加载第三个tab的同时加载第一个,解决方法如下:

     1、首先查看addTab(TabSpec tabSpec)源代码:

    /**
         * Add a tab.
         * @param tabSpec Specifies how to create the indicator and content.
         */
        public void addTab(TabSpec tabSpec) {
    
            if (tabSpec.mIndicatorStrategy == null) {
                throw new IllegalArgumentException("you must specify a way to create the tab indicator.");
            }
    
            if (tabSpec.mContentStrategy == null) {
                throw new IllegalArgumentException("you must specify a way to create the tab content");
            }
            View tabIndicator = tabSpec.mIndicatorStrategy.createIndicatorView();
            tabIndicator.setOnKeyListener(mTabKeyListener);
    
            // If this is a custom view, then do not draw the bottom strips for
            // the tab indicators.
            if (tabSpec.mIndicatorStrategy instanceof ViewIndicatorStrategy) {
                mTabWidget.setStripEnabled(false);
            }
            mTabWidget.addView(tabIndicator);
            mTabSpecs.add(tabSpec);
    
            if (mCurrentTab == -1) {
                setCurrentTab(0);
            }
        }
    

      

     发现当我们进行addTab操作时,默认执行了最后一步,设置了第一个tab,所以我们需要bamCurrentTab的值设置为不为-1的一个数,且大于0。

    2、再看setCurrentTab(int index)方法源码:

    public void setCurrentTab(int index) {
            if (index < 0 || index >= mTabSpecs.size()) {
                return;
            }
    
            if (index == mCurrentTab) {
                return;
            }
    
            // notify old tab content
            if (mCurrentTab != -1) {
                mTabSpecs.get(mCurrentTab).mContentStrategy.tabClosed();
            }
    
            mCurrentTab = index;
            final TabHost.TabSpec spec = mTabSpecs.get(index);
    
            // Call the tab widget's focusCurrentTab(), instead of just
            // selecting the tab.
            mTabWidget.focusCurrentTab(mCurrentTab);
    
            // tab content
            mCurrentView = spec.mContentStrategy.getContentView();
    
            if (mCurrentView.getParent() == null) {
                mTabContent.addView(mCurrentView, new ViewGroup.LayoutParams(
                                        ViewGroup.LayoutParams.MATCH_PARENT,
                                        ViewGroup.LayoutParams.MATCH_PARENT));
            }
    
            if (!mTabWidget.hasFocus()) {
                // if the tab widget didn't take focus (likely because we're in touch mode)
                // give the current tab content view a shot
                mCurrentView.requestFocus();
            }
    
            //mTabContent.requestFocus(View.FOCUS_FORWARD);
            invokeOnTabChangeListener();
        }
    

      

    当mCurrentTab不为-1的时候会执行mTabSpecs.get(mCurrentTab).mContentStrategy.tabClosed()操作,所以在我们执行setCurrentTab()方法之前,我们再把mCurrentTab的值恢复为-1,这样就不会执行关闭操作导致空指针异常。

    3、具体方法如下:

     //取消tabhost默认加载第一个tab。
            try
            {
                Field current = tabHost.getClass().getDeclaredField("mCurrentTab");
                current.setAccessible(true);
                current.setInt(tabHost, 0);
            }catch (Exception e){
                e.printStackTrace();
            }
    
            TabHost.TabSpec tSpecCoupon = tabHost.newTabSpec("sth");
            tSpecCoupon.setIndicator(tabIndicator1);
            tSpecCoupon.setContent(new DummyTabContent(getBaseContext()));
            tabHost.addTab(tSpecCoupon);
    
            //mCurrentTab恢复到-1状态
            try
            {
                Field current = tabHost.getClass().getDeclaredField("mCurrentTab");
                current.setAccessible(true);
                current.set(tabHost, -1);
            }catch (Exception e){
                e.printStackTrace();
            }
    

      

    到此,我们屏蔽了默认的setCurrentTab(0)操作,同时恢复为-1后,又执行了我们的setCurrentTab(1)操作。

    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-9-8 06:35 , Processed in 0.063063 second(s), 29 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表