FragmentTabHost 阻止切换

来源:互联网 发布:python是开源的吗 编辑:程序博客网 时间:2024/06/07 08:17

底部标签栏 和页面的切换  

有好几种做法

1: 自己布局 拼凑底部导航的  和 fragment一起使用   缺点:底部导航的布局的的自己话 费时间   有点 自己我控制灵活

2:FragmentTabHost+Fragment  这个很方便  我知道这个之后 一般都是用的这个  优点当然是方便了 缺点 我开始发现是 不灵活 比如一个按钮只有在登陆之后才能被出现 我开始用的这个  这个只有一个onTabChanged 这个监听 这个是切换完了 才执行   效果不理想 

但是  今天解决了 也没发现别的缺点 这里说说吧   不过总体还是 每自己拼凑的灵活啦  


因为不知道 怎么去监听切换之前的动作  只能跟着源码进去 看看了   只看和tabhost有关系的

   mTabHost.addTab(tabSpec, fragmentArray[i],null);  


    public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {        tabSpec.setContent(new DummyTabFactory(mContext));        String tag = tabSpec.getTag();        TabInfo info = new TabInfo(tag, clss, args);        if (mAttached) {            // If we are already attached to the window, then check to make            // sure this tab's fragment is inactive if it exists.  This shouldn't            // normally happen.            info.fragment = mFragmentManager.findFragmentByTag(tag);            if (info.fragment != null && !info.fragment.isDetached()) {                FragmentTransaction ft = mFragmentManager.beginTransaction();                ft.detach(info.fragment);                ft.commit();            }        }        mTabs.add(info);         addTab(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);  //这里有坐标 我们去看看  只有看这个其余的和tabhost没关系        mTabSpecs.add(tabSpec);        if (mCurrentTab == -1) {            setCurrentTab(0);        }    }


到了这里

  @Override    public void addView(View child) {        if (child.getLayoutParams() == null) {            final LinearLayout.LayoutParams lp = new LayoutParams(                    0,                    ViewGroup.LayoutParams.MATCH_PARENT, 1.0f);            lp.setMargins(0, 0, 0, 0);            child.setLayoutParams(lp);        }        // Ensure you can navigate to the tab with the keyboard, and you can touch it        child.setFocusable(true);           child.setClickable(true);        super.addView(child);        // TODO: detect this via geometry with a tabwidget listener rather        // than potentially interfere with the view's listener        child.setOnClickListener(new TabClickListener(getTabCount() - 1));    // 点击事件        child.setOnFocusChangeListener(this);    }

找到 这个监听  tabclicklistener

    // registered with each tab indicator so we can notify tab host    private class TabClickListener implements OnClickListener {        private final int mTabIndex;        private TabClickListener(int tabIndex) {            mTabIndex = tabIndex;        }<span style="white-space:pre"></span>//就是这个理 再点击里面 切换的         public void onClick(View v) {            mSelectionChangedListener.onTabSelectionChanged(mTabIndex, true);        }    }

看到这 我们发现是在这个mTabWidget 的child监听点击事件来处理切换的

那我们 可以这也试一下  

 mTabHost.getTabWidget().getChildAt(x).setonclicklistsner~~~


在这里面做我们自己的处理 

可以实现在页面切换之前的处理,默认是切换页面这里重写就行


0 0