android 日程安排view,RecyclerView 列表控件中简单实现时间线

时间

时间,时间,时间啊;走慢一点吧~

看见很多软件中都有时间线的东西,貌似天气啊,旅游啊什么的最多了;具体实现方式很多,在本篇文章中讲解一种自定义View封装的方式。

效果

先来看看效果。

1440469250572021.png

分析

软件中,可以看见前面的时间线也就是线条加上圆圈组成;当然这里的圆圈与线条也都是可以随意换成其他的,比如图片等等。

当然这里最简单的来说,是上面一个线条,然后一个圆圈,然后下面一个线条;上线条在第一条数据时不做显示,下线条在最后一条数据时不做显示。

1440469277121276.png

这里自定义布局部分也就是把旁边的线条与圆圈封装到一起,并使用简单的方法来控制是否显示。 当封装好了后,与旁边的文字部分也就是水瓶方向的线性布局了,然后设置为每一个的RecyclerView 的Item的布局也就完成了。

控件

控件很简单,首先我们继承View,取名为 TimeLineMarker 就OK。

Attrs 属性

开始控件之前先准备好需要的属性。<?xml  version="1.0" encoding="utf-8"?>

在这里也就准备了线条的大小、开始线条、结束线条、中间标示部分及大小。

属性与现实private int mMarkerSize = 24;

private int mLineSize = 12;

private Drawable mBeginLine;

private Drawable mEndLine;

private Drawable mMarkerDrawable;

@Override

protected void onDraw(Canvas canvas) {

if (mBeginLine != null) {

mBeginLine.draw(canvas);

}

if (mEndLine != null) {

mEndLine.draw(canvas);

}

if (mMarkerDrawable != null) {

mMarkerDrawable.draw(canvas);

}

super.onDraw(canvas);

}

两个大小属性,3个具体的Drawable,然后在onDraw方法中进行具体的显示也就OK。

构造与属性初始化

在上面我们定义了属性,在这里我们在构造函数中获取XML所设置的属性。public TimeLineMarker(Context context) {

this(context, null);

}

public TimeLineMarker(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public TimeLineMarker(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

init(attrs);

}

private void init(AttributeSet attrs) {

// Load attributes

final TypedArray a = getContext().obtainStyledAttributes(

attrs, R.styleable.TimeLineMarker, 0, 0);

mMarkerSize = a.getDimensionPixelSize(

R.styleable.TimeLineMarker_markerSize,

mMarkerSize);

mLineSize = a.getDimensionPixelSize(

R.styleable.TimeLineMarker_lineSize,

mLineSize);

mBeginLine = a.getDrawable(

R.styleable.TimeLineMarker_beginLine);

mEndLine = a.getDrawable(

R.styleable.TimeLineMarker_endLine);

mMarkerDrawable = a.getDrawable(

R.styleable.TimeLineMarker_marker);

a.recycle();

if (mBeginLine != null)

mBeginLine.setCallback(this);

if (mEndLine != null)

mEndLine.setCallback(this);

if (mMarkerDrawable != null)

mMarkerDrawable.setCallback(this);

}

Drawable 的位置与大小初始化

属性啥的有了,具体的Drawable 也有了,要显示的地方调用也是OK了;但是如果没有进行进行具体的位置调整这一切也都没有意义。@Override

protected void onSizeChanged(int w, int h, int oldw, int oldh) {

super.onSizeChanged(w, h, oldw, oldh);

initDrawableSize();

}

private void initDrawableSize() {

int pLeft = getPaddingLeft();

int pRight = getPaddingRight();

int pTop = getPaddingTop();

int pBottom = getPaddingBottom();

int width = getWidth();

int height = getHeight();

int cWidth = width - pLeft - pRight;

int cHeight = height - pTop - pBottom;

Rect bounds;

if (mMarkerDrawable != null) {

// Size

int markerSize = Math.min(mMarkerSize, Math.min(cWidth, cHeight));

mMarkerDrawable.setBounds(pLeft, pTop,

pLeft + markerSize, pTop + markerSize);

bounds = mMarkerDrawable.getBounds();

} else {

bounds = new Rect(pLeft, pTop, pLeft + cWidth, pTop + cHeight);

}

int halfLineSize = mLineSize >> 1;

int lineLeft = bounds.centerX() - halfLineSize;

if (mBeginLine != null) {

mBeginLine.setBounds(lineLeft, 0, lineLeft + mLineSize, bounds.top);

}

if (mEndLine != null) {

mEndLine.setBounds(lineLeft, bounds.bottom, lineLeft + mLineSize, height);

}

}

initDrawableSize 方法进行具体的运算,而运算的时间点就是当控件的大小改变(onSizeChanged)的时候。

在初始化中采用了一定的投机取巧;这里利用了上内边距与下内边距分别作为上线条与下线条的长度;而线条与中间的标识都采用了水平距中。

其他设置方法public void setLineSize(int lineSize) {

if (mLineSize != lineSize) {

this.mLineSize = lineSize;

initDrawableSize();

invalidate();

}

}

public void setMarkerSize(int markerSize) {

if (this.mMarkerSize != markerSize) {

mMarkerSize = markerSize;

initDrawableSize();

invalidate();

}

}

public void setBeginLine(Drawable beginLine) {

if (this.mBeginLine != beginLine) {

this.mBeginLine = beginLine;

if (mBeginLine != null) {

mBeginLine.setCallback(this);

}

initDrawableSize();

invalidate();

}

}

public void setEndLine(Drawable endLine) {

if (this.mEndLine != endLine) {

this.mEndLine = endLine;

if (mEndLine != null) {

mEndLine.setCallback(this);

}

initDrawableSize();

invalidate();

}

}

public void setMarkerDrawable(Drawable markerDrawable) {

if (this.mMarkerDrawable != markerDrawable) {

this.mMarkerDrawable = markerDrawable;

if (mMarkerDrawable != null) {

mMarkerDrawable.setCallback(this);

}

initDrawableSize();

invalidate();

}

}

在设置中,首先判断是否更改,如果更改那么就更新并重新计算位置;随后刷新界面。到这里,控件差不多准备OK了,其中还有很多可以完善的地方,比如加上快捷设置颜色什么的,也可以加上大小计算的东西。同时还可以加上时间线是水瓶还是垂直等等。在这里就不累赘介绍哪些了。下面来看看如何使用。

使用

XML布局

ITEM布局item_time_line.xml<?xml  version="1.0" encoding="utf-8"?>

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:paddingLeft="@dimen/lay_16"

android:paddingRight="@dimen/lay_16"

tools:ignore="MissingPrefix">

android:id="@+id/item_time_line_mark"

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:paddingBottom="@dimen/lay_16"

android:paddingLeft="@dimen/lay_4"

android:paddingRight="@dimen/lay_4"

android:paddingTop="@dimen/lay_16"

app:beginLine="@color/black_alpha_32"

app:endLine="@color/black_alpha_32"

app:lineSize="2dp"

app:marker="@drawable/ic_timeline_default_marker"

app:markerSize="24dp" />

android:id="@+id/item_time_line_txt"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:paddingBottom="@dimen/lay_16"

android:paddingLeft="@dimen/lay_4"

android:paddingRight="@dimen/lay_4"

android:paddingTop="@dimen/lay_16"

android:textColor="@color/grey_600"

android:textSize="@dimen/font_16" />

在这里我们之间使用顺序布局,左边是TimelIne控件,右边是一个简单的字体控件,具体使用中可以细化一些。 在TImeLine控件中我们的Mark是使用的drawable/ic_timeline_default_marker;这个就是一个简单的圆圈而已;对于自己美化可以使用一张图片代替或者更加复杂的布局;当然上面的线条就更加简单了,就直接使用颜色代替。<?xml  version="1.0" encoding="utf-8"?>

android:shape="oval">

android:width="1dp"

android:color="@color/black_alpha_32" />

主界面XML RecyclerView

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context=".MainActivity">

android:id="@+id/time_line_recycler"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:clickable="true"

android:fadeScrollbars="true"

android:fadingEdge="none"

android:focusable="true"

android:focusableInTouchMode="true"

android:overScrollMode="never"

android:scrollbarSize="2dp"

android:scrollbarThumbVertical="@color/cyan_500"

android:scrollbars="vertical" />

在这里就是加上了一个RecyclerView 控件在主界面就OK。

Java代码部分

在开始之前先来看看我们的文件具体有些神马。

1440469609114998.pngwidget中就是具体的自定义控件,model是具体的数据模型,adapter部分,这里有一个Recyclerview的adapter文件,以及一个具体的Item TimeLineViewHolder,当然在这里还定义了一个ItemType类,该类用来标示每个Item的类型,比如头部,第一个,普通,最后一个,底部等等。

TimeLineModel.javapackage net.qiujuer.example.timeline.model;

/**

* Created by qiujuer

* on 15/8/23.

*/

public class TimeLineModel {

private String name;

private int age;

public TimeLineModel() {

}

public TimeLineModel(String name, int age) {

this.name = name;

this.age = age;

}

public int getAge() {

return age;

}

public String getName() {

return name;

}

public void setAge(int age) {

this.age = age;

}

public void setName(String name) {

this.name = name;

}

}

一个名字,一个年龄也就OK。

ItemType.javapackage net.qiujuer.example.timeline.adapter;

/**

* Created by qiujuer

* on 15/8/23.

*/

public class ItemType {

public final static int NORMAL = 0;

public final static int HEADER = 1;

public final static int FOOTER = 2;

public final static int START = 4;

public final static int END = 8;

public final static int ATOM = 16;

}

分别定义了几个静态值,分别代表普通、头部、底部、开始、结束、原子;当然其中有些可以不用定义。

TimeLineViewHolder.javapackage net.qiujuer.example.timeline.adapter;

import android.support.v7.widget.RecyclerView;

import android.view.View;

import android.widget.TextView;

import net.qiujuer.example.timeline.R;

import net.qiujuer.example.timeline.model.TimeLineModel;

import net.qiujuer.example.timeline.widget.TimeLineMarker;

/**

* Created by qiujuer

* on 15/8/23.

*/

public class TimeLineViewHolder extends RecyclerView.ViewHolder {

private TextView mName;

public TimeLineViewHolder(View itemView, int type) {

super(itemView);

mName = (TextView) itemView.findViewById(R.id.item_time_line_txt);

TimeLineMarker mMarker = (TimeLineMarker) itemView.findViewById(R.id.item_time_line_mark);

if (type == ItemType.ATOM) {

mMarker.setBeginLine(null);

mMarker.setEndLine(null);

} else if (type == ItemType.START) {

mMarker.setBeginLine(null);

} else if (type == ItemType.END) {

mMarker.setEndLine(null);

}

}

public void setData(TimeLineModel data) {

mName.setText("Name:" + data.getName() + " Age:" + data.getAge());

}

}

该文件为RecyclerView 的Adapter中每个Item需要实现的Holder类。 在该类中,我们在构造函数中需要传入一个根View同时传入一个当然item的状态。 随后使用find….找到控件,在这里我们把TextView保存起来,而TimeLineView找到后直接进行初始化设置。 根据传入的ItemType来判断是否是第一个,最后一个,以及原子;然后设置TimeLineView的属性。 在下面的setData方法中我们显示具体的Model数据。

TimeLineAdapter.java

适配器部分,我们需要做的工作是;根据具体的数据渲染上对应的界面就OK。package net.qiujuer.example.timeline.adapter;

import android.support.v7.widget.RecyclerView;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import net.qiujuer.example.timeline.R;

import net.qiujuer.example.timeline.model.TimeLineModel;

import java.util.List;

/**

* Created by qiujuer

* on 15/8/23.

*/

public class TimeLineAdapter extends RecyclerView.Adapter {

private List mDataSet;

public TimeLineAdapter(List models) {

mDataSet = models;

}

@Override

public int getItemViewType(int position) {

final int size = mDataSet.size() - 1;

if (size == 0)

return ItemType.ATOM;

else if (position == 0)

return ItemType.START;

else if (position == size)

return ItemType.END;

else return ItemType.NORMAL;

}

@Override

public TimeLineViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

// Create a new view.

View v = LayoutInflater.from(viewGroup.getContext())

.inflate(R.layout.item_time_line, viewGroup, false);

return new TimeLineViewHolder(v, viewType);

}

@Override

public void onBindViewHolder(TimeLineViewHolder timeLineViewHolder, int i) {

timeLineViewHolder.setData(mDataSet.get(i));

}

@Override

public int getItemCount() {

return mDataSet.size();

}

}在这里需要着重说一下:我复写了getItemViewType方法;在该方法中我们需要设置对应的Item的类型;在这里传入的是item的坐标,需要返回的是item的具体状态,该状态标示是int类型;在这里我使用的是ItemType的静态属性。

该方法会在调用onCreateViewHolder方法之前调用;而onCreateViewHolder方法中的第二个参数int值也就是从getItemViewType之中来;所以我们可以在这里进行对应的数据状态标示。

而在onCreateViewHolder方法中我们返回一个:TimeLineViewHolder就OK,随后在onBindViewHolder方法中进行数据初始化操作。

MainActivity.java

上面所有都准备好了,下面就进行具体的显示。 在这里就只贴出核心代码了;篇幅也是有些长。private RecyclerView mRecycler;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mRecycler = (RecyclerView) findViewById(R.id.time_line_recycler);

initRecycler();

}

private void initRecycler() {

LinearLayoutManager layoutManager = new LinearLayoutManager(this);

layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

TimeLineAdapter adapter = new TimeLineAdapter(getData());

mRecycler.setLayoutManager(layoutManager);

mRecycler.setAdapter(adapter);

}

private List getData() {

List models = new ArrayList();

models.add(new TimeLineModel("XiaoMing", 21));

models.add(new TimeLineModel("XiaoFang", 20));

models.add(new TimeLineModel("XiaoHua", 25));

models.add(new TimeLineModel("XiaoA", 22));

models.add(new TimeLineModel("XiaoNiu", 23));

return models;

}

在这里就是傻瓜的操作了,流程就是准备好对应的数据,装进Adapter,准备好对应的布局方式,然后都设置到RecyclerView中就OK。

效果

来看看具体的效果:

1440469865127253.png

效果虽然简单,但是也算是五脏具全;其中无非就是控件的自定义。这个自定义是可以扩展的,大家可以扩展为水平方向试试。

代码

写在最后

文章的开始截屏来源于:最近没事儿捣鼓了一个APP[UPMiss],一个简单的生日,纪念日提醒软件;欢迎大家尝鲜。

{UPMiss} 思念你的夏天 下载地址:百度 这个审核有问题,明明没有支付的东西,结果说有支付的SDK存在,不得不说百度的自动审核有很大漏洞。

豌豆荚 新版2.0还在审核中!

======================================================== 作者:qiujuer

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/554180.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

小技巧来助阵 玩转Chrome浏览器

核心提示&#xff1a;Chrome问世已经有段时间了&#xff0c;相关的应用技巧也开始被挖掘出来&#xff0c;这里小编教你3则小技巧&#xff0c;让Chrome更满足你的需求。 Chrome问世已经有段时间了&#xff0c;相关的应用技巧也开始被挖掘出来&#xff0c;这里小编教你3则小技巧…

chrome浏览器遭eFast浏览器恶意软件删除取代

近日&#xff0c;有一款称为 eFast 浏览器的新恶意软件。该恶意软件从表面上看起来很像谷歌浏览器&#xff0c;但它会执行删除Chrome浏览器的操作&#xff0c;然后自我安装替代Chrome浏览器&#xff0c;并将自身设置为默认浏览器。之后&#xff0c;当你打开“浏览器”时&#x…

android rxjava2 简书,RXJava2学习

什么是RxJava一个可观测的序列来组成异步的、基于事件的程序的库。(简单来说&#xff1a;它就是一个实现异步操作的库)RxJava 好在哪?RxJava 其实就是提供一套异步编程的 API&#xff0c;这套 API 是基于观察者模式的&#xff0c;而且是链式调用的&#xff0c;所以使用 RxJava…

谷歌Chrome:将逐步阻止浏览器不安全下载内容

谷歌浏览器是一款非常好用的浏览服务软件&#xff0c;用户可以使用手机获取更多的线上内容&#xff0c;随时都可以使用手机下载想要的内容&#xff0c;这款软件最近对于功能进行了更改&#xff0c;用户在使用这款软件下载应用和需要的资讯时&#xff0c;会对下载的内容更加的严…

最新版谷歌浏览器Chrome45版本性能提升

最新版谷歌浏览器Chrome45版本性能提升 最新发布的Chrome 45版本内存消耗暴减1/4 性能大提升。距离上个v44版本发布已经10多天了&#xff0c;伴随着全新Logo&#xff0c;xx近日推出了首个v45版本&#xff1a;45.0.2454.85&#xff0c;而在实测中&#xff0c;Crome 45在实测中比…

android如何获得开发者权限,Android 动态权限获取 超级简单的方式

1.添加依赖implementation com.werb.permissionschecker:permissionschecker:0.0.1-beta22.声明 写你想要获取的权限private PermissionChecker permissionChecker;static final String[] PERMISSIONS new String[]{Manifest.permission.RECORD_AUDIO,//写你想获取的权限Manif…

chrome浏览器手机版怎么设置中文

chrome谷歌浏览器手机版怎么设置中文 chrome浏览器手机版怎么设置中文?手机chrome浏览器英文版改成中文需要在语言设置中改变语言选项&#xff0c;具体步骤如下&#xff1a; 1、打开手机chrome浏览器&#xff0c;点击右上角的菜单按钮 2、弹出菜单界面&#xff0c;找到Setti…

无尽包围html5游戏在线玩,小团体激发潜能小游戏突破自我

缩小包围圈游戏其实是一个不可能完成的任务&#xff0c;但是它会给游戏者带来无尽欢笑&#xff0c;使小组充满活力&#xff0c;让队员们能够自然地进行身体接触和配合&#xff0c;消除害羞和忸怩感&#xff0c;创造融洽的气氛&#xff0c;为后续工作的开展奠定良好基础。可以作…

安卓版谷歌浏览器怎么样 Android版Chrome评测

安卓版谷歌浏览器怎么样 Android版Chrome评测 Android安卓版谷歌浏览器怎么样?在众多Android手机用户的苦苦期盼之下&#xff0c;Android版xx Chrome移动浏览器终于发布了&#xff0c;虽然目前还是beta版本但是已经可以让我们痛快的体验一下。我花了几乎一整天的时间进行了比较…

html%2b怎么转换成加号,Apache mod_rewrite%2B和加号(+)符号

不&#xff0c;这与引用的问题不完全相同。这里的问题特别是加号和Apache的答案&#xff1a;mod_rewrite&#xff1a;Spcaes&#xff06;amp; URL中的特殊字符无法正常工作。斜杠也存在问题&#xff0c;请参阅http://httpd.apache.org/docs/current/mod/core.html#allowencoded…

360手机浏览器升级至chrome62 成内核版本最高的手机浏览器

360手机浏览器升级至chrome62 成内核版本最高的手机浏览器 春节和元宵的爆竹声还未走远&#xff0c;春意盎然的三月却已悄然而至&#xff0c;不知不觉又到一年一度的3月8日女神节。在官方定义中&#xff0c;3月8日被称为妇女节&#xff0c;然而随着越来越多的女性活出自我&…

2个html文件顺序播放,CSS3两个动画顺序衔接播放

无标题文档}/*myfirst*/keyframes myfirst{from {top:-50px;}to{top:100px;}}-moz-keyframes myfirst{from {top:-70px;}to{top:100px;}}-webkit-keyframes myfirst{from {top:-300px;}to{top:100px;}}-ms-keyframes myfirst{from {top:-300px;}to{top:100px;}}-o-keyframes my…

谷歌Chrome浏览器正式上新Android版黑暗模式

chrome谷歌浏览器安卓版本迎来了全新的使用版本&#xff0c;这次不仅对于浏览器的性能进行了升级&#xff0c;其他方面也进行了升级使用&#xff0c;并且还上线了全新的“黑暗模式”&#xff0c;相信有很多用户对这个模式已经不陌生了&#xff0c;这个模式主要是为用户在夜晚使…

html5鼠标下拉浮窗固定,【前端技术】vue-floating-menu可拖拽吸附的浮窗菜单

前言正如这个名字&#xff0c;这是一个具有拖拽吸附功能的浮窗菜单&#xff0c;开源项目一个基于 vue 的浮窗组件,可在屏幕内自由拖拽&#xff0c;拖拽后可以根据最后的位置吸附到页面两边&#xff0c;而且可以点击浮窗显示菜单效果如下:遇到的问题总结鼠标移动过快&#xff0c…

Chrome浏览器最新改版 Android P预览版和桌面版界面有变化

chrome谷歌浏览器最新改版 Android P预览版和桌面版界面有变化 作为 xx 设计风格的先锋&#xff0c;每一代 Android 系统都会充满着各种新设计元素。但 xx 旗下的一些传统服务&#xff0c;比如 Gmail 和 Chorme&#xff0c;则往往需要花更长的时间才能跟上。 今年年初&#xf…

js html 生成长图,html生成图片

# html生成图片~~~*{margin: 0;}.test{width: 100px;height: auto;text-align: center;line-height: 100px;background-color: #87CEEB;display: inline-block;vertical-align:top;}canvas{margin-right: 5px;}.down{float: right;margin: 40px 10px;}下载asdfa asdfadsf sdf//…

谷歌Chrome浏览器添加新技术 可防止广告主追踪用户

谷歌chrome浏览器添加新技术 可防止广告主追踪用户 据美国科技媒体ZDNet报道&#xff0c;谷歌Chrome提出一套新的技术解决方案&#xff0c;目的是想调和用户隐私与广告投放之间的矛盾。 新方案名叫Privacy Sandbox&#xff0c;它是一种新的开放式WEB技术&#xff0c;谷歌将会把…

html vba 单元格 格式,VBA设置单元格格式之——字体

009 设置单元格格式之字体(文档下载&#xff1a;关注本公众号&#xff0c;发送消息【教程】即可获得)通过VBA对单元格字体进行设置也是比较常用的方式&#xff0c;那么本节内容我们就来学习如何使用VBA对单元格中的字体进行设置。如图所示&#xff0c;字体设置主要有&#xff0…

2021聊城二中高考成绩查询,聊城中考成绩查询时间2021

聊城市2021年中考查分时间大约是6月27日。各普通高中要于7月10日前在校内张榜公布录取考生名单&#xff0c;并签发录取通知书。聊城中考录取时间各普通高中要于7月10日前在校内张榜公布录取考生名单&#xff0c;并签发录取通知书。所有学校均不得违规招收已被其他学校录取的考生…

谷歌Chrome 80稳定版更新:对浏览器进行两项重大的更改

谷歌Chrome 80稳定版会是浏览器更新改革一个非常重要的里程碑&#xff0c;这个更新了非常重要的两个功能和服务&#xff0c;想要使用的用户现在就可以马上更新浏览器的操作系统&#xff0c;马上就可以体验这两个新的功能&#xff0c;相信有很多用户都已经体验到了这个浏览器的功…