这篇文章将为大家详细讲解有关android怎么实现条目倒计时功能,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
创新互联自成立以来,一直致力于为企业提供从网站策划、网站设计、网站设计、成都网站设计、电子商务、网站推广、网站优化到为企业提供个性化软件开发等基于互联网的全面整合营销服务。公司拥有丰富的网站建设和互联网应用系统开发管理经验、成熟的应用系统解决方案、优秀的网站开发工程师团队及专业的网站设计师团队。
网上对于这样的功能已经是泛滥成河了,但是最近遇到这样的一个需求,还是要值得我们学习一下,并将他记录下来。
布局文件:
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); Listlist = 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{ private final String TAG = CountDownAdapter.class.getSimpleName(); private final int STOP = 0x01; private final int START = 0x02; private final int LOOP = 0x03; private List mDatas; public CountDownAdapter() { } public void setmDatas(List 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 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 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; } }
关于“android怎么实现条目倒计时功能”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
售后响应及时
7×24小时客服热线数据备份
更安全、更高效、更稳定价格公道精准
项目经理精准报价不弄虚作假合作无风险
重合同讲信誉,无效全额退款