Android自定义物流进度布局StepProgressLayout

一、实际项目效果图
效果图.png
二、效果分析
效果分析.png
  1. 条目有三种样式:第一条、中间部分、最后一条
  2. 一个条目分成三个部分,二条线加一个图标
  3. 图标大小有二种样式
  4. 二条线均可以设置显示或者隐藏
三、自定义属性
<declare-styleable name="StepProgressLayout">
        <!--第一个条目的图片、大小-->
        <attr name="step_first_image" format="reference" />
        <attr name="step_first_image_size" format="dimension" />
        <!--第一条Top线的高度-->
        <attr name="step_first_line_height" format="dimension" />
        <!--正常条目的图片、大小-->
        <attr name="step_normal_image" format="reference" />
        <attr name="step_normal_image_size" format="dimension" />
        <!--正常条目Top线的高度-->
        <attr name="step_normal_line_height" format="dimension" />
        <!--线的大小、线的颜色-->
        <attr name="step_line_size" format="dimension" />
        <attr name="step_line_color" format="color|reference" />
       <!--条目样式-->
        <attr name="step_style" format="enum">
            <enum name="first" value="0" />
            <enum name="normal" value="1" />
            <enum name="end" value="2" />
        </attr>
    </declare-styleable>
四、自定义(R.layout.layout_view_step)布局样式
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <View
        android:id="@+id/step_top_line"
        android:layout_width="1px"
        android:layout_height="22dp"
        android:background="#e6e6e6"
        app:layout_constraintBottom_toTopOf="@id/step_icon"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/step_icon"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:src="@drawable/ic_supplier_on"
        app:layout_constraintBottom_toTopOf="@id/step_bottom_line"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/step_top_line" />

    <View
        android:id="@+id/step_bottom_line"
        android:layout_width="1px"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#e6e6e6"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/step_icon" />
</androidx.constraintlayout.widget.ConstraintLayout>
  • 布局很简单,自己看看就行
  • 对于ConstraintLayout 不会用的自己去百度
五、自定义StepProgressLayout布局
package cn.carhouse.yctone.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import cn.carhouse.yctone.R;

/**
 * 物流进度的View
 */
public class StepProgressLayout extends ConstraintLayout {
    private int mFirstImage;
    private int mFirstImageSize;
    private int mNormalImage;
    private int mNormalImageSize;
    private int mLineSize;
    private int mLineColor = Color.parseColor("#e6e6e6");
    private int mFirstLineHeight, mNormalLineHeight;
    private int mStepStyle = STYLE_NORMAL;// 进度样式
    private static final int STYLE_FIRST = 0;//  第一个条目样式
    private static final int STYLE_NORMAL = 1;//  正常条目样式
    private static final int STYLE_END = 2;//  最后一条条目样式

    private View mTopLine, mBottomLine;
    private ImageView mIcon;

    public StepProgressLayout(@NonNull Context context) {
        this(context, null);
    }

    public StepProgressLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public StepProgressLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // 加载布局
        inflate(context, R.layout.layout_view_step, this);
        // 加载自定义的属性
        obtainStyledAttributes(context, attrs);
        // 初始化View
        initViews();
    }


    private void obtainStyledAttributes(Context context, AttributeSet attrs) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.StepProgressLayout);
        mFirstImage = array.getResourceId(R.styleable.StepProgressLayout_step_first_image, 0);
        mFirstImageSize = (int) array.getDimension(R.styleable.StepProgressLayout_step_first_image_size, dip2px(20));
        mNormalImage = array.getResourceId(R.styleable.StepProgressLayout_step_normal_image, 0);
        mNormalImageSize = (int) array.getDimension(R.styleable.StepProgressLayout_step_normal_image_size, dip2px(11));
        mLineSize = (int) array.getDimension(R.styleable.StepProgressLayout_step_line_size, 1);
        mLineColor = array.getColor(R.styleable.StepProgressLayout_step_line_color, mLineColor);
        mFirstLineHeight = (int) array.getDimension(R.styleable.StepProgressLayout_step_first_line_height, dip2px(17));
        mNormalLineHeight = (int) array.getDimension(R.styleable.StepProgressLayout_step_normal_line_height, dip2px(17));
        array.recycle();
    }

    private void initViews() {
        mTopLine = findViewById(R.id.step_top_line);
        mBottomLine = findViewById(R.id.step_bottom_line);
        mIcon = findViewById(R.id.iv_icon);

        if (mStepStyle == STYLE_FIRST) {
            setImageResource(mFirstImage);
            setImageSize(mFirstImageSize);
            mTopLine.setVisibility(INVISIBLE);
        } else if (mStepStyle == STYLE_NORMAL || mStepStyle == STYLE_END) {
            setImageResource(mNormalImage);
            setImageSize(mNormalImageSize);
            if (mStepStyle == STYLE_END) {
                mBottomLine.setVisibility(INVISIBLE);
            }
        }
        setTopLineHeight(mFirstLineHeight);
        setLineColor(mLineColor);
        setLineSize(mLineSize);

    }

    public void setLineColor(int color) {
        if (color != 0) {
            mBottomLine.setBackgroundColor(color);
            mTopLine.setBackgroundColor(color);
        }
    }

    /**
     * 设置第一条线第一个条目的高度
     */
    public void setFirstLineHeight(int height) {
        this.mFirstLineHeight = height;
    }

    /**
     * 设置第一条线正常样式的高度
     */
    public void setNormalLineHeight(int height) {
        this.mNormalLineHeight = height;
    }

    public void setTopLineHeight(int height) {
        if (height != 0) {
            ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) mTopLine.getLayoutParams();
            params.height = height;
            mTopLine.setLayoutParams(params);
        }
    }

    /**
     * 设置线的大小
     */
    public void setLineSize(int width) {
        this.mLineSize = width;
        if (this.mLineSize != 0) {
            ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) mTopLine.getLayoutParams();
            params.width = width;
            mTopLine.setLayoutParams(params);

            params = (ConstraintLayout.LayoutParams) mBottomLine.getLayoutParams();
            params.width = width;
            mBottomLine.setLayoutParams(params);
        }
    }


    /**
     * 设置第一个图片资源
     */
    public void setHeadImageSize(int size) {
        this.mFirstImageSize = size;
        setImageSize(this.mFirstImageSize);
    }


    private void setImageResource(int resource) {
        if (resource != 0) {
            mIcon.setImageResource(resource);
        }
    }

    private void setImageSize(int size) {
        if (size >= 0) {
            ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) mIcon.getLayoutParams();
            params.height = size;
            params.width = size;
            mIcon.setLayoutParams(params);
        }
    }

    /**
     * 变成第一个条目样式
     */
    public void changeFirstStyle() {
        if (mStepStyle != STYLE_FIRST) {
            mStepStyle = STYLE_FIRST;
            mTopLine.setVisibility(INVISIBLE);
            setImageResource(mFirstImage);
            setHeadImageSize(mFirstImageSize);
            setTopLineHeight(mFirstLineHeight);
        }
    }

    /**
     * 变成正常条目样式
     */
    public void changeNormalStyle() {
        if (mStepStyle != STYLE_NORMAL) {
            mStepStyle = STYLE_NORMAL;
            mBottomLine.setVisibility(VISIBLE);
            mTopLine.setVisibility(VISIBLE);
            setImageResource(mNormalImage);
            setImageSize(mNormalImageSize);
            setTopLineHeight(mFirstLineHeight);
        }
    }

    /**
     * 最后一种样式
     */
    public void changeEndStyle() {
        if (mStepStyle != STYLE_END) {
            mStepStyle = STYLE_END;
            mTopLine.setVisibility(VISIBLE);
            mBottomLine.setVisibility(INVISIBLE);
            setImageResource(mNormalImage);
            setImageSize(mNormalImageSize);
            setTopLineHeight(mFirstLineHeight);
        }
    }

    private int dip2px(int dip) {
        return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, getResources().getDisplayMetrics()) + 0.5);
    }
}

提供了一些常用方法,每个公司的UI布局不一样,自己稍作调整就好。

六、列表条目(R.layout.supplier_item_express_track_item)布局技巧
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <!--换成你的View的位置-->
    <your view packet .StepProgressLayout
        android:id="@+id/step_view"
        android:layout_width="50dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@id/v_item_bottom_line"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:step_first_image="@drawable/ic_supplier_express_first"
        app:step_normal_image="@drawable/ic_supplier_express_normal"
        app:step_style="normal" />

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="17dp"
        android:layout_marginRight="24dp"
        android:lineSpacingMultiplier="1.2"
        android:text="[深圳市] 快件已签收,感谢您!,期[深圳市] 快件已签收快件已签收快件已签收快件已签收快件已签收"
        android:textColor="@color/c_aa"
        android:textSize="14dp"
        app:layout_constraintLeft_toRightOf="@id/step_view"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@id/step_view" />

    <TextView
        android:id="@+id/tv_time"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:layout_marginRight="24dp"
        android:lineSpacingMultiplier="1.2"
        android:text="2016-07-12  12 : 30"
        android:textColor="@color/c_aa"
        android:textSize="14dp"
        app:layout_constraintLeft_toLeftOf="@id/tv_name"
        app:layout_constraintLeft_toRightOf="@id/step_view"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tv_name" />

    <View
        android:id="@+id/v_item_bottom_line"
        android:layout_width="0dp"
        android:layout_height="2px"
        android:layout_marginTop="14dp"
        android:background="@color/line_bg"
        app:layout_constraintLeft_toLeftOf="@id/tv_name"
        app:layout_constraintLeft_toRightOf="@id/step_view"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tv_time" />
</androidx.constraintlayout.widget.ConstraintLayout>

用底部的线来约束限制左边自己定义View的高度

七、列表Adapter测试代码
mRecyclerView.setAdapter(new XQuickAdapter<String>(getAppActivity(), items, R.layout.supplier_item_express_track_item) {
            @Override
            public void convert(XQuickViewHolder holder, String item, int position) {
                StepProgressLayout step = holder.getView(R.id.step_view);
                TextView tvName = holder.getView(R.id.tv_name);
                TextView tvTime = holder.getView(R.id.tv_time);
                // 第一个位置
                if (position == 0) {
                    step.changeFirstStyle();
                    holder.setBold(tvName, true);
                    holder.setBold(tvTime, true);
                    tvName.setTextColor(Color.parseColor("#FFDD2828"));
                    tvTime.setTextColor(Color.parseColor("#FFDD2828"));
                } else {
                    // 最后一个位置
                    if (position == getCount() - 1) {
                        step.changeEndStyle();
                    } else {
                        step.changeNormalStyle();
                    }
                    holder.setBold(tvName, false);
                    holder.setBold(tvTime, false);
                    tvName.setTextColor(Color.parseColor("#FFAAAAAA"));
                    tvTime.setTextColor(Color.parseColor("#FFAAAAAA"));
                }
            }
        });
  1. 看convert方法里面的逻辑就好,实际开发用自己的Adapter
  2. 源码:https://github.com/wenkency/views
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容