Ejemplo n.º 1
0
    @Override
    public View createTabView(ViewGroup container, int position, PagerAdapter adapter) {
      View tabView = mInflater.inflate(mTabViewLayoutId, container, false);

      View tabTextOrImageView = tabView.findViewById(mTabViewTextOrImageViewId);

      switch (mTabMode) {
        case TITLE_ONLY:
          {
            TextView tabTextView = (TextView) tabTextOrImageView;
            tabTextView.setText(adapter.getPageTitle(position));
            // TODO Set Color?
            break;
          }
        case ICON_ONLY:
          {
            if (adapter instanceof IconTabProvider) {
              ImageView tabImageView = (ImageView) tabTextOrImageView;
              tabImageView.setImageResource(((IconTabProvider) adapter).getPageIconResId(position));
              // TODO Set Color?
            } else {
              throw new ClassCastException(
                  "pager adapter must implements NiceTabLayout.IconTabProvider");
            }
            break;
          }
        case BOTH:
          {
            TextView tabTextView = (TextView) tabTextOrImageView;
            tabTextView.setText(adapter.getPageTitle(position));
            if (adapter instanceof IconTabProvider) {
              final int drawablePadding = tabTextView.getCompoundDrawablePadding();
              tabTextView.setCompoundDrawablesWithIntrinsicBounds(
                  0, ((IconTabProvider) adapter).getPageIconResId(position), 0, 0);
              tabTextView.setCompoundDrawablePadding(drawablePadding);
            } else {
              throw new ClassCastException(
                  "pager adapter must implements NiceTabLayout.IconTabProvider");
            }
            // TODO Set Color?
            break;
          }
        default:
          {
            throw new IllegalStateException("Invalid tab mode: " + mTabMode);
          }
      }

      return tabView;
    }
Ejemplo n.º 2
0
  private void populateTabStrip() {
    final PagerAdapter adapter = mViewPager.getAdapter();
    final View.OnClickListener tabClickListener = new TabClickListener();

    for (int i = 0; i < adapter.getCount(); i++) {
      View tabView = null;
      TextView tabTitleView = null;

      if (mTabViewLayoutId != 0) {
        // If there is a custom tab view layout id set, try and inflate it
        tabView = LayoutInflater.from(getContext()).inflate(mTabViewLayoutId, mTabStrip, false);
        tabTitleView = (TextView) tabView.findViewById(mTabViewTextViewId);
      }

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

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

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

      mTabStrip.addView(tabView);
    }
  }
 public void notifyDataSetChanged() {
   mTabLayout.removeAllViews();
   PagerAdapter adapter = mViewPager.getAdapter();
   IconPagerAdapter iconAdapter = null;
   if (adapter instanceof IconPagerAdapter) {
     iconAdapter = (IconPagerAdapter) adapter;
   }
   final int count = adapter.getCount();
   for (int i = 0; i < count; i++) {
     CharSequence title = adapter.getPageTitle(i);
     if (title == null) {
       title = EMPTY_TITLE;
     }
     int iconResId = 0;
     if (iconAdapter != null) {
       iconResId = iconAdapter.getIconResId(i);
     }
     addTab(i, title, iconResId);
   }
   if (mSelectedTabIndex > count) {
     mSelectedTabIndex = count - 1;
   }
   setCurrentItem(mSelectedTabIndex);
   requestLayout();
 }
Ejemplo n.º 4
0
 private void populateTabStrip() {
   final PagerAdapter adapter = mViewPager.getAdapter();
   final View.OnClickListener tabClickListener = new TabClickListener();
   for (int i = 0; i < adapter.getCount(); i++) {
     View tabView = null;
     TextView tabTitleView = null;
     if (mTabViewLayoutId != 0) {
       // If there is a custom tab view layout id set, try and inflate it
       tabView = LayoutInflater.from(getContext()).inflate(mTabViewLayoutId, mTabStrip, false);
       tabTitleView = (TextView) tabView.findViewById(mTabViewTextViewId);
     }
     if (tabView == null) {
       tabView = createDefaultTabView(getContext());
     }
     if (tabTitleView == null && TextView.class.isInstance(tabView)) {
       tabTitleView = (TextView) tabView;
     }
     if (mDistributeEvenly) {
       LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) tabView.getLayoutParams();
       lp.width = 0;
       lp.weight = 1;
     }
     tabTitleView.setText(adapter.getPageTitle(i));
     tabView.setOnClickListener(tabClickListener);
     String desc = mContentDescriptions.get(i, null);
     if (desc != null) {
       tabView.setContentDescription(desc);
     }
     mTabStrip.addView(tabView);
     if (i == mViewPager.getCurrentItem()) {
       tabView.setSelected(true);
     }
   }
 }
 private CharSequence getTitle(int i) {
   CharSequence title = mPagerAdapter.getPageTitle(i);
   if (title == null) {
     title = EMPTY_TITLE;
   }
   return title.toString();
 }
Ejemplo n.º 6
0
  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);
      }
    }
  }
  public void notifyDataSetChanged() {

    tabsContainer.removeAllViews();
    PagerAdapter adapter = pager.getAdapter();

    tabCount = adapter.getCount();

    for (int i = 0; i < tabCount; i++) {

      if (adapter instanceof IconTabProvider) {
        IconTabProvider ipa = ((IconTabProvider) adapter);
        if (TEKST_DER_FADER_OVER_I_IKONER) {
          Bitmap res = ipa.getPageIconBitmap(i);
          int resId = ipa.getPageIconResId(i);
          if (resId != 0 || res != null)
            addIconTabBådeTekstOgBillede(i, resId, res, ipa.getPageContentDescription(i));
          else addTextTab(i, adapter.getPageTitle(i).toString());
        } else {
          addIconTab(i, ipa.getPageIconResId(i), ipa.getPageContentDescription(i));
        }
      } else {
        addTextTab(i, adapter.getPageTitle(i).toString());
      }
    }

    updateTabStyles();

    getViewTreeObserver()
        .addOnGlobalLayoutListener(
            new OnGlobalLayoutListener() {

              @SuppressWarnings("deprecation")
              @SuppressLint("NewApi")
              @Override
              public void onGlobalLayout() {

                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                  getViewTreeObserver().removeGlobalOnLayoutListener(this);
                } else {
                  getViewTreeObserver().removeOnGlobalLayoutListener(this);
                }

                currentPosition = pager.getCurrentItem();
                scrollToChild(currentPosition, 0);
              }
            });
  }
Ejemplo n.º 8
0
 public void setTabsFromPagerAdapter(@NonNull PagerAdapter adapter) {
   removeAllTabs();
   for (int i = 0, count = adapter.getCount(); i < count; i++) {
     addTab(adapter.getPageTitle(i), getPageDrawable(adapter, i));
   }
   mSplit = new ImageView(getContext());
   mSplit.setBackgroundColor(splitColor);
   addView(mSplit, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
   invalidate();
   mCurrentSelectedPos = -1;
   setTabSelected(0, true);
 }
Ejemplo n.º 9
0
  private void notifyDataSetInvalidated() {
    PagerAdapter adapter = mViewPager.getAdapter();
    final int count = adapter.getCount();
    for (int i = 0; i < count; i++) {
      TextView tv = getTabView(i);

      CharSequence title = adapter.getPageTitle(i);
      if (title == null) title = "NULL";

      tv.setText(title);
    }

    requestLayout();
  }
Ejemplo n.º 10
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();
  }
  /*
   * return to the previously selected page in the viewPager
   */
  private void restorePreviousPage() {
    if (mViewPager == null || !hasPageAdapter()) {
      return;
    }

    String pageTitle = AppPrefs.getReaderSubsPageTitle();
    if (TextUtils.isEmpty(pageTitle)) {
      return;
    }

    PagerAdapter adapter = getPageAdapter();
    for (int i = 0; i < adapter.getCount(); i++) {
      if (pageTitle.equals(adapter.getPageTitle(i))) {
        mViewPager.setCurrentItem(i);
        return;
      }
    }
  }
Ejemplo n.º 12
0
 @Override
 public void notifyDataSetChanged() {
   mTabLayout.removeAllViews();
   PagerAdapter adapter = mViewPager.getAdapter();
   final int count = adapter.getCount();
   for (int i = 0; i < count; i++) {
     CharSequence title = adapter.getPageTitle(i);
     if (title == null) {
       title = EMPTY_TITLE;
     }
     addTab(title, i);
   }
   if (mSelectedTabIndex > count) {
     mSelectedTabIndex = count - 1;
   }
   setCurrentItem(mSelectedTabIndex);
   requestLayout();
 }
Ejemplo n.º 13
0
  private void populateTabStrip() {
    final PagerAdapter adapter = mViewPager.getAdapter();
    final View.OnClickListener tabClickListener = new TabClickListener();

    for (int i = 0; i < adapter.getCount(); i++) {
      View tabView = null;
      TextView tabTitleView = null;

      if (mTabViewLayoutId != 0) {
        // If there is a custom tab view layout id set, try and inflate it
        tabView = LayoutInflater.from(getContext()).inflate(mTabViewLayoutId, mTabStrip, false);
        LinearLayout.LayoutParams loparams = (LinearLayout.LayoutParams) tabView.getLayoutParams();
        loparams.weight = 1;
        //                loparams.height = 50;
        //                loparams.gravity = Gravity.CENTER;
        tabView.setLayoutParams(loparams);
        tabTitleView = (TextView) tabView.findViewById(mTabViewTextViewId);
        tabTitleView.setLayoutParams(
            new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1f));
      }

      if (tabView == null) {
        tabView = createDefaultTabView(getContext());
        LinearLayout.LayoutParams loparams = (LinearLayout.LayoutParams) tabView.getLayoutParams();
        loparams.weight = 1;
        //                loparams.height = 50;
        //                loparams.gravity = Gravity.CENTER;
        tabView.setLayoutParams(loparams);
      }

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

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

      mTabStrip.addView(tabView);
    }
  }
Ejemplo n.º 14
0
    @Override
    public View createTabView(ViewGroup container, int position, PagerAdapter adapter) {
      View tabView = null;
      TextView tabTitleView = null;

      if (tabViewLayoutId != NO_ID) {
        tabView = inflater.inflate(tabViewLayoutId, container, false);
      }

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

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

      if (tabTitleView != null) {
        tabTitleView.setText(adapter.getPageTitle(position));
      }

      return tabView;
    }
Ejemplo n.º 15
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;
  }