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); } }

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

时间: 2024-09-03 08:29:24

Android TimeLine 时间节点轴的实现实例代码的相关文章

Android高仿QQ6.0侧滑删除实例代码_Android

推荐阅读: 先给大家分享一下,侧滑删除,布局也就是前面一个item,然后有两个隐藏的按钮(TextView也可以),然后我们可以向左侧滑动,然后显示出来,然后对delete(删除键)实现监听,就可以了哈.好了那就来看看代码怎么实现的吧. 首先和之前一样 自定义View,初始化ViewDragHelper: package com.example.removesidepull; import android.content.Context; import android.support.v4.wi

Android高仿QQ6.0侧滑删除实例代码

推荐阅读: 先给大家分享一下,侧滑删除,布局也就是前面一个item,然后有两个隐藏的按钮(TextView也可以),然后我们可以向左侧滑动,然后显示出来,然后对delete(删除键)实现监听,就可以了哈.好了那就来看看代码怎么实现的吧. 首先和之前一样 自定义View,初始化ViewDragHelper: package com.example.removesidepull; import android.content.Context; import android.support.v4.wi

Android中文件的压缩和解压缩实例代码

使用场景 当我们在应用的Assets目录中需要加入文件时,可以直接将源文件放入,但这样会造成打包后的apk整体过大,此时就需要将放入的文件进行压缩.又如当我们需要从服务器中下载文件时,如果下载源文件耗时又消耗流量,较大文件需要压缩,可以使得传输效率大大提高.下面我们就学习下基本的文件压缩和解压缩.Java中提供了压缩和解压缩的输入输出流 public static void zip(String src,String dest) throwsIOException { //定义压缩输出流 Zip

android判断设备是否有相机的实例代码

通过PackageManager可以判断android设备是否有相机 PackageManager pm = getPackageManager(); // FEATURE_CAMERA - 后置相机 // FEATURE_CAMERA_FRONT - 前置相机 if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA) && !pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT

Android开发模仿qq视频通话悬浮按钮(实例代码)

模仿qq视频通话的悬浮按钮的实例代码,如下所示: public class FloatingWindowService extends Service{ private static final String TAG="OnTouchListener"; private static View mView = null; private static WindowManager mWindowManager = null; private static Context mContext

Android实现空心圆角矩形按钮的实例代码

页面上有时会用到背景为空心圆角矩形的Button,可以通过xml绘制出来. drawrable文件夹下bg_red_hollow_rectangle.xml <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle&qu

Android判断是否有拍照权限的实例代码

下面一段代码给大家介绍android判断是否有拍照权限,具体代码如下所示: /** * 返回true 表示可以使用 返回false表示不可以使用 */ public boolean cameraIsCanUse() { boolean isCanUse = true; Camera mCamera = null; try { mCamera = Camera.open(); Camera.Parameters mParameters = mCamera.getParameters(); //针对

Android 使用Fragment模仿微信界面的实例代码

什么是Fragment 自从Android 3.0中引入fragments 的概念,根据词海的翻译可以译为:碎片.片段.其目的是为了解决不同屏幕分辩率的动态和灵活UI设计.大屏幕如平板小屏幕如手机,平板电脑的设计使得其有更多的空间来放更多的UI组件,而多出来的空间存放UI使其会产生更多的交互,从而诞生了fragments . fragments 的设计不需要你来亲自管理view hierarchy 的复杂变化,通过将Activity 的布局分散到frament 中,可以在运行时修改activit

Android App增量更新详解及实例代码_Android

Android App增量更新实例--Smart App Updates        介绍 你所看到的,是一个用于Android应用程序增量更新的开源库. 包括客户端.服务端两部分代码. 原理 自从 Android 4.1 开始,Google引入了应用程序的增量更新. Link: http://developer.android.com/about/versions/jelly-bean.html Smart app updates is a new feature of Google Pla