android实现条目倒计时功能
作者:like1521 发布时间:2023-08-23 08:35:38
标签:android,倒计时
网上对于这样的功能已经是泛滥成河了,但是最近遇到这样的一个需求,还是要值得我们学习一下,并将他记录下来。
布局文件:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.rui.demo.list_count_down.ListCountDownActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_list_count_down"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</FrameLayout>
Activity界面:
public class ListCountDownActivity extends AppCompatActivity {
RecyclerView mRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_count_down);
initView();
}
private void initView() {
mRecyclerView = (RecyclerView) findViewById(R.id.rv_list_count_down);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
CountDownAdapter adapter = new CountDownAdapter();
mRecyclerView.setAdapter(adapter);
List<DataInfo> list = new ArrayList<>();
for (int i = 1; i < 101; i++) {
list.add(new DataInfo("我是条目" + i, i * 100));
}
adapter.setmDatas(list);
}
}
倒计时条目适配器:
/**
* @Date 2018/4/26
* @Introduction 倒计时条目适配器
*/
public class CountDownAdapter extends RecyclerView.Adapter<CountDownAdapter.MyViewHoder> {
private final String TAG = CountDownAdapter.class.getSimpleName();
private final int STOP = 0x01;
private final int START = 0x02;
private final int LOOP = 0x03;
private List<DataInfo> mDatas;
public CountDownAdapter() {
}
public void setmDatas(List<DataInfo> mDatas) {
this.mDatas = mDatas;
notifyDataSetChanged();
}
@Override
public MyViewHoder onCreateViewHolder(ViewGroup parent, int viewType) {
return new MyViewHoder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_count_down, parent, false));
}
@Override
public void onBindViewHolder(final MyViewHoder holder, int position) {
final DataInfo info = mDatas.get(position);
holder.tvName.setText(info.getName());
holder.tvTime.setText(info.getTime() + "");
if (info.isCountDown()) {
holder.btnStart.setText("停止");
} else {
holder.btnStart.setText("开始");
}
holder.btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Message msg = Message.obtain();
if (!info.isCountDown()) {
holder.btnStart.setText("停止");
msg.what = START;
} else {
holder.btnStart.setText("开始");
msg.what = STOP;
}
msg.obj = info;
mHandler.sendMessage(msg);
info.setCountDown(!info.isCountDown());
}
});
}
@Override
public int getItemCount() {
return mDatas == null ? 0 : mDatas.size();
}
static class MyViewHoder extends RecyclerView.ViewHolder {
private final TextView tvName;
private final TextView tvTime;
private final Button btnStart;
public MyViewHoder(View itemView) {
super(itemView);
tvName = (TextView) itemView.findViewById(R.id.tv_name_count_down_item);
tvTime = (TextView) itemView.findViewById(R.id.tv_time_count_down_item);
btnStart = (Button) itemView.findViewById(R.id.btn_time_count_down_item);
}
}
private Handler mHandler = new Handler() {
private List<DataInfo> mCountDownList = new ArrayList<>();
@Override
public void handleMessage(Message msg) {
setChange(msg);
}
private synchronized void setChange(Message msg) {
switch (msg.what) {
case STOP:
DataInfo stopInfo = (DataInfo) msg.obj;
Log.e(TAG, "------------stop:" + stopInfo.getName());
mCountDownList.remove(stopInfo);
notifyDataSetChanged();
if (mCountDownList.size() <= 0) {
mHandler.removeCallbacksAndMessages(null);
}
break;
case START:
DataInfo startInfo = (DataInfo) msg.obj;
Log.e(TAG, "------------start:" + startInfo.getName());
if (startInfo.getTime() > 0) {
mCountDownList.add(startInfo);
mHandler.sendEmptyMessageDelayed(LOOP, 1000);
}
break;
case LOOP:
if (mCountDownList.size() <= 0) {
return;
}
for (Iterator<DataInfo> iterator = mCountDownList.iterator(); iterator.hasNext(); ) {
DataInfo dataInfo = iterator.next();
int time = dataInfo.getTime();
time--;
dataInfo.setTime(time);
if (time <= 0) {
iterator.remove();
}
}
notifyDataSetChanged();
mHandler.removeCallbacksAndMessages(null);
mHandler.sendEmptyMessageDelayed(LOOP, 1000);
break;
default:
break;
}
}
};
}
JavaBean类
/**
* @Date 2018/4/26
* @Introduction 倒计时数据实体类
*/
public class DataInfo {
private String name;
private int time;
private boolean isCountDown = false;
public DataInfo(String name, int time) {
this.name = name;
this.time = time;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getTime() {
return time;
}
public void setTime(int time) {
this.time = time;
}
public boolean isCountDown() {
return isCountDown;
}
public void setCountDown(boolean countDown) {
isCountDown = countDown;
}
}
来源:https://blog.csdn.net/like15835551521/article/details/80163597


猜你喜欢
- 前序(先序)遍历中序遍历后续遍历层序遍历如图二叉树:二叉树结点结构public class TreeNode { int val
- 本文实例为大家分享了Android开发之ViewPager实现滑动切换页面的具体代码,供大家参考,具体内容如下基本构件activity_ma
- 前言Kafka是现在非常热门的分布式消息队列,常用于微服务间异步通信,业务解耦等场景。kafka的性能非常强大,但是单个微服务吞吐性能是有上
- 异常和异常处理C# 语言的异常处理功能可帮助您处理程序运行时出现的任何意外或异常情况。异常处理使用 try、catch 和 finally
- Java BigDecimal的坑采坑处 BigDecimal bd =new BigDecimal(0.1);
- MyBatis-Plus 使用简单,内置通用 Mapper、通用 Service,仅仅通过少量配置,即可实现单表大部分 CRUD 操作。下面
- Java反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动
- Interceptor 介绍 * (Interceptor)同 Filter 过滤器一样,它俩都是面向切面编程—&
- 最早开始的时候做过一些数据Excel导出的功能,但是到后期每一次导出都需要写一些差不多类似的代码,稍微研究了一下写了个公共的导出方法。这里用
- 我们可以试想ImageView能显示图片,而VideoView就是用来显示视频的。使用VideoView播放视频的步骤如下 【1】在界面布局
- Spring Data 概述Spring Data用于简化数据库访问,支持NoSQL 和 关系数据存储,其主要目标是使数据库的访问变得方便快
- 本文实例讲述了Android编程实现对电池状态的监视功能。分享给大家供大家参考,具体如下:最近在开发一个与GPS相关的项目,因为其中涉及到了
- 打个比方:一个object就像一个大房子,大门永远打开。房子里有很多房间(也就是方法)。这些房间有上锁的(synchronized方法),
- 一、简介在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,
- 方法1:C#Label1.Text = Request.Form["txtName"].ToString();vb.ne
- 本文实例为大家分享了C#实现简单文本编辑器的具体代码,供大家参考,具体内容如下建立一个窗体文件,实现对文件的编辑保存和对txt文件的打开界面
- 本篇博客要分享的一个UI效果——实现底部切换标签,想必大家在一些应用上面遇到过这种效果了,最典型的就是微信了,可以左右滑动切换页面,也可以点
- 一、代理是Java常用的设计模式,代理类通过调用被代理类的相关方法,并对相关方法进行增强。加入一些非业务性代码,比如事务、日志、报警发邮件等
- 之前有做过手机端后台的国际化,因为手机统一传递了language参数所以只要设置LocaleChangeInterceptor就行了/**
- 本文实例讲述了Spring实战之注入集合值操作。分享给大家供大家参考,具体如下:一 配置<?xml version="1.0