com.lee.sdk.widget.viewpager.BdPagerTabHost.java Source code

Java tutorial

Introduction

Here is the source code for com.lee.sdk.widget.viewpager.BdPagerTabHost.java

Source

/*
 * Copyright (C) 2013 Lee Hong (http://blog.csdn.net/leehong2005)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.lee.sdk.widget.viewpager;

import android.content.Context;
import android.content.res.ColorStateList;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.FrameLayout;

import com.lee.sdk.R;
import com.lee.sdk.widget.viewpager.BdPagerTabBar.OnTabSelectedListener;

/**
 * ??Tabtab???adapter?
 * {@link #setTabAdapter(Adapter)}?tab
 * 
 * ?ViewPager??
 * 
 * <h2>:</h2>
 * 
 * <pre class="prettyprint">
 * // BdPagerTabHost?new?XML
 * final BdPagerTabHost tabHostView = new BdPagerTabHost(this);
 * // tab
 * tabHostView.addTab(new BdPagerTab().setTitle("?"));
 * tabHostView.addTab(new BdPagerTab().setTitle("?"));
 * tabHostView.selectTab(0);   // tab
 * tabHostView.setTabBarBackground(R.drawable.picture_action_bar_bg);  // tab bar??
 * tabHostView.setTabTextSize(20);  // tab?????
 * tabHostView.setPageIndicatorDrawable(R.drawable.picture_tab_indicator); // tabindicator?
 * tabHostView.layoutTabs();   // tab
 *       
 * FragmentPagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
 *     public int getCount() {
 *         return tabHostView.getTabCount();
 *     }
 *           
 *     public Fragment getItem(int position) {
 *         // fragment
 *         return null;
 *     }
 * };
 * tabHostView.setPagerAdapter(adapter, 0);    // adapter1tab
 * </pre>
 * 
 * @author LiHong
 * @since 2013-11-11
 */
public class BdPagerTabHost extends FrameLayout {
    /** View pager */
    private ViewPager mViewPager;
    /** indicator **/
    private DrawablePageIndicator mPageIndicator;
    /** tab? */
    private BdPagerTabBar mPagerTabBar;
    /** tab?listener */
    private OnTabHostChangeListener mListener;

    /** 
     * 
     * @param context context
     */
    public BdPagerTabHost(Context context) {
        super(context);

        init(context);
    }

    /** 
     * 
     * @param context context
     * @param attrs attrs
     */
    public BdPagerTabHost(Context context, AttributeSet attrs) {
        super(context, attrs);

        init(context);
    }

    /** 
     * 
     * @param context context
     * @param attrs attrs
     * @param defStyle defStyle
     */
    public BdPagerTabHost(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        init(context);
    }

    /**
     * ?
     * 
     * @param context context
     */
    private void init(Context context) {
        View root = LayoutInflater.from(context).inflate(R.layout.sdk_tabhost_layout, this);

        // TabBar
        mPagerTabBar = (BdPagerTabBar) root.findViewById(R.id.pager_tab_bar);
        mPagerTabBar.setOnTabSelectedListener(new OnTabSelectedListener() {
            @Override
            public void onTabSelected(BdPagerTabBar pagerBar, int index) {
                if (null != mViewPager) {
                    mViewPager.setCurrentItem(index);
                }
            }
        });

        // ViewPager
        mViewPager = (ViewPager) root.findViewById(R.id.viewpager);
        mViewPager.setOffscreenPageLimit(3); //SUPPRESS CHECKSTYLE
        // Indicator
        mPageIndicator = (DrawablePageIndicator) root.findViewById(R.id.indicator);
        mPageIndicator.setOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrollStateChanged(int state) {
                if (null != mListener) {
                    mListener.onPageScrollStateChanged(state);
                }
            }

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

            @Override
            public void onPageSelected(int position) {
                selectTab(position);
                if (mListener != null) {
                    mListener.onPageSelected(position);
                }
            }
        });

        // tab
        setTabTextColor(getResources().getColorStateList(R.color.sdk_tab_item_color));
        // ?
        setTabTextSize((int) getResources().getDimension(R.dimen.pager_tab_item_textsize));
    }

    /**
     * Tab bar
     * 
     * @return BdPagerTabBar
     */
    public BdPagerTabBar getPagerTabBar() {
        return mPagerTabBar;
    }

    /**
     * 
     * 
     * @param resId ?id
     */
    public void setPageIndicatorDrawable(int resId) {
        if (null != mPageIndicator) {
            mPageIndicator.setIndicatorDrawable(getResources().getDrawable(resId));
        }
    }

    /**
     * Tab??
     * 
     * @param colorStateList colorStateList
     */
    public void setTabTextColor(ColorStateList colorStateList) {
        if (null != mPagerTabBar) {
            mPagerTabBar.setTabTextColor(colorStateList);
        }
    }

    /**
     * Tab
     * 
     * @param textColor 
     * @param selTextColor 
     */
    public void setTabTextColor(int textColor, int selTextColor) {
        if (null != mPagerTabBar) {
            mPagerTabBar.setTabTextColor(textColor, selTextColor);
        }
    }

    /**
     * ?
     * 
     * @param textSize textSize
     */
    public void setTabTextSize(int textSize) {
        if (null != mPagerTabBar) {
            mPagerTabBar.setTabTextSize(textSize);
        }
    }

    /**
     * tab bar
     * 
     * @param height 
     */
    public void setTabBarHeight(int height) {
        View container = findViewById(R.id.pager_tab_bar_container);
        if (null != container) {
            ViewGroup.LayoutParams params = container.getLayoutParams();
            if (null != params) {
                params.height = height;
                container.setLayoutParams(params);
                requestLayout();
            }
        }
    }

    /**
     * ?tab
     * 
     * @param index 
     */
    public void selectTab(final int index) {
        if (null != mPagerTabBar) {
            mPagerTabBar.selectTab(index);
        }
    }

    /**
     * ?tab,?
     * 
     * @param index 
     */
    public void selectTabAndScroll(final int index) {
        if (null != mPagerTabBar) {
            mPagerTabBar.selectTab(index);
            if (mViewPager != null) {
                mViewPager.setCurrentItem(index);
            }
        }
    }

    /**
     * Fragmentadapter
     * 
     * @param initPosition ??
     * @param adapter adapter
     */
    public void setPagerAdapter(PagerAdapter adapter, int initPosition) {
        if (null != mViewPager) {
            mViewPager.setAdapter(adapter);
            mPageIndicator.setViewPager(mViewPager, initPosition);
        }

        selectTab(initPosition);
    }

    /**
     * tabAdapter
     * 
     * @param adapter adapter
     */
    public void setTabAdapter(Adapter adapter) {
        if (null != mPagerTabBar) {
            mPagerTabBar.setAdapter(adapter);
        }
    }

    /**
     * itemitem???action bar
     * 
     * @param tab ActionItem
     * @return this object.
     */
    public BdPagerTabHost addTab(BdPagerTab tab) {
        mPagerTabBar.addTab(tab);
        return this;
    }

    /**
     * ?Tab
     * 
     * @return tab
     */
    public int getTabCount() {
        return mPagerTabBar.getTabCount();
    }

    /**
     * ?item
     * 
     * @return index
     */
    public int getCurrentItem() {
        return mViewPager.getCurrentItem();
    }

    /**
     * tabs
     */
    public void layoutTabs() {
        mPagerTabBar.updateTabs();
    }

    /**
     * tab bar
     * 
     * @param resid ?ID
     */
    public void setTabBarBackground(int resid) {
        if (null != mPagerTabBar) {
            mPagerTabBar.setBackgroundResource(resid);
        }
    }

    /**
     * tab?
     * @param listener OnTabHostChangeListener
     */
    public void setTabChangeListener(OnTabHostChangeListener listener) {
        mListener = listener;
    }

    /**
     * tab?
     * @author haiyang
     *
     */
    public interface OnTabHostChangeListener {
        /**
         * ?tab
         * @param position viewpagerviewindex
         */
        void onPageSelected(int position);

        /**
         * Pager??
         * @param state state
         */
        void onPageScrollStateChanged(int state);
    }
}