コード例 #1
0
  private void addTemporaryTab() {
    for (int i = 0; i < 3; i++) {
      CharSequence title = null;
      if (i == 0) title = "TAB ONE";
      else if (i == 1) title = "TAB TWO";
      else if (i == 2) title = "TAB THREE";

      CheckedTextView tv = new CheckedTextView(getContext());
      tv.setCheckMarkDrawable(null);
      tv.setText(title);
      tv.setGravity(Gravity.CENTER);
      tv.setTextAppearance(getContext(), mTextApperance);
      tv.setSingleLine(true);
      tv.setEllipsize(TruncateAt.END);
      tv.setTag(i);
      tv.setChecked(i == 0);
      if (mMode == MODE_SCROLL) {
        tv.setPadding(mTabPadding, 0, mTabPadding, 0);
        mTabContainer.addView(
            tv,
            new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
      } else if (mMode == MODE_FIXED) {
        LinearLayout.LayoutParams params =
            new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT);
        params.weight = 1f;
        mTabContainer.addView(tv, params);
      }
    }
  }
コード例 #2
0
  public void setCurrentItem(int position) {
    if (mSelectedPosition != position) {
      CheckedTextView tv = getTabView(mSelectedPosition);
      if (tv != null) tv.setChecked(false);
    }

    mSelectedPosition = position;
    CheckedTextView tv = getTabView(mSelectedPosition);
    if (tv != null) tv.setChecked(true);

    animateToTab(position);
  }
コード例 #3
0
  @Override
  public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    if (mListener != null) mListener.onPageScrolled(position, positionOffset, positionOffsetPixels);

    CheckedTextView tv_scroll = getTabView(position);
    CheckedTextView tv_next = getTabView(position + 1);

    if (tv_scroll != null && tv_next != null) {
      int width_scroll = tv_scroll.getWidth();
      int width_next = tv_next.getWidth();
      float distance = (width_scroll + width_next) / 2f;

      int width = (int) (width_scroll + (width_next - width_scroll) * positionOffset + 0.5f);
      int offset =
          (int)
              (tv_scroll.getLeft()
                  + width_scroll / 2f
                  + distance * positionOffset
                  - width / 2f
                  + 0.5f);
      updateIndicator(offset, width);
    }
  }
コード例 #4
0
  private void notifyDataSetChanged() {
    mTabContainer.removeAllViews();

    PagerAdapter adapter = mViewPager.getAdapter();
    final int count = adapter.getCount();

    if (mSelectedPosition > count) mSelectedPosition = count - 1;

    for (int i = 0; i < count; i++) {
      CharSequence title = adapter.getPageTitle(i);
      if (title == null) title = "NULL";

      CheckedTextView tv = new CheckedTextView(getContext());
      tv.setCheckMarkDrawable(null);
      tv.setText(title);
      tv.setGravity(Gravity.CENTER);
      tv.setTextAppearance(getContext(), mTextApperance);
      tv.setSingleLine(true);
      tv.setEllipsize(TruncateAt.END);
      tv.setOnClickListener(this);
      tv.setTag(i);
      if (mTabRippleStyle > 0) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
          tv.setBackground(new RippleDrawable.Builder(getContext(), mTabRippleStyle).build());
        else
          tv.setBackgroundDrawable(
              new RippleDrawable.Builder(getContext(), mTabRippleStyle).build());
      }

      if (mMode == MODE_SCROLL) {
        tv.setPadding(mTabPadding, 0, mTabPadding, 0);
        mTabContainer.addView(
            tv,
            new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
      } else if (mMode == MODE_FIXED) {
        LinearLayout.LayoutParams params =
            new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT);
        params.weight = 1f;
        mTabContainer.addView(tv, params);
      }
    }

    setCurrentItem(mSelectedPosition);
    requestLayout();
  }