先上效果图:

RecyclerView + SwipeRefreshLayout

实现倒计时效果

MainActivity.java

package top.wintp.counttimedemo1;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; import java.util.ArrayList;
import java.util.List; /**
* 类描述:主要的Activity
* <p>
* 作者: 你的感冒清
* <p>
* qq: 337081267
* <p>
* CSDN: http://blog.csdn.net/pyfysf
* <p>
* 个人博客: http://wintp.top
* <p>
* 时间: 17-5-18
* <p>
* 邮箱: pyfysf@163.com
*/
public class MainActivity extends AppCompatActivity
implements SwipeRefreshLayout.OnRefreshListener { private SwipeRefreshLayout mSwrl_refresh;
private RecyclerView mRv_list;
private List<CountTimeInfo> mCountTimeInfos; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //初始化View
initView(); } /**
* 初始化View
*/
private void initView() {
mSwrl_refresh = (SwipeRefreshLayout) findViewById(R.id.swrl_refresh);
mRv_list = (RecyclerView) findViewById(R.id.rv_list); //设置下拉刷新的监听
mSwrl_refresh.setOnRefreshListener(this); //获取数据
getData2Local();
} @Override
public void onRefresh() {
//刷新数据
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mSwrl_refresh.setRefreshing(false);
getData2Local();
}
},2000);
} /**
* 从本地加载数据
* 可以从服务器端加载数据
*/
public void getData2Local() {
//创建本地集合装载数据
mCountTimeInfos = new ArrayList<>();
//添加数据
for (int i = 0; i < 100; i++) {
CountTimeInfo countTimeInfo = new CountTimeInfo(); countTimeInfo.setContent("item内容" + i);
countTimeInfo.setCreateTime(TimeUtils.getWebsiteDatetime() - ((i + 1) * 10));
countTimeInfo.setNeedTime((i+1)*10); mCountTimeInfos.add(countTimeInfo);
} //设置布局管理器
LinearLayoutManager llm = new LinearLayoutManager(this);
mRv_list.setLayoutManager(llm); //设置数据适配器
mRv_list.setAdapter(new CountTimeListAdapter(this, mCountTimeInfos)); }
}

activity_main.xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swrl_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView
android:id="@+id/rv_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/> </android.support.v4.widget.SwipeRefreshLayout> </LinearLayout>

TimeUtil.java

package top.wintp.counttimedemo1;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone; /**
* 类描述: 时间的工具类
*
* 作者: 你的感冒清
*
* qq: 337081267
*
* CSDN: http://blog.csdn.net/pyfysf
*
* 个人博客: http://wintp.top
*
* 时间: 17-5-18
*
* 邮箱: pyfysf@163.com
*/ public class TimeUtils { //时间格式转换
public static String timeChange(String time) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = format.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat format1 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
String str = format1.format(date);
return str;
} public static long timeDifference(String nowtime, String endtime) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long diff = 0;
try {
//系统时间转化为Date形式
Date dstart = format.parse(nowtime);
//活动结束时间转化为Date形式
Date dend = format.parse(endtime);
//算出时间差,用ms表示
diff = dend.getTime() - dstart.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
//返回时间差
return diff;
} /**
* 获取网络时间
*/
public static Long getWebsiteDatetime() {
SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dff.setTimeZone(TimeZone.getTimeZone("GMT+08")); return stringToLongTime(dff.format(new Date()));
} /**
* 把String类型的事件转换为毫秒值 "yyyy-MM-dd HH:mm:ss"
*/
public static Long stringToLongTime(String time) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); long millionSeconds = 0;//毫秒
try {
return millionSeconds = sdf.parse(time).getTime();
} catch (ParseException e) {
e.printStackTrace();
throw new RuntimeException(e);
} } /**
* 把Long类型的毫秒值转换为
*
* @param counttime day天 HH时mm分ss秒
* @return
*/
public static String longToStringTime(long counttime) {
long days = counttime / (1000 * 60 * 60 * 24);
long hours = (counttime - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
long minutes = (counttime - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
long second = (counttime - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60) - minutes * (1000 * 60)) / 1000; return days + "天" + hours + "时" + minutes + "分" + second + "秒"; } }

item_count_time.xml

<android.support.v7.widget.CardView
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="180dp"
android:layout_margin="20dp"
android:clickable="true"
app:cardCornerRadius="10dp"
> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="180dp"
android:padding="15dp"> <TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="4"
android:gravity="center"
android:text="内容条目"
android:textColor="@android:color/holo_red_dark"
android:textSize="20sp"
/> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@mipmap/ic_launcher"/> <TextView
android:id="@+id/tv_count_down_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_weight="4"
android:text="2017-05-18 09:26:53"
android:textColor="@android:color/holo_red_dark"
android:textSize="17sp"
/> </RelativeLayout> </android.support.v7.widget.CardView>

CountTimeAdapter.java

package top.wintp.counttimedemo1;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; import java.util.List;
import java.util.Timer;
import java.util.TimerTask; /**
* 作者: 你的感冒清
* <p>
* qq: 337081267
* <p>
* CSDN: http://blog.csdn.net/pyfysf
* <p>
* 个人博客: http://wintp.top
* <p>
* 时间: 17-5-18 上午9:19
* <p>
* 邮箱: pyfysf@163.com
*/ class CountTimeListAdapter extends RecyclerView.Adapter {
private final Context mContext;
private LayoutInflater mLayoutInflater;
private List<CountTimeInfo> mCountTimeInfos; public CountTimeListAdapter(Context context, List<CountTimeInfo> countTimeInfos) {
mLayoutInflater = mLayoutInflater.from(context);
this.mContext = context;
this.mCountTimeInfos = countTimeInfos; } @Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemView = mLayoutInflater.inflate(R.layout.item_count_time, parent, false); //下面两个不好看
// View itemView = mLayoutInflater.inflate(R.layout.item_count_time, null);
// View itemView = View.inflate(mContext,R.layout.item_count_time,null); return new CountTimeViewHolder(itemView);
} @Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { CountTimeViewHolder countTimeViewHolder = (CountTimeViewHolder) holder; countTimeViewHolder.bindView(this.mCountTimeInfos, position);
} @Override
public int getItemCount() {
return mCountTimeInfos.size();
} private static class CountTimeViewHolder extends RecyclerView.ViewHolder { private final TextView mTv_content;
private final TextView tv_count_down_time;
private Timer mTimer;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 100:
String currentStringTime = TimeUtils.longToStringTime((Long) msg.obj); tv_count_down_time.setText(currentStringTime);
break;
}
}
}; public CountTimeViewHolder(View itemView) {
super(itemView);
mTv_content = (TextView) itemView.findViewById(R.id.tv_content);
tv_count_down_time = (TextView) itemView.findViewById(R.id.tv_count_down_time);
} /**
* 绑定View的内容
*
* @param countTimeInfos
* @param position
*/
public void bindView(List<CountTimeInfo> countTimeInfos, int position) {
final CountTimeInfo countTimeInfo = countTimeInfos.get(position);
mTv_content.setText(countTimeInfo.getContent()); mTimer = new Timer();
//刷新倒计时
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
Message msg = Message.obtain();
//结束时间减去当前时间
msg.what = 100;
msg.obj = countTimeInfo.getStopTime() - TimeUtils.getWebsiteDatetime(); mHandler.sendMessage(msg);
}
}; //0秒之后每隔1秒执行一次run
mTimer.schedule(timerTask, 1, 1000); }
}
}

CountTimeInfo.java

package top.wintp.counttimedemo1;

/**
* 描述:倒计时item的javabean
* <p>
* 作者: 你的感冒清
* <p>
* qq: 337081267
* <p>
* CSDN: http://blog.csdn.net/pyfysf
* <p>
* 个人博客: http://wintp.top
* <p>
* 时间: 17-5-18 上午9:12
* <p>
* 邮箱: pyfysf@163.com
*/ public class CountTimeInfo {
private String content; private long stopTime;
private long createTime;
private long needTime; public void setNeedTime(long needTime) {
this.needTime = needTime;
} public long getStopTime() {
return getCreateTime() + this.needTime * 60 * 1000; } public long getCreateTime() {
return createTime;
} public void setCreateTime(long createTime) {
this.createTime = createTime;
} public String getContent() {
return content;
} public void setContent(String content) {
this.content = content;
} public CountTimeInfo() {
} @Override
public String toString() {
return "CountTimeInfo{" +
"content='" + content + '\'' +
'}';
}
}

gradle(依赖)

    compile 'com.android.support:design:25.3.1'

    compile 'com.android.support:cardview-v7:25.3.1'

android实现倒计时,最简单实现RecyclerView倒计时+SwipeRefreshLayout下拉刷新的更多相关文章

  1. Android零基础入门第72节:SwipeRefreshLayout下拉刷新

    在实际开发中,经常都会遇到下拉刷新.上拉加载更多的情形,这一期就一起来学习Android系统的SwipeRefreshLayout下拉刷新组件. 一.SwipeRefreshLayout简介 Swip ...

  2. Android之SwipeRefreshLayout下拉刷新组件

    SwipeRefreshLayout概述 SwipeRefrshLayout是Google官方更新的一个Widget,可以实现下拉刷新的效果.该控件集成自ViewGroup在support-v4兼容包 ...

  3. Android之RecyclerView轻松实现下拉刷新和加载更多

    今天研究了下RecyclerView的滑动事件,特别是下拉刷新和加载更多事件,在现在几乎所有的APP显示数据列表时都用到了.自定义RecyclerView下拉刷新和加载更多听上去很复杂,实际上并不难, ...

  4. Android 学习笔记之AndBase框架学习(六) PullToRefrech 下拉刷新的实现

    PS:Struggle for a better future 学习内容: 1.PullToRefrech下拉刷新的实现...   不得不说AndBase这个开源框架确实是非常的强大..把大部分的东西 ...

  5. Android Material Design控件使用(四)——下拉刷新 SwipeRefreshLayout

    使用下拉刷新SwipeRefreshLayout 说明 SwipeRefreshLayout是Android官方的一个下拉刷新控件,一般我们使用此布局和一个RecyclerView嵌套使用 使用 xm ...

  6. [Android实例] Android 6.0RecyclerView SwipeRefreshLayout 下拉刷新 上拉加载

    这是Android 6.0的 SwipeRefreshLayout 实现下拉刷新和RecyclerView的上拉加载更多,以及添加分割线等 Android <ignore_js_op> r ...

  7. Android SwipeRefreshLayout 下拉刷新——Hi_博客 Android App 开发笔记

    以前写下拉刷新 感觉好费劲,要判断ListView是否滚到顶部,还要加载头布局,还要控制 头布局的状态,等等一大堆.感觉麻烦死了.今天学习了SwipeRefreshLayout 的用法,来分享一下,有 ...

  8. .Net 转战 Android 4.4 日常笔记(10)--PullToRefresh下拉刷新使用

    下拉刷新很多地方都用到了,新浪微博,微信,百度新闻 这里我们使用一个开源的库叫:PullToRefresh 开源地址:https://github.com/chenyoca/pull-to-refre ...

  9. SwipeRefreshLayout下拉刷新简单用例

    自己的下拉刷新组件 下拉刷新并自动添加数据 MainActivity package com.shaoxin.myswiperefreshlayout; import android.graphics ...

随机推荐

  1. Delphi编写系统服务:完成端口演示

    在开发大量Socket并发服务器,完成端口加重叠I/O是迄今为止最好的一种解决方案,下面是简单的介绍:   “完成端口”模型是迄今为止最为复杂的一种I/O模型,特别适合需要同时管理为数众多的套接字,采 ...

  2. 由Qmake.exe/QtCreator.exe启动速度慢挖进去(非常有趣的调试过程,作者态度不错,而且关闭Welcome插件也是常见办法)

    一直用Qt Creator开发Qt程序,Nokia的Qt Creator实在太慢了,启动慢,编译速度也是超级慢.昨天,终于它慢的让我无法忍受了,我决定抛开手上的一切工作,深入挖掘Qt Creator启 ...

  3. 设置tablewidget自适应列宽和设置自动等宽

      在网上很容易知道自适应列宽,100%不留空显示,这里还是提下: /*设置表格是否充满,即行末不留空*/ ui->tableWidget->horizontalHeader()-> ...

  4. Dependency Injection 筆記 (2)

    续上集,接着要说明如何运用 DI 来让刚才的范例程序具备执行时期切换实现类型的能力. (本文摘自電子書<.NET 依賴注入>) 入门范例—DI 版本 为了让 AuthenticationS ...

  5. git如何merge github forked repository里的代码更新

    git如何merge github forked repository里的代码更新? 问题是这样的,github里有个项目ruby-gmail,我需要从fork自同一个项目的另一个repository ...

  6. Vbox中Linux虚拟机网络配置(比较实用)

    好久没写过东西了,主要大部分都是来自对生活的感悟,很少有实实在在的关于学得有成就感的技术可以“炫耀”,所以也就懒得在这个上面登了. 实验室很早就有位师兄曾在吃饭的路上问过我们这群小弟,你们知道Vbox ...

  7. 【Web前端Talk】无聊吗?写个【飞机大战】来玩吧(下篇)

    上一篇介绍了如何使用cocos creator开发游戏,此篇是详细介绍功能点以及如何部署打包至微信小游戏体验. 欢迎关注我们的公众号:Web前端Talk.前端文章持续更新. 资源管理制作 1.准备工具 ...

  8. play框架之简介

    Play Framework是一个开源的Web框架,背后商业公司是Typesafe.要介绍Play之前,首先理清Play的两个不同的分支. Play 1.x 使用Java开发,最新版本是1.3.1,只 ...

  9. 11 CSS的三种引入方式和基本选择器

    <!-- 整体说明: 1.CSS的三种引入方式 (1)行内样式 (2)内接样式 (3)外接样式 2.CSS的基本选择器 (1)id选择器 (引用方式:#id) (2)标签选择器(引用方式:标签名 ...

  10. 高并发 Nginx+Lua OpenResty系列(4)——Lua 模块开发

    在实际开发中,不可能把所有代码写到一个大而全的lua文件中,需要进行分模块开发:而且模块化是高性能Lua应用的关键.使用require第一次导入模块后,所有Nginx 进程全局共享模块的数据和代码,每 ...