Example usage for android.widget TabWidget setDescendantFocusability

List of usage examples for android.widget TabWidget setDescendantFocusability

Introduction

In this page you can find the example usage for android.widget TabWidget setDescendantFocusability.

Prototype

public void setDescendantFocusability(int focusability) 

Source Link

Document

Set the descendant focusability of this view group.

Usage

From source file:com.lillicoder.lib.uiwidgets.tabs.FragmentTabsAdapter.java

@Override
public void onPageSelected(int position) {
    // According to the sample comments, the TabHost
    // will automatically put focus on the current tab
    // when the tab changes, thus taking focus from the
    // ViewPager. This code functions as a workaround to
    // prevent this behavior by getting the current focus
    // state, disabling focus change for descendant views,
    // then restoring the old focus state.
    TabWidget widget = this._tabHost.getTabWidget();
    int oldFocusability = widget.getDescendantFocusability();
    widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);

    this._tabHost.setCurrentTab(position);

    widget.setDescendantFocusability(oldFocusability);
}

From source file:com.momock.outlet.tab.PagerTabOutlet.java

public void attach(TabHolder target) {
    Logger.check(target != null, "Parameter target cannot be null!");
    Logger.check(target.getTabContent() instanceof ViewPager,
            "The PagerTabOutlet must contains a ViewPager content!");
    this.target = target;
    final TabHost tabHost = target.getTabHost();
    final ViewPager tabContent = (ViewPager) target.getTabContent();
    plugs = getPlugs();//from w  w w.  j  a  v  a  2 s  .  c  om

    ViewPager pager = (ViewPager) target.getTabContent();
    for (int i = 0; i < plugs.getItemCount(); i++) {
        ITabPlug plug = (ITabPlug) plugs.getItem(i);
        Logger.check(plug.getContent() instanceof ViewHolder,
                "The plug of PagerTabOutlet must include a ViewHolder!");
        ((ViewHolder) plug.getContent()).reset();
    }
    pager.setAdapter(new PagerAdapter() {

        @Override
        public int getCount() {
            return plugs.getItemCount();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ITabPlug plug = (ITabPlug) plugs.getItem(position);
            View view = ((ViewHolder) plug.getContent()).getView();
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    });

    tabContent.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            TabWidget widget = tabHost.getTabWidget();
            int oldFocusability = widget.getDescendantFocusability();
            widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
            tabHost.setCurrentTab(position);
            widget.setDescendantFocusability(oldFocusability);
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });
    tabHost.setup();
    tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {

        @Override
        public void onTabChanged(String tabId) {
            int index = tabHost.getCurrentTab();
            ITabPlug plug = (ITabPlug) plugs.getItem(index);
            setActivePlug(plug);
            tabContent.setCurrentItem(index, true);
        }
    });
    for (int i = 0; i < plugs.getItemCount(); i++) {
        final ITabPlug plug = (ITabPlug) plugs.getItem(i);
        Logger.check(plug.getContent() instanceof ViewHolder,
                "Plug in PagerTabOutlet must contains a ViewHolder content!");

        TabHost.TabSpec spec = tabHost.newTabSpec("" + i);
        target.setTabIndicator(spec, plug);
        spec.setContent(new TabContentFactory() {

            @Override
            public View createTabContent(String tag) {
                View v = new View(tabHost.getContext());
                v.setMinimumWidth(0);
                v.setMinimumHeight(0);
                return v;
            }

        });
        tabHost.addTab(spec);
        if (getActivePlug() == plug)
            tabHost.setCurrentTab(i);
    }
}

From source file:com.momock.outlet.tab.FragmentPagerTabOutlet.java

public void attach(final FragmentTabHolder tabHolder) {
    Logger.check(tabHolder != null, "Parameter tabHolder cannot be null!");
    this.target = tabHolder;
    final TabHost tabHost = target.getTabHost();
    plugs = getPlugs();//from  ww w. j av  a2 s  .  c o  m
    Logger.check(target.getTabContent() instanceof ViewPager, "The tab content container must be a ViewPager!");
    final ViewPager tabContent = (ViewPager) target.getTabContent();
    tabContent.setOffscreenPageLimit(plugs.getItemCount());
    tabContent.setAdapter(new FragmentPagerAdapter(target.getFragmentManager()) {

        @Override
        public Fragment getItem(final int position) {
            final ITabPlug plug = (ITabPlug) plugs.getItem(position);
            Fragment f = ((FragmentHolder) plug.getContent()).getFragment();
            if (f instanceof CaseFragment) {
                CaseFragment cf = (CaseFragment) f;
                cf.setActiveCaseIndicator(new IActiveCaseIndicator() {

                    @Override
                    public boolean isActiveCase() {
                        return getActivePlug() == plug;
                    }

                });
            }
            return f;
        }

        @Override
        public int getCount() {
            return plugs.getItemCount();
        }

        void doFinishUpdate(ViewGroup container) {
            super.finishUpdate(container);
        }

        @Override
        public void finishUpdate(final ViewGroup container) {
            new Handler().post(new Runnable() {

                @Override
                public void run() {
                    doFinishUpdate(container);
                }

            });
        }

        @Override
        public void setPrimaryItem(ViewGroup container, int position, final Object object) {
            super.setPrimaryItem(container, position, object);
            new Handler().post(new Runnable() {

                @Override
                public void run() {

                    if (object instanceof CaseFragment) {
                        CaseFragment cf = (CaseFragment) object;
                        if (cf.getCase() != null && cf.getCase().getParent() != null)
                            cf.getCase().getParent().setActiveCase(cf.getCase());
                    }
                }

            });
        }
    });
    tabHost.setup();
    for (int i = 0; i < plugs.getItemCount(); i++) {
        final ITabPlug plug = (ITabPlug) plugs.getItem(i);
        Logger.check(plug.getContent() instanceof FragmentHolder,
                "Plug in PagerTabOutlet must contains a FragmentHolder content!");

        TabHost.TabSpec spec = tabHost.newTabSpec("" + i);
        target.setTabIndicator(spec, plug);
        spec.setContent(new TabContentFactory() {

            @Override
            public View createTabContent(String tag) {
                View v = new View(tabHost.getContext());
                v.setMinimumWidth(0);
                v.setMinimumHeight(0);
                return v;
            }

        });
        tabHost.addTab(spec);
        if (getActivePlug() == plug) {
            tabHost.setCurrentTab(i);
            tabContent.setCurrentItem(i, true);
        }
    }
    tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {

        @Override
        public void onTabChanged(String tabId) {
            int index = tabHost.getCurrentTab();
            ITabPlug plug = (ITabPlug) plugs.getItem(index);
            setActivePlug(plug);
            tabContent.setCurrentItem(index, true);
        }
    });
    tabContent.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            TabWidget widget = tabHost.getTabWidget();
            int oldFocusability = widget.getDescendantFocusability();
            widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
            tabHost.setCurrentTab(position);
            widget.setDescendantFocusability(oldFocusability);
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });
}