andorid模仿iOS的UISegmentedControl

效果图:
这里写图片描述

有关FileUtils这里写链接内容

主要代码:

package com.sun.framework.CustomizeVC;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import com.sun.framework.Utils.FileUtils;


public class SegmentedVC extends LinearLayout {

    public int mStrokeWidth;//边框宽度
    public int mStrokeColor;//边框夜色
    public int mSelectBgColor;//选中的背景颜色
    public int mUnSelectBgColor;//没选中的背景颜色
    public int mSelectTextColor;//选中的文本字体颜色
    public int mUnSelectTextColor;//没选中的文本字体颜色
    public int mDefaultSelectIndex;//默认选中的索引
    public int mTextSize;//字体大小
    public int mOrientation;//方向
    public int mBackgroundColor;//背景颜色
    public int[] mTextViewPadding;//new int[]{left, top, right, bottom}
    public int mCornerRadius;
    public String[] Texts = new String[]{"日","日","日","日","日","日","日","日","日","日","年"};
    private List<View> items;
    private Context context;
    private OnSegClickListener onSegClickListener;
    private int mSelectIndex;//选中的索引
    private boolean isSelectTwice;//是否选择两次
    private int frequency;//次数
    private FileUtils fileTool;
    protected final static String selectIndexPath="SegmentedVC/selectIndex";

    protected final static String selectTwicePath="SegmentedVC/IsSelectTwice";

    public boolean mIsUseLastTimeSelectIndex;//是否使用上次选择的索引
    public boolean mIsPersistenceSelectIndex;//是否持久户选择的索引

    public SegmentedVC(Context context) {
        super(context);
    }

    public SegmentedVC(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public SegmentedVC(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }

    private void  init(){
        fileTool = new FileUtils(context);
        mIsUseLastTimeSelectIndex = true;
        mIsPersistenceSelectIndex = true;
        mTextViewPadding = new int[]{10, 10, 10, 10};
        mStrokeWidth = 2;
        mStrokeColor = Color.WHITE;
        mSelectBgColor = Color.BLUE;
        mUnSelectBgColor = Color.argb(0,255,255,255);
        mSelectTextColor = Color.BLACK;
        mUnSelectTextColor = Color.WHITE;
        mCornerRadius = 15;
        mOrientation = HORIZONTAL;
        mBackgroundColor = Color.argb(0,255,255,255);
        mTextSize = 15;
    }

    public void refreshSeg(){
        if (this.getChildCount() > 0){
            this.removeAllViews();
        }
        this.setBackgroundColor(mBackgroundColor);
        this.setOrientation(mOrientation);
        if (null == Texts){
            return;
        }
        int orientation = this.getOrientation();
        GradientDrawable thisDrawable = new GradientDrawable();
        thisDrawable.setCornerRadius(mCornerRadius);
        this.setBackgroundDrawable(thisDrawable);
        items = new ArrayList<>();
        for (int i=0;i<Texts.length;i++){
            GradientDrawable drawable = new GradientDrawable();
            ShapeDrawable shapeDrawable = new ShapeDrawable();
            shapeDrawable.setAlpha(0);
            if (i == 0){
                if (orientation == HORIZONTAL){
                    drawable.setCornerRadii(new float[]{mCornerRadius,mCornerRadius,0,0,0,0,mCornerRadius,mCornerRadius});
                }else {
                    drawable.setCornerRadii(new float[]{mCornerRadius, mCornerRadius, mCornerRadius, mCornerRadius, 0, 0, 0, 0});
                }
            }else if (i == Texts.length-1){
                if (orientation == HORIZONTAL) {
                    drawable.setCornerRadii(new float[]{0, 0, mCornerRadius, mCornerRadius, mCornerRadius, mCornerRadius, 0, 0});
                }else  {
                    drawable.setCornerRadii(new float[]{0, 0, 0, 0, mCornerRadius, mCornerRadius, mCornerRadius, mCornerRadius});
                }
            }

            if (i%2 != 0 && Texts.length != 2){
                if (orientation == HORIZONTAL) {
                    shapeDrawable.setPadding(-mStrokeWidth, 0, -mStrokeWidth, 0);
                }else {
                    shapeDrawable.setPadding(0, -mStrokeWidth, 0, -mStrokeWidth);
                }
            }

            drawable.setStroke(mStrokeWidth,mStrokeColor);
            drawable.setColor(mUnSelectBgColor);
            Drawable[] layers = new Drawable[]{shapeDrawable,drawable};
            LayerDrawable layerDrawable = new LayerDrawable(layers);
            TextView textView = new TextView(context);
            textView.setBackgroundDrawable(layerDrawable);
            textView.setText(Texts[i]);
            textView.setTextColor(mUnSelectTextColor);
            textView.setGravity(Gravity.CENTER);
            textView.setPadding(mTextViewPadding[0],mTextViewPadding[1],mTextViewPadding[2],mTextViewPadding[3]);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,mTextSize);
            textView.setTag(i);
            this.addView(textView);
            items.add(textView);
            LayoutParams layoutParams = (LayoutParams)textView.getLayoutParams();
            layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
            layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            layoutParams.weight = 1;
        }

        for (View item : items){
            item.setClickable(true);
            item.setOnClickListener(On_Item_Click);
        }
        if (mIsUseLastTimeSelectIndex){
            if (isSelectTwice || getPersistenceIsSelectTwice()){
                if (mIsPersistenceSelectIndex){
                    Object obj = fileTool.getFileObjectContent(selectIndexPath);
                    if (null != obj){
                        mSelectIndex = (int)obj;
                    }
                }
                if (0 <= mSelectIndex && mSelectIndex < items.size()){
                    On_Item_Click.onClick(items.get(mSelectIndex));
                }else {
                    On_Item_Click.onClick(items.get(items.size()-1));
                }
            }else  {
                DefaultSelect();
            }
        }else {
            DefaultSelect();
        }
    }

    private boolean getPersistenceIsSelectTwice(){
        Object obj = fileTool.getFileObjectContent(selectTwicePath);
        if (null == obj){
            return false;
        }else {
            return (boolean)obj;
        }
    }

    private void DefaultSelect() {
        if (0 <= mDefaultSelectIndex && mDefaultSelectIndex < items.size()){
            On_Item_Click.onClick(items.get(mDefaultSelectIndex));
        }
    }

    OnClickListener On_Item_Click = new OnClickListener() {
        @Override
        public void onClick(View view) {
            int tag = (int)view.getTag();
            if (mIsUseLastTimeSelectIndex){
                mSelectIndex = tag;
                if (mIsPersistenceSelectIndex){
                    fileTool.saveFileContent(mSelectIndex,selectIndexPath);
                }
            }
            if (null != onSegClickListener){
                if (mIsUseLastTimeSelectIndex) {
                    if (frequency < 1) {
                        frequency++;
                    } else {
                        isSelectTwice = true;
                        if (mIsPersistenceSelectIndex) {
                            fileTool.saveFileContent(isSelectTwice, selectTwicePath);
                        }

                    }
                }
                onSegClickListener.OnSegClickListener(view,tag);
            }
            for (View item : items){
                TextView textView = (TextView)item;
                LayerDrawable layerDrawable = (LayerDrawable)textView.getBackground();
                if (null != layerDrawable) {
                    int layerDrawableSize = layerDrawable.getNumberOfLayers();
                    if (layerDrawableSize == 2) {
                        GradientDrawable drawable = (GradientDrawable) layerDrawable.getDrawable(1);
                        drawable.setColor(mUnSelectBgColor);
                    }
                }
                textView.setTextColor(mUnSelectTextColor);
            }

            TextView textView = (TextView)view;
            LayerDrawable layerDrawable = (LayerDrawable)textView.getBackground();
            if (null != layerDrawable) {
                int layerDrawableSize = layerDrawable.getNumberOfLayers();
                if (layerDrawableSize == 2) {
                    GradientDrawable drawable = (GradientDrawable) layerDrawable.getDrawable(1);
                    drawable.setColor(mSelectBgColor);
                }
            }
            textView.setTextColor(mSelectTextColor);
        }
    };

    public void setOnSegClickListener(OnSegClickListener onSegClickListener) {
        this.onSegClickListener = onSegClickListener;
    }

    public interface OnSegClickListener {
        boolean OnSegClickListener(View view, int index);
    }

}

使用:

xml布局:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data class="ForecastBinding">

    </data>

    <LinearLayout
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >

        <com.sun.framework.CustomizeVC.SegmentedVC
            android:id="@+id/segmentedVC"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="16dp"
            android:layout_width="200dp"
            android:layout_height="wrap_content">

        </com.sun.framework.CustomizeVC.SegmentedVC>

    </LinearLayout>

</layout>
ForecastBinding binding;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.forecast_fra, container, false);

        binding.segmentedVC.Texts = new String[]{"日","日","年"};
        binding.segmentedVC.refreshSeg();
        //点击回调
        binding.segmentedVC.setOnSegClickListener(new SegmentedVC.OnSegClickListener() {
            @Override
            public boolean OnSegClickListener(View view, int index) {

                return false;
            }
        });
        return binding.getRoot();
    }

我的业余技术微信公众号:YKJGZH,欢迎大家进入

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值