Esempio n. 1
0
 @Override
 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
   super.onSizeChanged(w, h, oldw, oldh);
   if (mTabStrip.isIndicatorAlwaysInCenter() && getChildCount() > 0) {
     int left = (w - mTabStrip.getChildMeasuredWidthAt(0)) / 2;
     int right = (w - mTabStrip.getChildMeasuredWidthAt(getChildCount() - 1)) / 2;
     setPadding(left, getPaddingTop(), right, getPaddingBottom());
     setClipToPadding(false);
   }
 }
  private void populateTabStrip() {
    final PagerAdapter adapter = viewPager.getAdapter();

    for (int i = 0; i < adapter.getCount(); i++) {

      final View tabView =
          (tabProvider == null)
              ? createDefaultTabView(adapter.getPageTitle(i))
              : tabProvider.createTabView(tabStrip, i, adapter);

      if (tabView == null) {
        throw new IllegalStateException("tabView is null.");
      }

      if (distributeEvenly) {
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) tabView.getLayoutParams();
        lp.width = 0;
        lp.weight = 1;
      }

      if (internalTabClickListener != null) {
        tabView.setOnClickListener(internalTabClickListener);
      }

      tabStrip.addView(tabView);

      if (i == viewPager.getCurrentItem()) {
        tabView.setSelected(true);
      }
    }
  }
  /**
   * Sets the associated view pager. Note that the assumption here is that the pager content (number
   * of tabs and tab titles) does not change after this call has been made.
   */
  public void setViewPager(ViewPager viewPager) {
    tabStrip.removeAllViews();

    this.viewPager = viewPager;
    if (viewPager != null && viewPager.getAdapter() != null) {
      viewPager.addOnPageChangeListener(new InternalViewPagerListener());
      populateTabStrip();
    }
  }
Esempio n. 4
0
  /**
   * Sets the associated view pager. Note that the assumption here is that the pager content (number
   * of tabs and tab titles) does not change after this call has been made.
   */
  public void setViewPager(ViewPager viewPager) {
    mTabStrip.removeAllViews();

    mViewPager = viewPager;
    if (viewPager != null) {
      viewPager.setOnPageChangeListener(new InternalViewPagerListener());
      populateTabStrip();
    }
  }
Esempio n. 5
0
  private void scrollToTab(int tabIndex, int positionOffset) {
    final int tabStripChildCount = mTabStrip.getChildCount();
    if (tabStripChildCount == 0 || tabIndex < 0 || tabIndex >= tabStripChildCount) {
      return;
    }

    View selectedChild = mTabStrip.getChildAt(tabIndex);
    if (selectedChild != null) {
      int targetScrollX = selectedChild.getLeft() + positionOffset;
      if (mTabStrip.isIndicatorAlwaysInCenter()) {
        targetScrollX -= (mTabStrip.getChildWidthAt(0) - selectedChild.getWidth()) / 2;
      } else if (tabIndex > 0 || positionOffset > 0) {
        // If we're not at the first child and are mid-scroll, make sure we obey the offset
        targetScrollX -= mTitleOffset;
      }

      scrollTo(targetScrollX, 0);
    }
  }
Esempio n. 6
0
  private void populateTabStrip() {
    final PagerAdapter adapter = mViewPager.getAdapter();
    final OnClickListener tabClickListener = new TabClickListener();

    final LayoutInflater inflater = LayoutInflater.from(getContext());
    for (int i = 0; i < adapter.getCount(); i++) {
      View tabView = null;
      TextView tabTitleView = null;

      if (mTabViewLayoutId != NO_ID) {
        // If there is a custom tab view layout id set, try and inflate it
        tabView = inflater.inflate(mTabViewLayoutId, mTabStrip, false);
      }

      if (mTabViewTextViewId != NO_ID && tabView != null) {
        tabTitleView = (TextView) tabView.findViewById(mTabViewTextViewId);
      }

      if (tabView == null) {
        tabView = createDefaultTabView(getContext());
      }

      if (tabTitleView == null && TextView.class.isInstance(tabView)) {
        tabTitleView = (TextView) tabView;
      }

      if (tabTitleView == null) {
        throw new IllegalStateException("tabTitleView not found.");
      }

      if (mDistributeEvenly) {
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) tabView.getLayoutParams();
        lp.width = 0;
        lp.weight = 1;
      }

      tabTitleView.setText(adapter.getPageTitle(i));
      tabView.setOnClickListener(tabClickListener);

      mTabStrip.addView(tabView);

      if (i == mViewPager.getCurrentItem()) {
        tabView.setSelected(true);
      }
    }
  }
 @Override
 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
   super.onSizeChanged(w, h, oldw, oldh);
   if (tabStrip.isIndicatorAlwaysInCenter() && tabStrip.getChildCount() > 0) {
     View firstTab = tabStrip.getChildAt(0);
     View lastTab = tabStrip.getChildAt(getChildCount() - 1);
     int start = (w - Utils.getMeasuredWidth(firstTab)) / 2 - Utils.getMarginStart(firstTab);
     int end = (w - Utils.getMeasuredWidth(lastTab)) / 2 - Utils.getMarginEnd(lastTab);
     tabStrip.setMinimumWidth(tabStrip.getMeasuredWidth());
     ViewCompat.setPaddingRelative(this, start, getPaddingTop(), end, getPaddingBottom());
     setClipToPadding(false);
   }
 }
  public SmartTabLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    // Disable the Scroll Bar
    setHorizontalScrollBarEnabled(false);

    final DisplayMetrics dm = getResources().getDisplayMetrics();
    final float density = dm.density;

    int tabBackgroundResId = NO_ID;
    boolean textAllCaps = TAB_VIEW_TEXT_ALL_CAPS;
    ColorStateList textColors;
    float textSize =
        TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP, dm);
    int textHorizontalPadding = (int) (TAB_VIEW_PADDING_DIPS * density);
    int textMinWidth = (int) (TAB_VIEW_TEXT_MIN_WIDTH * density);
    boolean distributeEvenly = DEFAULT_DISTRIBUTE_EVENLY;
    int customTabLayoutId = NO_ID;
    int customTabTextViewId = NO_ID;
    boolean clickable = TAB_CLICKABLE;
    int titleOffset = (int) (TITLE_OFFSET_DIPS * density);

    TypedArray a =
        context.obtainStyledAttributes(attrs, R.styleable.stl_SmartTabLayout, defStyle, 0);
    tabBackgroundResId =
        a.getResourceId(
            R.styleable.stl_SmartTabLayout_stl_defaultTabBackground, tabBackgroundResId);
    textAllCaps =
        a.getBoolean(R.styleable.stl_SmartTabLayout_stl_defaultTabTextAllCaps, textAllCaps);
    textColors = a.getColorStateList(R.styleable.stl_SmartTabLayout_stl_defaultTabTextColor);
    textSize = a.getDimension(R.styleable.stl_SmartTabLayout_stl_defaultTabTextSize, textSize);
    textHorizontalPadding =
        a.getDimensionPixelSize(
            R.styleable.stl_SmartTabLayout_stl_defaultTabTextHorizontalPadding,
            textHorizontalPadding);
    textMinWidth =
        a.getDimensionPixelSize(
            R.styleable.stl_SmartTabLayout_stl_defaultTabTextMinWidth, textMinWidth);
    customTabLayoutId =
        a.getResourceId(
            R.styleable.stl_SmartTabLayout_stl_customTabTextLayoutId, customTabLayoutId);
    customTabTextViewId =
        a.getResourceId(
            R.styleable.stl_SmartTabLayout_stl_customTabTextViewId, customTabTextViewId);
    distributeEvenly =
        a.getBoolean(R.styleable.stl_SmartTabLayout_stl_distributeEvenly, distributeEvenly);
    clickable = a.getBoolean(R.styleable.stl_SmartTabLayout_stl_clickable, clickable);
    titleOffset = a.getLayoutDimension(R.styleable.stl_SmartTabLayout_stl_titleOffset, titleOffset);
    a.recycle();

    this.titleOffset = titleOffset;
    this.tabViewBackgroundResId = tabBackgroundResId;
    this.tabViewTextAllCaps = textAllCaps;
    this.tabViewTextColors =
        (textColors != null) ? textColors : ColorStateList.valueOf(TAB_VIEW_TEXT_COLOR);
    this.tabViewTextSize = textSize;
    this.tabViewTextHorizontalPadding = textHorizontalPadding;
    this.tabViewTextMinWidth = textMinWidth;
    this.internalTabClickListener = clickable ? new InternalTabClickListener() : null;
    this.distributeEvenly = distributeEvenly;

    if (customTabLayoutId != NO_ID) {
      setCustomTabView(customTabLayoutId, customTabTextViewId);
    }

    this.tabStrip = new SmartTabStrip(context, attrs);

    if (distributeEvenly && tabStrip.isIndicatorAlwaysInCenter()) {
      throw new UnsupportedOperationException(
          "'distributeEvenly' and 'indicatorAlwaysInCenter' both use does not support");
    }

    // Make sure that the Tab Strips fills this View
    setFillViewport(!tabStrip.isIndicatorAlwaysInCenter());

    addView(tabStrip, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
  }
  private void scrollToTab(int tabIndex, float positionOffset) {
    final int tabStripChildCount = tabStrip.getChildCount();
    if (tabStripChildCount == 0 || tabIndex < 0 || tabIndex >= tabStripChildCount) {
      return;
    }

    final boolean isLayoutRtl = Utils.isLayoutRtl(this);
    View selectedTab = tabStrip.getChildAt(tabIndex);
    int widthPlusMargin = Utils.getWidth(selectedTab) + Utils.getMarginHorizontally(selectedTab);
    int extraOffset = (int) (positionOffset * widthPlusMargin);

    if (tabStrip.isIndicatorAlwaysInCenter()) {

      if (0f < positionOffset && positionOffset < 1f) {
        View nextTab = tabStrip.getChildAt(tabIndex + 1);
        int selectHalfWidth = Utils.getWidth(selectedTab) / 2 + Utils.getMarginEnd(selectedTab);
        int nextHalfWidth = Utils.getWidth(nextTab) / 2 + Utils.getMarginStart(nextTab);
        extraOffset = Math.round(positionOffset * (selectHalfWidth + nextHalfWidth));
      }

      View firstTab = tabStrip.getChildAt(0);
      int x;
      if (isLayoutRtl) {
        int first = Utils.getWidth(firstTab) + Utils.getMarginEnd(firstTab);
        int selected = Utils.getWidth(selectedTab) + Utils.getMarginEnd(selectedTab);
        x = Utils.getEnd(selectedTab) - Utils.getMarginEnd(selectedTab) - extraOffset;
        x -= (first - selected) / 2;
      } else {
        int first = Utils.getWidth(firstTab) + Utils.getMarginStart(firstTab);
        int selected = Utils.getWidth(selectedTab) + Utils.getMarginStart(selectedTab);
        x = Utils.getStart(selectedTab) - Utils.getMarginStart(selectedTab) + extraOffset;
        x -= (first - selected) / 2;
      }

      scrollTo(x, 0);
      return;
    }

    int x;
    if (titleOffset == TITLE_OFFSET_AUTO_CENTER) {

      if (0f < positionOffset && positionOffset < 1f) {
        View nextTab = tabStrip.getChildAt(tabIndex + 1);
        int selectHalfWidth = Utils.getWidth(selectedTab) / 2 + Utils.getMarginEnd(selectedTab);
        int nextHalfWidth = Utils.getWidth(nextTab) / 2 + Utils.getMarginStart(nextTab);
        extraOffset = Math.round(positionOffset * (selectHalfWidth + nextHalfWidth));
      }

      if (isLayoutRtl) {
        x = -Utils.getWidthWithMargin(selectedTab) / 2 + getWidth() / 2;
      } else {
        x = Utils.getWidthWithMargin(selectedTab) / 2 - getWidth() / 2;
      }

    } else {

      if (isLayoutRtl) {
        x = (tabIndex > 0 || positionOffset > 0) ? titleOffset : 0;
      } else {
        x = (tabIndex > 0 || positionOffset > 0) ? -titleOffset : 0;
      }
    }

    int start = Utils.getStart(selectedTab);
    int startMargin = Utils.getMarginStart(selectedTab);
    if (isLayoutRtl) {
      x += start + startMargin - extraOffset - getWidth() + Utils.getPaddingHorizontally(this);
    } else {
      x += start - startMargin + extraOffset;
    }

    scrollTo(x, 0);
  }
 /**
  * Returns the view at the specified position in the tabs.
  *
  * @param position the position at which to get the view from
  * @return the view at the specified position or null if the position does not exist within the
  *     tabs
  */
 public View getTabAt(int position) {
   return tabStrip.getChildAt(position);
 }
 /**
  * Sets the colors to be used for tab dividers. These colors are treated as a circular array.
  * Providing one color will mean that all tabs are indicated with the same color.
  */
 public void setDividerColors(int... colors) {
   tabStrip.setDividerColors(colors);
 }
 /**
  * Sets the colors to be used for indicating the selected tab. These colors are treated as a
  * circular array. Providing one color will mean that all tabs are indicated with the same color.
  */
 public void setSelectedIndicatorColors(int... colors) {
   tabStrip.setSelectedIndicatorColors(colors);
 }
 /**
  * Set the custom {@link TabColorizer} to be used.
  *
  * <p>If you only require simple customisation then you can use {@link
  * #setSelectedIndicatorColors(int...)} and {@link #setDividerColors(int...)} to achieve similar
  * effects.
  */
 public void setCustomTabColorizer(TabColorizer tabColorizer) {
   tabStrip.setCustomTabColorizer(tabColorizer);
 }
 /**
  * Set the behavior of the Indicator scrolling feedback.
  *
  * @param interpolator {@link com.ogaclejapan.smarttablayout.SmartTabIndicationInterpolator}
  */
 public void setIndicationInterpolator(SmartTabIndicationInterpolator interpolator) {
   tabStrip.setIndicationInterpolator(interpolator);
 }
Esempio n. 15
0
  public SmartTabLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    // Disable the Scroll Bar
    setHorizontalScrollBarEnabled(false);
    // Make sure that the Tab Strips fills this View
    setFillViewport(true);
    setOverScrollMode(OVER_SCROLL_NEVER);

    final DisplayMetrics dm = getResources().getDisplayMetrics();
    final float density = dm.density;

    boolean textAllCaps = TAB_VIEW_TEXT_ALL_CAPS;
    int textColor = TAB_VIEW_TEXT_COLOR;
    float textSize =
        TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP, dm);
    int textHorizontalPadding = (int) (TAB_VIEW_PADDING_DIPS * density);
    int textMinWidth = (int) (TAB_VIEW_TEXT_MIN_WIDTH * density);
    boolean distributeEvenly = DEFAULT_DISTRIBUTE_EVENLY;
    int textLayoutId = NO_ID;
    int textViewId = NO_ID;

    TypedArray a =
        context.obtainStyledAttributes(attrs, R.styleable.stl_SmartTabLayout, defStyle, 0);
    textAllCaps =
        a.getBoolean(R.styleable.stl_SmartTabLayout_stl_defaultTabTextAllCaps, textAllCaps);
    textColor = a.getColor(R.styleable.stl_SmartTabLayout_stl_defaultTabTextColor, textColor);
    textSize = a.getDimension(R.styleable.stl_SmartTabLayout_stl_defaultTabTextSize, textSize);
    textHorizontalPadding =
        a.getDimensionPixelSize(
            R.styleable.stl_SmartTabLayout_stl_defaultTabTextHorizontalPadding,
            textHorizontalPadding);
    textMinWidth =
        a.getDimensionPixelSize(
            R.styleable.stl_SmartTabLayout_stl_defaultTabTextMinWidth, textMinWidth);
    textLayoutId =
        a.getResourceId(R.styleable.stl_SmartTabLayout_stl_customTabTextLayoutId, textLayoutId);
    textViewId =
        a.getResourceId(R.styleable.stl_SmartTabLayout_stl_customTabTextViewId, textViewId);
    distributeEvenly =
        a.getBoolean(R.styleable.stl_SmartTabLayout_stl_distributeEvenly, distributeEvenly);
    a.recycle();

    mTitleOffset = (int) (TITLE_OFFSET_DIPS * density);
    mTabViewTextAllCaps = textAllCaps;
    mTabViewTextColor = textColor;
    mTabViewTextSize = textSize;
    mTabViewTextHorizontalPadding = textHorizontalPadding;
    mTabViewTextMinWidth = textMinWidth;
    mTabViewLayoutId = textLayoutId;
    mTabViewTextViewId = textViewId;
    mDistributeEvenly = distributeEvenly;

    mTabStrip = new SmartTabStrip(context, attrs);

    if (distributeEvenly && mTabStrip.isIndicatorAlwaysInCenter()) {
      throw new UnsupportedOperationException(
          "'distributeEvenly' and 'indicatorAlwaysInCenter' both use does not support");
    }

    addView(mTabStrip, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
  }