欢迎来到入门教程网!

Android

当前位置:主页 > 软件编程 > Android >

android实现条目倒计时功能

来源:本站原创|时间:2020-01-10|栏目: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;
 }
}

以上就是条目中倒计时的一个小Demo。

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

上一篇:Android手电筒兼容各个手机与版本

栏    目:Android

下一篇:Android 7.0 手电筒控制实现

本文标题:android实现条目倒计时功能

本文地址:https://www.xiuzhanwang.com/a1/Android/9181.html

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 脚本教程网 版权所有