欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android TimeLine 时间节点轴的实现实例代码

程序员文章站 2023-11-29 13:36:52
整理文档,搜刮出一个android timeline 时间节点轴的实现实例代码,稍微整理精简一下做下分享。 效果图 具体实现 (recyclerview) 1...

整理文档,搜刮出一个android timeline 时间节点轴的实现实例代码,稍微整理精简一下做下分享。

效果图

Android TimeLine 时间节点轴的实现实例代码

具体实现 (recyclerview)

1.adapter

package com.haoren.timeline;

import android.content.context;
import android.support.v7.widget.recyclerview;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
import android.widget.imageview;

/**
 * created by hh on 2017/3/8.
 */
public class timelineadapter extends recyclerview.adapter<timelineadapter.horizontalvh> {

  private context context;


  //时间节点数
  private int nodenum = 0;

  //当前到达节点
  private int currentnode = 1;


  public timelineadapter(context context, int nodenum) {
    this.context = context;
    this.nodenum = nodenum;
  }

  @override
  public horizontalvh oncreateviewholder(viewgroup parent, int viewtype) {
    view view = layoutinflater.from(context).inflate(r.layout.time_line, null, false);
    horizontalvh vh = new horizontalvh(view);
    return vh;
  }

  @override
  public void onbindviewholder(horizontalvh holder, int position) {
    if (position < currentnode) {
      //当前节点之前的全部设为已经经过
      holder.point.setimageresource(r.mipmap.point_select);
      holder.lineleft.setbackgroundresource(r.color.colorprimary);
      holder.lineright.setbackgroundresource(r.color.colorprimary);
    }

    // 去掉左右两头的分支
    if (position == 0) {
      holder.lineleft.setvisibility(view.invisible);
    }
    if (position == nodenum - 1) {
      holder.lineright.setvisibility(view.invisible);
    }
  }

  @override
  public int getitemcount() {
    return nodenum;
  }

  /**
   * 设置当前节点
   * @param currentnode
   */
  public void setcurrentnode(int currentnode) {
    this.currentnode = currentnode;
    this.notifydatasetchanged();
  }

  class horizontalvh extends recyclerview.viewholder {
    private imageview point;

    private view lineleft, lineright;


    public horizontalvh(view itemview) {
      super(itemview);
      this.point = (imageview) itemview.findviewbyid(r.id.point);
      this.lineleft = itemview.findviewbyid(r.id.line_left);
      this.lineright = itemview.findviewbyid(r.id.line_right);
    }
  }
}

item.xml

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="0dp"
  android:layout_weight="1"
  android:layout_height="50dp"
  android:gravity="center_horizontal"
  android:orientation="vertical">

  <relativelayout
    android:layout_margintop="20dp"
    android:layout_width="40dp"
    android:layout_height="wrap_content">

    <view
      android:id="@+id/line_left"
      android:layout_width="20dp"
      android:layout_height="1dp"
      android:layout_centervertical="true"
      android:background="#a6a6a6" />


    <view
      android:id="@+id/line_right"
      android:layout_width="20dp"
      android:layout_height="1dp"
      android:layout_centervertical="true"
      android:layout_torightof="@+id/line_left"
      android:background="#a6a6a6" />

    <imageview
      android:id="@+id/point"
      android:layout_width="15dp"
      android:layout_height="15dp"
      android:layout_centerhorizontal="true"
      android:scaletype="center"
      android:src="@mipmap/point_normal" />
  </relativelayout>

  <textview
    android:id="@+id/show_time"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="node"
    android:textsize="11sp" />
</linearlayout>

mainactivity

package com.haoren.timeline;

import android.os.bundle;
import android.support.v7.app.appcompatactivity;
import android.support.v7.widget.linearlayoutmanager;
import android.support.v7.widget.recyclerview;

public class mainactivity extends appcompatactivity {

  private recyclerview mrecyclerview;

  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);

    mrecyclerview = (recyclerview) findviewbyid(r.id.mrecyclerview);

    initadapter();
  }

  private void initadapter() {
    timelineadapter adapter = new timelineadapter(this, 8);
    mrecyclerview.setlayoutmanager(new linearlayoutmanager(this, linearlayoutmanager.horizontal, false));
    mrecyclerview.setadapter(adapter);
    adapter.setcurrentnode(5);
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。