Android中如何利用定时器和倒计时实现淘宝秒杀功能-创新互联
这篇文章主要介绍了Android中如何利用定时器和倒计时实现淘宝秒杀功能的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Android中如何利用定时器和倒计时实现淘宝秒杀功能文章都会有所收获,下面我们一起来看看吧。
专注于为中小企业提供网站设计制作、网站设计服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业大兴安岭免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上1000+企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。目录结构
效果图:
imageViewHolder
public class imageViewHolder extends RecyclerView.ViewHolder { public ImageView imageView; public imageViewHolder(View itemView) { super(itemView); imageView = (ImageView) itemView; } }
MyViewHolder
public class MyViewHolder extends RecyclerView.ViewHolder { public TextView textView; public MyViewHolder(View itemView) { super(itemView); textView = (TextView) itemView; } }
recycleAdapter
package com.nodeprogress.snapupview.SnapUp; import android.content.Context; import android.graphics.Color; import android.support.v7.widget.RecyclerView; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; public class recycleAdapter extends RecyclerView.Adapter { Context context; public recycleAdapter(Context context) { this.context = context; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if (viewType == 0){ return new imageViewHolder(new ImageView(context)); }else { return new MyViewHolder(new TextView(context)); } } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { if (getItemViewType(position) == 0) { imageViewHolder viewHolder = (imageViewHolder) holder; viewHolder.imageView.setPadding(150,20,20,20); viewHolder.imageView.setBackgroundColor(Color.BLUE); } else { MyViewHolder viewHolder = (MyViewHolder) holder; viewHolder.textView.setText(" 淘宝 " + position); } } @Override public int getItemCount() { return 21; } @Override public int getItemViewType(int position) { return (position == 20) ? 0 : 1; } }
MainActivity
package com.nodeprogress.snapupview; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import com.nodeprogress.snapupview.SnapUp.recycleAdapter; import com.nodeprogress.snapupview.View.HorizontalRecycleViewLoadMore; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); HorizontalRecycleViewLoadMore recyclerView = (HorizontalRecycleViewLoadMore) findViewById(R.id.recycle); recyclerView.setAdapter(new recycleAdapter(MainActivity.this)); SnapUpCountDownTimerView rushBuyCountDownTimerView = (SnapUpCountDownTimerView) findViewById(R.id.RushBuyCountDownTimerView); rushBuyCountDownTimerView.setTime(1,55,3); rushBuyCountDownTimerView.start(); } }
SnapUpCountDownTimerView
package com.nodeprogress.snapupview; import android.annotation.SuppressLint; import android.content.Context; import android.content.res.TypedArray; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import java.util.Timer; import java.util.TimerTask; @SuppressLint("HandlerLeak") public class SnapUpCountDownTimerView extends LinearLayout { private TextView tv_hour_decade; private TextView tv_hour_unit; private TextView tv_min_decade; private TextView tv_min_unit; private TextView tv_sec_decade; private TextView tv_sec_unit; private Context context; private int hour_decade; private int hour_unit; private int min_decade; private int min_unit; private int sec_decade; private int sec_unit; private Timer timer; private Handler handler = new Handler() { public void handleMessage(Message msg) { countDown(); } }; public SnapUpCountDownTimerView(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.view_countdowntimer, this); tv_hour_decade = (TextView) view.findViewById(R.id.tv_hour_decade); tv_hour_unit = (TextView) view.findViewById(R.id.tv_hour_unit); tv_min_decade = (TextView) view.findViewById(R.id.tv_min_decade); tv_min_unit = (TextView) view.findViewById(R.id.tv_min_unit); tv_sec_decade = (TextView) view.findViewById(R.id.tv_sec_decade); tv_sec_unit = (TextView) view.findViewById(R.id.tv_sec_unit); TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.SnapUpCountDownTimerView); int size = array.getInteger(R.styleable.SnapUpCountDownTimerView_viewSize, 12); tv_hour_decade.setTextSize(size); tv_hour_unit.setTextSize(size); tv_min_decade.setTextSize(size); tv_min_unit.setTextSize(size); tv_sec_decade.setTextSize(size); tv_sec_unit.setTextSize(size); ((TextView)view.findViewById(R.id.colon_minute)).setTextSize(size); ((TextView)view.findViewById(R.id.colon_hour)).setTextSize(size); } public void start() { if (timer == null) { timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { handler.sendEmptyMessage(0); } }, 0, 1000); } } public void stop() { if (timer != null) { timer.cancel(); timer = null; } } public void setTime(int hour, int min, int sec) { if (hour >= 60 || min >= 60 || sec >= 60 || hour < 0 || min < 0 || sec < 0) { throw new RuntimeException("时间格式错误,请检查你的代码"); } hour_decade = hour / 10; hour_unit = hour - hour_decade * 10; min_decade = min / 10; min_unit = min - min_decade * 10; sec_decade = sec / 10; sec_unit = sec - sec_decade * 10; tv_hour_decade.setText(hour_decade + ""); tv_hour_unit.setText(hour_unit + ""); tv_min_decade.setText(min_decade + ""); tv_min_unit.setText(min_unit + ""); tv_sec_decade.setText(sec_decade + ""); tv_sec_unit.setText(sec_unit + ""); } private void countDown() { if (isCarry4Unit(tv_sec_unit)) { if (isCarry4Decade(tv_sec_decade)) { if (isCarry4Unit(tv_min_unit)) { if (isCarry4Decade(tv_min_decade)) { if (isCarry4Unit(tv_hour_unit)) { if (isCarry4Decade(tv_hour_decade)) { Toast.makeText(context, "计数完成", Toast.LENGTH_SHORT).show(); stop(); setTime(0, 0, 0);//重置为0 } } } } } } } private boolean isCarry4Decade(TextView tv) { int time = Integer.valueOf(tv.getText().toString()); time = time - 1; if (time < 0) { time = 5; tv.setText(time + ""); return true; } else { tv.setText(time + ""); return false; } } private boolean isCarry4Unit(TextView tv) { int time = Integer.valueOf(tv.getText().toString()); time = time - 1; if (time < 0) { time = 9; tv.setText(time + ""); return true; } else { tv.setText(time + ""); return false; } } }
activity_main.xml
home_snap_up.xml
" android:textSize="15sp" />
view_countdowntimer.xml
关于“Android中如何利用定时器和倒计时实现淘宝秒杀功能”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“Android中如何利用定时器和倒计时实现淘宝秒杀功能”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注创新互联行业资讯频道。
本文名称:Android中如何利用定时器和倒计时实现淘宝秒杀功能-创新互联
网址分享:http://pwwzsj.com/article/dgoisc.html