package com.example.countdowntimerdemo.adapter;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.countdowntimerdemo.R;
import com.example.countdowntimerdemo.bean.CountDownTimerBean;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class CountDownTimerAdapter extends RecyclerView.Adapter<CountDownTimerAdapter.Holder> {
private static final String TAG = "CountDownTimerAdapter->";
private List<CountDownTimerBean> mList;//数据
private Handler mHandler;//线程调度,用来更新列表
private Timer mTimer;
private MyTask mTask;
public CountDownTimerAdapter() {
mList = new ArrayList<>();
mHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
notifyItemChanged(msg.arg1, "update-time");
break;
}
}
};
mTask = new MyTask();
}
public void bindAdapterToRecyclerView(@NonNull RecyclerView view) {
view.setAdapter(this);
}
/**
* 设置新的数据源
*
* @param list 数据
*/
public void setNewData(@NonNull List<CountDownTimerBean> list) {
destroy();
mList.clear();
mList.addAll(list);
notifyDataSetChanged();
if (mTimer == null) {
mTimer = new Timer();
}
mTimer.schedule(mTask, 0, 1000);
}
/**
* 销毁资源
*/
public void destroy() {
mHandler.removeMessages(1);
if (mTimer != null) {
mTimer.cancel();
mTimer.purge();
mTimer = null;
}
}
@NonNull
@Override
public Holder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_count_down_timer, viewGroup, false);
return new Holder(view);
}
@Override
public void onBindViewHolder(@NonNull Holder holder, int position, @NonNull List<Object> payloads) {
if (payloads.isEmpty()) {
onBindViewHolder(holder, position);
return;
}
//更新某个控件,比如说只需要更新时间信息,其他不用动
CountDownTimerBean bean = mList.get(position);
long day = bean.getTotalTime() / (1000 * 60 * 60 * 24);
long hour = (bean.getTotalTime() / (1000 * 60 * 60) - day * 24);
long min = ((bean.getTotalTime() / (60 * 1000)) - day * 24 * 60 - hour * 60);
long s = (bean.getTotalTime() / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
holder.tvTime.setText("剩余时间: " + day + "天" + hour + "小时" + min + "分" + s + "秒");
holder.btnAction.setText(bean.isPause() ? "开始" : "暂停");
holder.btnAction.setEnabled(bean.getTotalTime() != 0);
}
@Override
public void onBindViewHolder(@NonNull final Holder holder, int position) {
holder.ivIcon.setImageResource(R.mipmap.ic_launcher_round);
final CountDownTimerBean bean = mList.get(position);
long day = bean.getTotalTime() / (1000 * 60 * 60 * 24);
long hour = (bean.getTotalTime() / (1000 * 60 * 60) - day * 24);
long min = ((bean.getTotalTime() / (60 * 1000)) - day * 24 * 60 - hour * 60);
long s = (bean.getTotalTime() / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
holder.tvTime.setText("剩余时间: " + day + "天" + hour + "小时" + min + "分" + s + "秒");
holder.btnAction.setText(bean.isPause() ? "开始" : "暂停");
holder.btnAction.setEnabled(bean.getTotalTime() != 0);
holder.btnAction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (bean.isPause()) {
bean.setPause(false);
holder.btnAction.setText("暂停");
} else {
bean.setPause(true);
holder.btnAction.setText("开始");
}
}
});
}
@Override
public int getItemCount() {
return mList.size();
}
class Holder extends RecyclerView.ViewHolder {
private ImageView ivIcon;
private TextView tvTime;
private Button btnAction;
Holder(@NonNull View itemView) {
super(itemView);
ivIcon = itemView.findViewById(R.id.iv_icon);
tvTime = itemView.findViewById(R.id.tv_time);
btnAction = itemView.findViewById(R.id.btn_action);
}
}
class MyTask extends TimerTask {
@Override
public void run() {
if (mList.isEmpty()) {
return;
}
int size = mList.size();
CountDownTimerBean bean;
long totalTime;
for (int i = 0; i < size; i++) {
bean = mList.get(i);
if (!bean.isPause()) {//不处于暂停状态
totalTime = bean.getTotalTime() - 1000;
if (totalTime <= 0) {
bean.setPause(true);
bean.setTotalTime(0);
}
bean.setTotalTime(totalTime);
Message message = mHandler.obtainMessage(1);
message.arg1 = i;
mHandler.sendMessage(message);
}
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Android实现列表倒计时效果 Demo
共106个文件
xml:61个
png:10个
bin:9个
4星 · 超过85%的资源 需积分: 42 65 下载量 92 浏览量
2019-02-24
14:58:12
上传
评论
收藏 460KB 7Z 举报
温馨提示
Android实现RecyclerView列表倒计时效果,主要使用Timer等方法。
资源推荐
资源详情
资源评论
收起资源包目录
Android实现列表倒计时效果 Demo (106个子文件)
gradlew.bat 2KB
taskHistory.bin 1.12MB
taskHistory.bin 760KB
classAnalysis.bin 751KB
jarAnalysis.bin 300KB
fileHashes.bin 251KB
taskJars.bin 23KB
resourceHashesCache.bin 20KB
outputFiles.bin 20KB
last-build.bin 1B
.gitignore 147B
.gitignore 8B
build.gradle 1KB
build.gradle 573B
settings.gradle 16B
gradlew 5KB
app.iml 13KB
CountDownTimerDemo.iml 872B
gradle-wrapper.jar 53KB
CountDownTimerAdapter.java 6KB
MainActivity.java 5KB
CountDownTimerBean.java 1KB
ExampleInstrumentedTest.java 770B
ExampleUnitTest.java 407B
taskHistory.lock 17B
javaCompile.lock 17B
fileContent.lock 17B
fileHashes.lock 17B
buildOutputCleanup.lock 17B
ic_launcher_round.png 15KB
ic_launcher_round.png 10KB
ic_launcher.png 9KB
ic_launcher_round.png 7KB
ic_launcher.png 6KB
ic_launcher_round.png 5KB
ic_launcher.png 4KB
ic_launcher.png 3KB
ic_launcher_round.png 3KB
ic_launcher.png 2KB
proguard-rules.pro 772B
gradle.properties 739B
local.properties 377B
gradle-wrapper.properties 236B
cache.properties 51B
build_file_checksums.ser 535B
workspace.xml 470KB
ic_launcher_background.xml 6KB
ic_launcher_foreground.xml 2KB
item_count_down_timer.xml 2KB
Project.xml 2KB
misc.xml 2KB
activity_main.xml 835B
Gradle__com_android_support_test_espresso_espresso_idling_resource_3_0_2.xml 773B
AndroidManifest.xml 744B
Gradle__com_android_support_test_espresso_espresso_core_3_0_2.xml 718B
Gradle__android_arch_lifecycle_livedata_core_1_1_1.xml 695B
Gradle__android_arch_lifecycle_viewmodel_1_1_1.xml 676B
Gradle__android_arch_lifecycle_livedata_1_1_1.xml 671B
Gradle__com_android_support_test_monitor_1_0_2.xml 670B
Gradle__android_arch_lifecycle_runtime_1_1_1.xml 666B
Gradle__com_android_support_test_runner_1_0_2.xml 665B
Gradle__android_arch_core_runtime_1_1_1.xml 655B
gradle.xml 626B
Gradle__org_hamcrest_hamcrest_integration_1_3_jar.xml 586B
Gradle__org_hamcrest_hamcrest_library_1_3_jar.xml 565B
runConfigurations.xml 564B
Gradle__android_arch_lifecycle_common_1_1_1_jar.xml 556B
Gradle__org_hamcrest_hamcrest_core_1_3_jar.xml 551B
Gradle__com_squareup_javawriter_2_1_1_jar.xml 546B
Gradle__android_arch_core_common_1_1_1_jar.xml 541B
Gradle__javax_inject_javax_inject_1_jar.xml 536B
Gradle__com_android_support_animated_vector_drawable_28_0_0.xml 522B
Gradle__com_android_support_support_vector_drawable_28_0_0.xml 519B
Gradle__net_sf_kxml_kxml2_2_3_0_jar.xml 518B
Gradle__com_android_support_localbroadcastmanager_28_0_0.xml 513B
Gradle__com_android_support_constraint_constraint_layout_1_1_3.xml 509B
Gradle__com_android_support_asynclayoutinflater_28_0_0.xml 507B
Gradle__com_android_support_versionedparcelable_28_0_0.xml 507B
Gradle__com_android_support_support_core_utils_28_0_0.xml 504B
Gradle__com_android_support_swiperefreshlayout_28_0_0.xml 504B
Gradle__com_android_support_slidingpanelayout_28_0_0.xml 501B
Gradle__com_android_support_coordinatorlayout_28_0_0.xml 501B
Gradle__com_android_support_support_fragment_28_0_0.xml 498B
Gradle__com_android_support_recyclerview_v7_28_0_0.xml 495B
Gradle__com_android_support_support_core_ui_28_0_0.xml 495B
Gradle__junit_junit_4_12_jar.xml 495B
Gradle__com_android_support_support_compat_28_0_0.xml 492B
Gradle__com_android_support_cursoradapter_28_0_0.xml 489B
Gradle__com_android_support_drawerlayout_28_0_0.xml 486B
Gradle__com_android_support_appcompat_v7_28_0_0.xml 486B
Gradle__com_android_support_interpolator_28_0_0.xml 486B
Gradle__com_android_support_documentfile_28_0_0.xml 486B
Gradle__com_android_support_customview_28_0_0.xml 480B
Gradle__com_android_support_viewpager_28_0_0.xml 477B
Gradle__com_android_support_loader_28_0_0.xml 468B
Gradle__com_android_support_print_28_0_0.xml 465B
Gradle__com_android_support_constraint_constraint_layout_solver_1_1_3_jar.xml 433B
Gradle__com_android_support_support_annotations_28_0_0_jar.xml 399B
styles.xml 392B
Gradle__com_android_support_collections_28_0_0_jar.xml 375B
共 106 条
- 1
- 2
资源评论
- renjianbo20162020-11-21效果不错,但是使用起来根据业务改动的地方太多了。谢谢!
冬季穿短裤
- 粉丝: 32
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于 Java的扫雷程序毕业设计
- axure9 里面有码 axure9 里面有码
- 551-2404242311-1.amr
- 自己搭建的关于springboot整合mybatis的框架.zip
- 这是一个保存Springboot+MyBaits项目的仓库.zip
- tensorflow-gpu-2.7.2-cp39-cp39-manylinux2010-x86-64.whl
- tensorflow-2.7.4-cp38-cp38-manylinux2010-x86-64.whl
- tensorflow-gpu-2.7.4-cp38-cp38-manylinux2010-x86-64.whl
- 詹佳 陈敏 口语2.mp3
- 图书管理系统毕业设计Java语言实现
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功