AndroidUI:用ItemDecoration實現(xiàn)物流時間軸

time_line.png

github 源碼

昨天看到有人實現(xiàn)了這個ui,但是主要在布局里寫的。當(dāng)然我之前也是在布局里寫的,但是貌似見過用ItemDecoration實現(xiàn)的。于是我評論說讓他用ItemDecoration試一下,他回復(fù)我想多了村缸。好吧,不管想的多不多武氓,我自己試下就知道可行不可行了梯皿。當(dāng)然是可行的,不然我也不會寫這篇文章了聋丝。還發(fā)現(xiàn)了這樣寫的優(yōu)點索烹。

  • 布局簡單
  • 可復(fù)用
  • 對ItemDecoration更了解

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="15dp"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/tv_describe"
        tools:text="客戶簽收"
        android:textSize="16sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_marginTop="5dp"
        android:textSize="12sp"
        tools:text="2018-6-1"
        android:id="@+id/tv_time"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

布局是再簡單不過了(用布局實現(xiàn)過的心里清楚)。

ItemDecoration

package com.xunevermore.timelinedecor;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * Created by Administrator on 2018/6/1 0001.
 * com.xunevermore.timelinedecor
 */

public class TimelineDecoration extends RecyclerView.ItemDecoration {


    private int width;//時間軸寬度
    private int top;//圓距離item頂部高度
    private Drawable goingDrawable;//綠色對勾圓
    private int goingDrawableSize;//綠色對勾圓的直徑
    private int dividerHeight;//線條粗細(xì)

    private int lintColor = 0xff999999;//線條顏色
    private Paint mPaint;

    private int ovalRadius = 12;//灰色圓的半徑


    public TimelineDecoration(int width, int top, Drawable goingDrawable,int goingDrawableSize, int dividerHeight) {
        this.width = width;
        this.top = top;
        this.goingDrawableSize = goingDrawableSize;
        this.goingDrawable = goingDrawable;
        this.dividerHeight = dividerHeight;
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        outRect.set(width, 0, 0, dividerHeight);
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {

        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);

            int top = child.getTop();
            int bottom = child.getBottom();

            //豎直線

            int left = parent.getPaddingLeft() + width / 2;
            c.drawRect(left,
                    i==0?this.top+goingDrawableSize:top,//第一個item線有空一段
                    left + dividerHeight,
                    bottom + dividerHeight,
                    mPaint);

            //小圓點

            int ovalCenterX = top + this.top + ovalRadius;
            if (i == 0) {
                goingDrawable.setBounds(left-goingDrawableSize/2,top+this.top,left+goingDrawableSize/2,top+this.top+goingDrawableSize);
                goingDrawable.draw(c);
            } else {
                c.drawCircle(left, ovalCenterX, ovalRadius, mPaint);
            }


            //分割線
            mPaint.setColor(lintColor);
            c.drawRect(parent.getPaddingLeft() + width, bottom, parent.getWidth() - parent.getPaddingRight(), bottom + dividerHeight, mPaint);


        }

    }
}

Activity

package com.xunevermore.timelinedecor;

import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.Arrays;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        BaseQuickAdapter<String,BaseViewHolder> adapter = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_transport,
                Arrays.asList(getResources().getStringArray(R.array.transport_list))) {
            @Override
            protected void convert(BaseViewHolder helper, String item) {
                TextView tvDescribe = helper.getView(R.id.tv_describe);
                TextView tvTime = helper.getView(R.id.tv_time);
                tvDescribe.setText(item);
                tvTime.setText("2018-06-01 12:00");

                int position = helper.getAdapterPosition();
                tvDescribe.setTextColor(position==0?0xff4caf65:0xff999999);
                tvTime.setTextColor(position==0?0xff4caf65:0xff999999);

            }
        };

        recyclerView.addItemDecoration(new TimelineDecoration(getDimen(R.dimen.time_line_width),
                getDimen(R.dimen.time_line_top),
                ContextCompat.getDrawable(this,R.drawable.ic_check_circle),
                getDimen(R.dimen.time_going_size),
                1));
        recyclerView.setAdapter(adapter);

    }

    private int getDimen(int dimeRes){
        return (int) getResources().getDimension(dimeRes);
    }


}

Ok,完事了弱睦。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末百姓,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子况木,更是在濱河造成了極大的恐慌垒拢,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,490評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件火惊,死亡現(xiàn)場離奇詭異求类,居然都是意外死亡,警方通過查閱死者的電腦和手機屹耐,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評論 3 395
  • 文/潘曉璐 我一進(jìn)店門尸疆,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人惶岭,你說我怎么就攤上這事寿弱。” “怎么了按灶?”我有些...
    開封第一講書人閱讀 165,830評論 0 356
  • 文/不壞的土叔 我叫張陵症革,是天一觀的道長。 經(jīng)常有香客問我鸯旁,道長噪矛,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,957評論 1 295
  • 正文 為了忘掉前任铺罢,我火速辦了婚禮艇挨,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘畏铆。我一直安慰自己雷袋,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,974評論 6 393
  • 文/花漫 我一把揭開白布辞居。 她就那樣靜靜地躺著楷怒,像睡著了一般。 火紅的嫁衣襯著肌膚如雪瓦灶。 梳的紋絲不亂的頭發(fā)上鸠删,一...
    開封第一講書人閱讀 51,754評論 1 307
  • 那天,我揣著相機與錄音贼陶,去河邊找鬼刃泡。 笑死,一個胖子當(dāng)著我的面吹牛碉怔,可吹牛的內(nèi)容都是我干的烘贴。 我是一名探鬼主播,決...
    沈念sama閱讀 40,464評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼撮胧,長吁一口氣:“原來是場噩夢啊……” “哼桨踪!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起芹啥,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤锻离,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后墓怀,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體汽纠,經(jīng)...
    沈念sama閱讀 45,847評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,995評論 3 338
  • 正文 我和宋清朗相戀三年傀履,在試婚紗的時候發(fā)現(xiàn)自己被綠了虱朵。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,137評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡钓账,死狀恐怖碴犬,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情官扣,我是刑警寧澤翅敌,帶...
    沈念sama閱讀 35,819評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站惕蹄,受9級特大地震影響恰响,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜沼撕,卻給世界環(huán)境...
    茶點故事閱讀 41,482評論 3 331
  • 文/蒙蒙 一侧啼、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧泪蔫,春花似錦棒旗、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽饶深。三九已至,卻和暖如春逛拱,著一層夾襖步出監(jiān)牢的瞬間敌厘,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評論 1 272
  • 我被黑心中介騙來泰國打工朽合, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留俱两,地道東北人。 一個月前我還...
    沈念sama閱讀 48,409評論 3 373
  • 正文 我出身青樓曹步,卻偏偏與公主長得像宪彩,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子讲婚,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,086評論 2 355

推薦閱讀更多精彩內(nèi)容