rey5137 / material

A library to bring fully animated Material Design components to pre-Lolipop Android.
http://rey5137.com/material/
Apache License 2.0
6k stars 1.32k forks source link

Does TabPageIndicator support Icon tab #193

Closed oliguo closed 9 years ago

oliguo commented 9 years ago

According to WIKI:https://github.com/rey5137/material/wiki/TabPageIndicator I cannot find any detail to change from text to icon like(SmartTabLayout),

so,I watch your source(TabPageIndicator.java) and try to override something, but I cannot figure out, I appreciate if you can help me, thanks.

oliguo commented 9 years ago

I change all CheckedTextView to ImageView , and add ImageView a image, it works. My code:

   private ImageView getTabView(int position){
    return (ImageView)mTabContainer.getChildAt(position);
}

private void animateToTab(final int position) {
    final ImageView tv = getTabView(position);
    if(tv == null)
        return;

    if (mTabAnimSelector != null)
        removeCallbacks(mTabAnimSelector);

    mTabAnimSelector = new Runnable() {
        public void run() {
            if(!mScrolling)
                updateIndicator(tv.getLeft(), tv.getWidth());

            smoothScrollTo(tv.getLeft() - (getWidth() - tv.getWidth()) / 2 + getPaddingLeft(), 0);
            mTabAnimSelector = null;
        }
    };

    post(mTabAnimSelector);
}
@Override
public void onPageScrollStateChanged(int state) {
    if(state == ViewPager.SCROLL_STATE_IDLE){
        mScrolling = false;
        ImageView tv = getTabView(mSelectedPosition);
        if(tv != null)
            updateIndicator(tv.getLeft(), tv.getMeasuredWidth());
    }
    else
        mScrolling = true;

    if (mListener != null)
        mListener.onPageScrollStateChanged(state);
}

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    if (mListener != null)
        mListener.onPageScrolled(position, positionOffset, positionOffsetPixels);

    ImageView tv_scroll = getTabView(position);
    ImageView 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);
    }
}

/**
 * Set the current page of this CustomTabPageIndicator.
 * @param position The position of current page.
 */
public void setCurrentItem(int position) {
    if(mSelectedPosition != position){
        ImageView tv = getTabView(mSelectedPosition);
        if(tv != null) {
            //tv.setChecked(false);
        }
    }

    mSelectedPosition = position;
    ImageView tv = getTabView(mSelectedPosition);
    if(tv != null) {
        //tv.setChecked(true);
    }

    animateToTab(position);
}

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);
        int img=0;
        if (title == null) {
            title = "NULL";
        }else{
            img= R.mipmap.ic_launcher;
        }

        ImageView tv = new ImageView(getContext());
        tv.setImageResource(img);
 //            tv.setCheckMarkDrawable(null);
 //            tv.setText(title);
 //            tv.setGravity(Gravity.CENTER);
 //            tv.setTextAppearance(getContext(), mTextAppearance);
 //            tv.setSingleLine(true);
 //            tv.setEllipsize(TextUtils.TruncateAt.END);
        tv.setOnClickListener(this);
        tv.setTag(i);
        if(mTabRippleStyle > 0)
            ViewUtil.setBackground(tv, 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();
}

private void notifyDataSetInvalidated() {
    PagerAdapter adapter = mViewPager.getAdapter();
    final int count = adapter.getCount();
    for (int i = 0; i < count; i++) {
        ImageView tv = getTabView(i);

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

       // tv.setText(title);
        tv.setImageResource(R.mipmap.ic_launcher);

    }

    requestLayout();
}

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";

        ImageView tv = new ImageView(getContext());
        tv.setImageResource(R.mipmap.ic_launcher);
 //            tv.setCheckMarkDrawable(null);
 //            tv.setText(title);
 //            tv.setGravity(Gravity.CENTER);
  //            tv.setTextAppearance(getContext(), mTextAppearance);
    //            tv.setSingleLine(true);
      //            tv.setEllipsize(TextUtils.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);
        }
    }
}
TonyAnine commented 8 years ago

@oliguo I do as you.It works,but ripple effect is disappeared.How can I do?