android倒计时功能,android 实现列表倒计时功能 -ag凯发k8国际
android 实现列表倒计时功能
发布时间:2020-08-21 21:47:11
来源:脚本之家
阅读:147
作者:choi晨
单个计时器,然后遍历数据 刷新条目;
两种实现方式:1、handler轮询; 2、子线程睡眠(时间到后 移除列表中的条目会有问题);
代码很简单,没有任何难度,列表使用recyclerview baserecyclerviewadapterhelper实现;
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'com.github.cymchad:baserecyclerviewadapterhelper:3.0.1'
public class mainactivity extends appcompatactivity {
private recyclerview rv;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
rv = findviewbyid(r.id.rv);
initview();
initdata();
findviewbyid(r.id.btn).setonclicklistener(new view.onclicklistener() {
@override
public void onclick(view view) {
quickadapter.adddata(0,new timebean("附加商品、离活动结束还剩:" , 99));
rv.scrolltoposition(0);
}
});
}
private quickadapter quickadapter;
private void initview() {
quickadapter = new quickadapter(r.layout.item);
rv.setlayoutmanager(new linearlayoutmanager(this));
rv.setadapter(quickadapter);
rv.setitemanimator(null);
countdown();
}
private void initdata() {
list datas = new arraylist<>();
for (int i = 1; i < 10; i ) {
datas.add(new timebean("商品" i "、离活动结束还剩:", (i 5) * i));
}
quickadapter.setnewdata(datas);
}
private class quickadapter extends basequickadapter {
public quickadapter(int layoutresid) {
super(layoutresid);
}
@override
protected void convert(baseviewholder vh, timebean datas) {
vh.settext(r.id.tv, datas.getstr() "");
vh.settext(r.id.tv2, datas.gettime() "s");
}
}
private handler mhandler = new handler();
private runnable runnable;
private void countdown() {
runnable = new runnable() {
@override
public void run() {
for (int i = 0; i < quickadapter.getdata().size(); i ) {
timebean bean = quickadapter.getdata().get(i);
if (bean.gettime() > 0) {
bean.settime(bean.gettime() - 1);
quickadapter.setdata(i, bean);
} else {
quickadapter.remove(i);
}
}
mhandler.postdelayed(runnable, 1000l);
}
};
mhandler.postdelayed(runnable, 1000l);
}
private void countdown2() {
new thread(new runnable() {
@override
public void run() {
while (true){
systemclock.sleep(1000l);
for (int i = 0; i < quickadapter.getdata().size(); i ) {
final timebean bean = quickadapter.getdata().get(i);
final int finali = i;
if (bean.gettime() > 0) {
bean.settime(bean.gettime() - 1);
runonuithread(new runnable() {
@override
public void run() {
quickadapter.setdata(finali, bean);
}
});
} else {
// 当时间是0时 移除条目(子线程加睡眠模式移除条目有问题,原因时数据源未更新)
runonuithread(new runnable() {
@override
public void run() {
quickadapter.remove(finali);
}
});
}
}
}
}
}).start();
}
@override
protected void ondestroy() {
super.ondestroy();
mhandler.removecallbacks(runnable);
mhandler.removecallbacksandmessages(null);
mhandler = null;
}
public class timebean {
public timebean(string str, int time) {
this.str = str;
this.time = time;
}
private string str;
private int time;
}
get 、 set方法就不写了;
布局文件:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".mainactivity">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@ id/btn"
android:text="add"
/>
android:id="@ id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent">
//下面是item
android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:paddingbottom="@dimen/dp_10"
android:layout_height="60dp">
android:background="#33fb1212"
android:id="@ id/tv"
tools:text="你好啊"
android:gravity="center_vertical"
android:textsize="17dp"
android:paddingleft="15dp"
android:textcolor="#333"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent" />
android:background="#33fb1212"
android:id="@ id/tv2"
tools:text="0s"
android:gravity="center_vertical"
android:textsize="17dp"
android:paddingleft="15dp"
android:textcolor="#333"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
总结
到此这篇关于android 实现列表倒计时功能的文章就介绍到这了,更多相关android 列表倒计时内容请搜索亿速云以前的文章或继续浏览下面的相关文章希望大家以后多多支持亿速云!
总结
以上是ag凯发k8国际为你收集整理的android倒计时功能,android 实现列表倒计时功能的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 中柏ezpade7装linux,中柏ez
- 下一篇: