com.dreamchen.useful.mouserace.view.sliding.AbSlidingSmoothFixTabView.java Source code

Java tutorial

Introduction

Here is the source code for com.dreamchen.useful.mouserace.view.sliding.AbSlidingSmoothFixTabView.java

Source

/*
 * Copyright (C) 2012 www.amsoft.cn
 * 
 * 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.dreamchen.useful.mouserace.view.sliding;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dreamchen.useful.mouserace.adapter.AbFragmentPagerAdapter;
import com.dreamchen.useful.mouserace.utils.AbAppUtil;
import com.dreamchen.useful.mouserace.utils.AbLogUtil;

// TODO: Auto-generated Javadoc

/**
 *  2012 amsoft.cn
 * ??AbSlidingSmoothFixTabView.java 
 * ??tab,tab?.
 *
 * @author 
 * @version v1.0
 * @date2013-05-17 ?6:46:29
 */
public class AbSlidingSmoothFixTabView extends LinearLayout {

    /** The context. */
    private Context context;

    /** tab. */
    private LinearLayout mTabLayout = null;

    /** The m view pager. */
    private ViewPager mViewPager;

    /** tab. */
    private ArrayList<TextView> tabItemList = null;

    /** View. */
    private ArrayList<Fragment> pagerItemList = null;

    /** tab. */
    private List<String> tabItemTextList = null;

    /** The layout params ff. */
    public LinearLayout.LayoutParams layoutParamsFF = null;

    /** The layout params fw. */
    public LinearLayout.LayoutParams layoutParamsFW = null;

    /** The layout params ww. */
    public LinearLayout.LayoutParams layoutParamsWW = null;

    /** ?. */
    private ImageView mTabImg;

    /** ???. */
    private int mSelectedTabIndex = 0;

    /** ?. */
    private AbFragmentPagerAdapter mFragmentPagerAdapter = null;

    /** tab?. */
    private int tabTextSize = 16;

    /** tab. */
    private int tabColor = Color.BLACK;

    /** tab. */
    private int tabSelectedColor = Color.BLACK;

    /** tab?. */
    private int tabSlidingHeight = 5;

    /** ?tab?. */
    private int startX = 0;

    /** The m width. */
    private int mWidth = 0;

    /**
     * Instantiates a new ab sliding smooth fix tab view.
     *
     * @param context the context
     * @param attrs the attrs
     */
    public AbSlidingSmoothFixTabView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;

        layoutParamsFW = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        layoutParamsFF = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        layoutParamsWW = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        this.setOrientation(LinearLayout.VERTICAL);

        mTabLayout = new LinearLayout(context);
        mTabLayout.setOrientation(LinearLayout.HORIZONTAL);
        mTabLayout.setGravity(Gravity.CENTER);

        //Tab?
        tabItemList = new ArrayList<TextView>();
        tabItemTextList = new ArrayList<String>();

        this.addView(mTabLayout, layoutParamsFW);

        //?
        mTabImg = new ImageView(context);
        this.addView(mTabImg, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, tabSlidingHeight));

        //View?
        mViewPager = new ViewPager(context);
        //ViewPager,setId()id
        mViewPager.setId(1985);
        pagerItemList = new ArrayList<Fragment>();

        this.addView(mViewPager, layoutParamsFF);

        //?FragmentActivity
        if (!(this.context instanceof FragmentActivity)) {
            AbLogUtil.e(AbSlidingSmoothFixTabView.class,
                    "AbSlidingSmoothTabView?context,FragmentActivity");
        }

        DisplayMetrics mDisplayMetrics = AbAppUtil.getDisplayMetrics(context);
        mWidth = mDisplayMetrics.widthPixels;

        FragmentManager mFragmentManager = ((FragmentActivity) this.context).getSupportFragmentManager();
        mFragmentPagerAdapter = new AbFragmentPagerAdapter(mFragmentManager, pagerItemList);
        mViewPager.setAdapter(mFragmentPagerAdapter);
        mViewPager.setOnPageChangeListener(new MyOnPageChangeListener());
        mViewPager.setOffscreenPageLimit(3);

    }

    /**
     * The listener interface for receiving myOnPageChange events.
     * The class that is interested in processing a myOnPageChange
     * event implements this interface, and the object created
     * with that class is registered with a component using the
     * component's <code>addMyOnPageChangeListener<code> method. When
     * the myOnPageChange event occurs, that object's appropriate
     * method is invoked.
     *
     * @see MyOnPageChangeEvent
     */
    public class MyOnPageChangeListener implements OnPageChangeListener {

        /* (non-Javadoc)
         * @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageScrollStateChanged(int)
         */
        @Override
        public void onPageScrollStateChanged(int arg0) {

        }

        /* (non-Javadoc)
         * @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageScrolled(int, float, int)
         */
        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        /* (non-Javadoc)
         * @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageSelected(int)
         */
        @Override
        public void onPageSelected(int arg0) {
            //???
            computeTabImg(arg0);
        }

    }

    /**
     * ??.
     *
     * @param v the v
     * @param startX the start x
     * @param toX the to x
     * @param startY the start y
     * @param toY the to y
     */
    public void imageSlide(View v, int startX, int toX, int startY, int toY) {
        TranslateAnimation anim = new TranslateAnimation(startX, toX, startY, toY);
        anim.setDuration(100);
        anim.setFillAfter(true);
        v.startAnimation(anim);
    }

    /**
     * ???.
     *
     * @param index the index
     */
    public void computeTabImg(int index) {

        for (int i = 0; i < tabItemList.size(); i++) {
            TextView tv = tabItemList.get(i);
            tv.setTextColor(tabColor);
            tv.setSelected(false);
            if (index == i) {
                tv.setTextColor(tabSelectedColor);
                tv.setSelected(true);
            }
        }

        //?
        int itemWidth = mWidth / tabItemList.size();

        LayoutParams mParams = new LayoutParams(itemWidth, tabSlidingHeight);
        mParams.topMargin = -tabSlidingHeight;
        mTabImg.setLayoutParams(mParams);

        AbLogUtil.d(AbSlidingSmoothFixTabView.class, "old--startX:" + startX);
        int toX = itemWidth * index;
        imageSlide(mTabImg, startX, toX, 0, 0);
        startX = toX;

        mSelectedTabIndex = index;
    }

    /**
     * ??tab.
     *
     * @param tabTexts the tab texts
     * @param fragments the fragments
     */
    public void addItemViews(List<String> tabTexts, List<Fragment> fragments) {

        tabItemTextList.addAll(tabTexts);
        pagerItemList.addAll(fragments);

        tabItemList.clear();
        mTabLayout.removeAllViews();

        for (int i = 0; i < tabItemTextList.size(); i++) {
            final int index = i;
            String text = tabItemTextList.get(i);
            TextView tv = new TextView(this.context);
            tv.setTextColor(tabColor);
            tv.setTextSize(tabTextSize);
            tv.setText(text);
            tv.setGravity(Gravity.CENTER);
            tv.setLayoutParams(new LayoutParams(0, LayoutParams.FILL_PARENT, 1));
            tv.setPadding(12, 5, 12, 5);
            tv.setFocusable(false);
            tabItemList.add(tv);
            mTabLayout.addView(tv);
            tv.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    mViewPager.setCurrentItem(index);
                }
            });
        }

        //?
        mFragmentPagerAdapter.notifyDataSetChanged();
        mViewPager.setCurrentItem(0);
        computeTabImg(0);

    }

    /**
     * ??tab.
     *
     * @param tabText the tab text
     * @param fragment the fragment
     */
    public void addItemView(String tabText, Fragment fragment) {

        tabItemTextList.add(tabText);
        pagerItemList.add(fragment);

        tabItemList.clear();
        mTabLayout.removeAllViews();

        for (int i = 0; i < tabItemTextList.size(); i++) {
            final int index = i;
            String text = tabItemTextList.get(i);
            TextView tv = new TextView(this.context);
            tv.setTextColor(tabColor);
            tv.setTextSize(tabTextSize);
            tv.setText(text);
            tv.setGravity(Gravity.CENTER);
            tv.setLayoutParams(new LayoutParams(0, LayoutParams.FILL_PARENT, 1));
            tv.setPadding(12, 5, 12, 5);
            tv.setFocusable(false);
            tabItemList.add(tv);
            mTabLayout.addView(tv);
            tv.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    mViewPager.setCurrentItem(index);
                }
            });
        }

        //?
        AbLogUtil.d(AbSlidingSmoothFixTabView.class, "addItemView finish");
        mFragmentPagerAdapter.notifyDataSetChanged();
        mViewPager.setCurrentItem(0);
        computeTabImg(0);
    }

    /**
     * ???.
     *
     * @param index the index
     */
    public void removeItemView(int index) {

        tabItemList.remove(index);
        mTabLayout.removeViewAt(index);
        pagerItemList.remove(index);

        mFragmentPagerAdapter.notifyDataSetChanged();
    }

    /**
     * ??.
     *
     * @param index the index
     */
    public void removeAllItemView(int index) {
        tabItemList.clear();
        mTabLayout.removeAllViews();
        pagerItemList.clear();
        mFragmentPagerAdapter.notifyDataSetChanged();
    }

    /**
     * ???ViewViewPager.
     *
     * @return the view pager
     */
    public ViewPager getViewPager() {
        return mViewPager;
    }

    /**
     * Gets the tab layout.
     *
     * @return the tab layout
     */
    public LinearLayout getTabLayout() {
        return mTabLayout;
    }

    /**
     * ??Tab.
     *
     * @param res the new tab layout background resource
     */
    public void setTabLayoutBackgroundResource(int res) {
        this.mTabLayout.setBackgroundResource(res);
    }

    /**
     * Gets the tab color.
     *
     * @return the tab color
     */
    public int getTabColor() {
        return tabColor;
    }

    /**
     * ??tab?.
     *
     * @param tabColor the new tab color
     */
    public void setTabColor(int tabColor) {
        this.tabColor = tabColor;
    }

    /**
     * ???.
     *
     * @param tabColor the new tab selected color
     */
    public void setTabSelectedColor(int tabColor) {
        this.tabSelectedColor = tabColor;
        this.mTabImg.setBackgroundColor(tabColor);
    }

    /**
     * Gets the tab text size.
     *
     * @return the tab text size
     */
    public int getTabTextSize() {
        return tabTextSize;
    }

    /**
     * Sets the tab text size.
     *
     * @param tabTextSize the new tab text size
     */
    public void setTabTextSize(int tabTextSize) {
        this.tabTextSize = tabTextSize;
    }

    /**
     * ???tab?.
     *
     * @param left the left
     * @param top the top
     * @param right the right
     * @param bottom the bottom
     */
    public void setTabPadding(int left, int top, int right, int bottom) {
        for (int i = 0; i < tabItemList.size(); i++) {
            TextView tv = tabItemList.get(i);
            tv.setPadding(left, top, right, bottom);
        }
    }

    /**
     * Gets the tab sliding height.
     *
     * @return the tab sliding height
     */
    public int getTabSlidingHeight() {
        return tabSlidingHeight;
    }

    /**
     * ???.
     *
     * @param tabSlidingHeight the new tab sliding height
     */
    public void setTabSlidingHeight(int tabSlidingHeight) {
        this.tabSlidingHeight = tabSlidingHeight;
    }

    /* (non-Javadoc)
     * @see android.widget.LinearLayout#onMeasure(int, int)
     */
    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

}