Ejemplo n.º 1
0
  /** Create a default view to be used for tabs. */
  private View createDefaultTabView(int position) {
    final PagerAdapter adapter = mViewPager.getAdapter();
    View view;

    switch (mTabMode) {
      case TITLE_ONLY:
        {
          TextView textView = new TextView(getContext());
          textView.setGravity(Gravity.CENTER);
          textView.setText(adapter.getPageTitle(position));
          textView.setTextColor(mTabColorize.getDefaultTabColor(position));
          textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
          textView.setTypeface(null, mTextStyle);
          textView.setLayoutParams(
              new ViewGroup.LayoutParams(
                  ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));

          if (mTabBackground != NO_ID) {
            textView.setBackgroundResource(mTabBackground);
          } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            TypedValue outValue = new TypedValue();
            getContext()
                .getTheme()
                .resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
            textView.setBackgroundResource(outValue.resourceId);
          }

          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            textView.setAllCaps(mTextAllCaps);
          }

          textView.setPadding(mTabPaddingLeft, mTabPaddingTop, mTabPaddingRight, mTabPaddingBottom);

          if (position == mViewPager.getCurrentItem()) {
            textView.setTextColor(mTabColorize.getSelectedTabColor(position));
            textView.setSelected(true);
          }

          view = textView;
          break;
        }
      case ICON_ONLY:
        {
          ImageView imageView = new ImageView(getContext());
          imageView.setScaleType(ImageView.ScaleType.CENTER);
          imageView.setLayoutParams(
              new ViewGroup.LayoutParams(
                  ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));

          if (mTabBackground != NO_ID) {
            imageView.setBackgroundResource(mTabBackground);
          } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            TypedValue outValue = new TypedValue();
            getContext()
                .getTheme()
                .resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
            imageView.setBackgroundResource(outValue.resourceId);
          }

          //                Drawable drawable = ContextCompat.getDrawable(getContext(),
          // ((IconTabProvider) adapter).getPageIconResId(position)).mutate();
          Drawable drawable =
              ContextCompat.getDrawable(
                  getContext(), ((IconTabProvider) adapter).getPageIconResId(position));

          if (mIconCrossFade && drawable instanceof StateListDrawable) {
            try {
              StateListDrawable stateListDrawable = (StateListDrawable) drawable;
              int fadingIndex =
                  StateListDrawableHelper.getStateDrawableIndex(
                      stateListDrawable, new int[] {android.R.attr.state_selected});
              Drawable fading =
                  StateListDrawableHelper.getStateDrawable(stateListDrawable, fadingIndex);
              int baseIndex =
                  StateListDrawableHelper.getStateDrawableIndex(stateListDrawable, new int[] {0});
              Drawable base =
                  StateListDrawableHelper.getStateDrawable(stateListDrawable, baseIndex);
              CrossFadeDrawable cd = new CrossFadeDrawable();
              cd.setFading(fading);
              //                        tintDrawable(cd.getFading().mutate(),
              // mTabColorize.getSelectedTabColor(position));
              tintDrawable(cd.getFading(), mTabColorize.getSelectedTabColor(position));
              cd.setBase(base);
              //                        tintDrawable(cd.getBase().mutate(),
              // mTabColorize.getDefaultTabColor(position));
              tintDrawable(cd.getBase(), mTabColorize.getDefaultTabColor(position));
              imageView.setImageDrawable(cd);
            } catch (Exception e) {
              imageView.setImageDrawable(drawable);
            }
          } else {
            imageView.setImageDrawable(drawable);
          }

          imageView.setPadding(
              mTabPaddingLeft, mTabPaddingTop, mTabPaddingRight, mTabPaddingBottom);

          if (position == mViewPager.getCurrentItem()) {
            Drawable d = imageView.getDrawable();
            if (d instanceof CrossFadeDrawable) {
              crossFadeDrawable(d, 1);
            } else {
              tintDrawable(d, mTabColorize.getSelectedTabColor(position));
            }
            imageView.setSelected(true);
          } else {
            tintDrawable(imageView.getDrawable(), mTabColorize.getDefaultTabColor(position));
          }

          view = imageView;
          break;
        }
      case BOTH:
        {
          TextView textView = new TextView(getContext());
          textView.setGravity(Gravity.CENTER);
          textView.setText(adapter.getPageTitle(position));
          textView.setTextColor(mTabColorize.getDefaultTabColor(position));
          textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
          textView.setTypeface(null, mTextStyle);
          textView.setLayoutParams(
              new ViewGroup.LayoutParams(
                  ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));

          if (mTabBackground != NO_ID) {
            textView.setBackgroundResource(mTabBackground);
          } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            TypedValue outValue = new TypedValue();
            getContext()
                .getTheme()
                .resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
            textView.setBackgroundResource(outValue.resourceId);
          }

          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            textView.setAllCaps(mTextAllCaps);
          }

          textView.setPadding(mTabPaddingLeft, mTabPaddingTop, mTabPaddingRight, mTabPaddingBottom);

          //                Drawable drawable = ContextCompat.getDrawable(getContext(),
          // ((IconTabProvider) adapter).getPageIconResId(position)).mutate();
          Drawable drawable =
              ContextCompat.getDrawable(
                  getContext(), ((IconTabProvider) adapter).getPageIconResId(position));

          if (mIconCrossFade && drawable instanceof StateListDrawable) {
            try {
              StateListDrawable stateListDrawable = (StateListDrawable) drawable;
              int fadingIndex =
                  StateListDrawableHelper.getStateDrawableIndex(
                      stateListDrawable, new int[] {android.R.attr.state_selected});
              Drawable fading =
                  StateListDrawableHelper.getStateDrawable(stateListDrawable, fadingIndex);
              int baseIndex =
                  StateListDrawableHelper.getStateDrawableIndex(stateListDrawable, new int[] {0});
              Drawable base =
                  StateListDrawableHelper.getStateDrawable(stateListDrawable, baseIndex);
              CrossFadeDrawable cd = new CrossFadeDrawable();
              cd.setFading(fading);
              cd.setFading(fading);
              //                        tintDrawable(cd.getFading().mutate(),
              // mTabColorize.getSelectedTabColor(position));
              tintDrawable(cd.getFading(), mTabColorize.getSelectedTabColor(position));
              cd.setBase(base);
              //                        tintDrawable(cd.getBase().mutate(),
              // mTabColorize.getDefaultTabColor(position));
              tintDrawable(cd.getBase(), mTabColorize.getDefaultTabColor(position));
              textView.setCompoundDrawablesWithIntrinsicBounds(null, cd, null, null);
            } catch (Exception e) {
              textView.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
            }
          } else {
            textView.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
          }

          textView.setCompoundDrawablePadding(mDrawablePadding);

          if (position == mViewPager.getCurrentItem()) {
            textView.setTextColor(mTabColorize.getSelectedTabColor(position));

            Drawable d = textView.getCompoundDrawables()[1];
            if (d instanceof CrossFadeDrawable) {
              crossFadeDrawable(d, 1);
            } else {
              tintDrawable(d, mTabColorize.getSelectedTabColor(position));
            }
            textView.setSelected(true);
          } else {
            Drawable d = textView.getCompoundDrawables()[1];
            if (!(d instanceof CrossFadeDrawable)) {
              tintDrawable(d, mTabColorize.getDefaultTabColor(position));
            }
          }

          view = textView;
          break;
        }
      default:
        {
          throw new IllegalStateException("Invalid tab mode: " + mTabMode);
        }
    }

    return view;
  }
Ejemplo n.º 2
0
 private void crossFadeDrawable(Drawable drawable, float progress) {
   CrossFadeDrawable cd = (CrossFadeDrawable) drawable;
   cd.setProgress(progress);
 }