用Switch实现开关样式的tabLayout

效果:

ezgif.com-gif-maker (1).gif

实现:

my_switch.xml:

   




   

   

   


   

selector_switch_caclutor_track.xml:

    





selector_switch_caclutor.xml


   




 

carmodel_switch_tablayout_on.xml:

   









 

carmodel_switch_tablayout_on.xml:


   










  

styles.xml:


  

自定义SwitchCompat,MySwitchTabLayout.java



import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.SwitchCompat;
import android.util.AttributeSet;
import android.view.View;

import com.bitauto.carmodel.R;

import java.lang.reflect.Method;



public class MySwitchTabLayout extends ConstraintLayout {
    private SwitchCompat mySwitch;

    public MySwitchTabLayout(Context context) {
        super(context);
        init(context);
    }

    public MySwitchTabLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);

    }

    public MySwitchTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }


    private void init(Context context) {
        View inflate = inflate(context, R.layout.my_switch, this);
        mySwitch = inflate.findViewById(R.id.switch);
    }


    public void setUpWithViewPager(final ViewPager viewPager) {
        if (viewPager != null) {
            viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                    float newPos = constrain(positionOffset, 0, 1);
                    try {
                        if (newPos != 0.0f && newPos != 1f) {
                            //通过反射修改Switch的UI
                            Class switchClass = SwitchCompat.class;
                            Method setThumbPosition = switchClass.getDeclaredMethod("setThumbPosition", float.class);
                            setThumbPosition.setAccessible(true);
                            setThumbPosition.invoke(mySwitch, newPos);
                        }
                        MySwitchTabLayout.this.invalidate();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                @Override
                public void onPageSelected(int position) {
                }
                @Override
                public void onPageScrollStateChanged(int state) {
                }
            });
            mySwitch.setOnCheckedChangeListener((buttonView, isChecked) -> viewPager.setCurrentItem(isChecked ? 1 : 0));
        } else {
            throw new NullPointerException("viewPager不能为空");
        }

    }


    private float constrain(float amount, float low, float high) {
        return amount < low ? low : (amount > high ? high : amount);
    }
}


你可能感兴趣的:(用Switch实现开关样式的tabLayout)