最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

android特賣列表倒計時卡頓問題的解決方法

 更新時間:2019年09月20日 11:39:32   作者:xiangzhihong8  
這篇文章主要為大家詳細(xì)介紹了android特賣列表倒計時卡頓問題的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

在Android的開發(fā)中,我們經(jīng)常遇見倒計時的操作,通常使用Timer和Handler共同操作來完成。當(dāng)然也可以使用Android系統(tǒng)控件CountDownTimer,這里我們封裝成一個控件,也方便大家的使用。

首先上一張效果圖吧:

說一下造成卡頓的原因,由于滑動的時候,adapter的getView頻繁的創(chuàng)建和銷毀,就會出現(xiàn)卡頓和數(shù)據(jù)錯位問題,那么我們每一個item的倒計時就需要單獨維護(hù),這里我用的Handler與timer及TimerTask結(jié)合的方法,我們知道TimerTask運行在自己子線程,然后通過Timer的schedule()方法實現(xiàn)倒計時功能,最后通過Hander實現(xiàn)View的刷新,其核心代碼如下:

public class CountDownView extends LinearLayout {
 
 @BindView(R.id.tv_day)
 TextView tvDay;
 @BindView(R.id.tv_hour)
 TextView tvHour;
 @BindView(R.id.tv_minute)
 TextView tvMinute;
 @BindView(R.id.tv_second)
 TextView tvSecond;
 
 @BindView(R.id.day)
 TextView day;
 @BindView(R.id.hour)
 TextView hour;
 @BindView(R.id.minute)
 TextView minute;
 
 private Context context;
 
 private int viewBg;//倒計時的背景
 private int cellBg;//每個倒計時的背景
 private int cellTextColor;//文字顏色
 private int textColor;//外部:等顏色
 private int textSize = 14;//外部文字大小
 private int cellTextSize = 12;//cell文字大小
 
 private TimerTask timerTask = null;
 private Timer timer = new Timer();
 private Handler handler = new Handler() {
 
 public void handleMessage(Message msg) {
  countDown();
 }
 };
 
 public CountDownView(Context context, AttributeSet attrs) {
 this(context, attrs, 0);
 this.context = context;
 }
 
 public CountDownView(Context context, AttributeSet attrs, int defStyleAttr) {
 super(context, attrs, defStyleAttr);
 this.context = context;
 initAttrs(attrs, defStyleAttr);
 initView(context);
 }
 
 private void initAttrs(AttributeSet attrs, int defStyle) {
 TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.CountDownView, defStyle,0);
 viewBg = typedArray.getColor(R.styleable.CountDownView_viewBg, Color.parseColor("#FFFFFF"));
 cellBg = typedArray.getColor(R.styleable.CountDownView_cellBg, Color.parseColor("#F4F4F4"));
 cellTextColor = typedArray.getColor(R.styleable.CountDownView_cellTextColor, Color.parseColor("#646464"));
 textColor = typedArray.getColor(R.styleable.CountDownView_TextColor, Color.parseColor("#B3B3B3"));
 textSize = (int) typedArray.getDimension(R.styleable.CountDownView_TextSize, UIUtils.dp2px(getContext(), 14));
 cellTextSize = (int) typedArray.getDimension(R.styleable.CountDownView_cellTextSize, UIUtils.dp2px(getContext(), 12));
 typedArray.recycle();
 
 }
 
 private void initView(Context context) {
 LayoutInflater inflater = (LayoutInflater) context
  .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 View view = inflater.inflate(R.layout.layout_countdown_layout, this);
 ButterKnife.bind(view);
 
 initProperty();
 }
 
 private void initProperty() {
 tvDay.setBackgroundColor(cellBg);
 tvHour.setBackgroundColor(cellBg);
 tvMinute.setBackgroundColor(cellBg);
 tvSecond.setBackgroundColor(cellBg);
 
 tvDay.setTextColor(cellTextColor);
 tvHour.setTextColor(cellTextColor);
 tvMinute.setTextColor(cellTextColor);
 tvSecond.setTextColor(cellTextColor);
 
 day.setTextColor(textColor);
 hour.setTextColor(textColor);
 minute.setTextColor(textColor);
 }
 
 
 public void setLeftTime(long leftTime) {
 if (leftTime <= 0) return;
 long time = leftTime / 1000;
 long day = time / (3600 * 24);
 long hours = (time - day * 3600 * 24) / 3600;
 long minutes = (time - day * 3600 * 24 - hours * 3600) / 60;
 long seconds = time - day * 3600 * 24 - hours * 3600 - minutes * 60;
 
 setTextTime(time);
 }
 
 
 public void start() {
 if (timerTask == null) {
  timerTask = new TimerTask() {
  @Override
  public void run() {
   handler.sendEmptyMessage(0);
  }
 
  };
  timer.schedule(timerTask, 1000, 1000);
//  timer.schedule(new TimerTask() {
//  @Override
//  public void run() {
//   handler.sendEmptyMessage(0);
//  }
//  }, 0, 1000);
 }
 }
 
 public void stop() {
 if (timer != null) {
  timer.cancel();
  timer = null;
 }
 }
 
 //保證天,時,分,秒都兩位顯示,不足的補(bǔ)0
 private void setTextTime(long time) {
 String[] s = TimeUtils.formatTimer(time);
 tvDay.setText(s[0]);
 tvHour.setText(s[1]);
 tvMinute.setText(s[2]);
 tvSecond.setText(s[3]);
 }
 
 private void countDown() {
 if (isCarry4Unit(tvSecond)) {
  if (isCarry4Unit(tvMinute)) {
  if (isCarry4Unit(tvHour)) {
   if (isCarry4Unit(tvDay)) {
   stop();
   }
  }
  }
 }
 }
 
 private boolean isCarry4Unit(TextView tv) {
 int time = Integer.valueOf(tv.getText().toString());
 time = time - 1;
 if (time < 0) {
  time = 59;
  tv.setText(time + "");
  return true;
 } else if (time < 10) {
  tv.setText("0" + time);
  return false;
 } else {
  tv.setText(time + "");
  return false;
 }
 }
}

另一種寫法:

public class CountDownTimerView extends LinearLayout {
 
 private TextView hourView, minuteView, secondView;
 private LimitTimer mTimer;
 private Handler handler;
 public static final int START_LIMIT_TIME_MSG = 0x0111;
 public static final int END_LIMIT_TIME_MSG = 0x0112;
 private long endTime, leftTime;
 private LimitTimeListener listener=null;
 
 public CountDownTimerView(Context context) {
 super(context);
 initView(context);
 }
 
 public CountDownTimerView(Context context, AttributeSet attrs) {
 super(context, attrs);
 initView(context);
 }
 
 private void initView(Context context) {
 setOrientation(HORIZONTAL);
 setGravity(Gravity.CENTER_VERTICAL);
 inflate(context, R.layout.layout_countdown_layout, this);
 hourView = (TextView) findViewById(R.id.tv_hour);
 minuteView = (TextView) findViewById(R.id.tv_minute);
 secondView = (TextView) findViewById(R.id.tv_second);
 }
 
 public long getLeftTime() {
 return leftTime;
 }
 
 //設(shè)置剩余時間
 public void initLeftTime(long endTime) {
 endTime=endTime*1000;
 if (null != mTimer) {
  mTimer.cancel();
  mTimer = null;
 }
 this.endTime = endTime;
 mTimer = new LimitTimer(endTime, 1000);
 mTimer.start();
 if (handler != null) {
  handler.sendEmptyMessage(START_LIMIT_TIME_MSG);
 }
 }
 
 /**
 * 如果該控件使用在碎片中,返回時,則最好還是要stop
 */
 public void stopTimeCount() {
 if (null != mTimer) {
  mTimer.cancel();
  mTimer = null;
 }
 }
 
 public Handler getHandler() {
 return handler;
 }
 
 public void setHandler(Handler handler) {
 this.handler = handler;
 }
 
 private class LimitTimer extends CountDownTimer {
 
 public LimitTimer(long millisInFuture, long countDownInterval) {
  super(0 != leftTime ? leftTime : endTime, countDownInterval);
 }
 
 @Override
 public void onTick(long millisUntilFinished) {
  leftTime = millisUntilFinished;
  long totalSecond = millisUntilFinished / 1000;
  int second = (int) (totalSecond % 60);
  int minute = (int) ((totalSecond / 60) % 60);
  int hour = (int) ((totalSecond / 3600) % 24);
  int day = (int) (totalSecond / (3600 * 24));
 
  formatView(hourView, hour);
  formatView(minuteView, minute);
  formatView(secondView, second);
 }
 
 @Override
 public void onFinish() {
  String zero = "00";
  hourView.setText(zero);
  minuteView.setText(zero);
  secondView.setText(zero);
  if (null != handler) {
  handler.sendEmptyMessage(END_LIMIT_TIME_MSG);
  }
  if (listener!=null){
  listener.onTimeOver(true);
  }
 }
 
 private void formatView(TextView view, int time) {
  DecimalFormat df = new DecimalFormat("#00");
  view.setText(df.format(time));
 }
 }
 
 //倒計時結(jié)束監(jiān)聽
 public void setOnLimitTimeListener(LimitTimeListener listener) {
 this.listener = listener;
 }
 public interface LimitTimeListener {
 void onTimeOver(boolean flag);
 }
 
 @Override
 protected void onDetachedFromWindow() {
 super.onDetachedFromWindow();
 if (handler != null) {
  handler.removeMessages(START_LIMIT_TIME_MSG);
 }
 }
}

涉及到的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:orientation="horizontal">
 
 
 <TextView
 android:id="@+id/tv_hour"
 style="@style/style_countdown" />
 
 <TextView
 style="@style/style_wrap_content"
 android:layout_marginLeft="3dp"
 android:layout_marginRight="3dp"
 android:layout_gravity="center_vertical"
 android:text="時"
 android:textColor="@color/color_646464" />
 
 <TextView
 android:id="@+id/tv_minute"
 style="@style/style_countdown" />
 
 <TextView
 style="@style/style_wrap_content"
 android:layout_marginLeft="3dp"
 android:layout_marginRight="3dp"
 android:layout_gravity="center_vertical"
 android:text="分"
 android:textColor="@color/color_646464" />
 
 <TextView
 android:id="@+id/tv_second"
 style="@style/style_countdown" />
 
 <TextView
 style="@style/style_wrap_content"
 android:layout_marginLeft="3dp"
 android:layout_marginRight="3dp"
 android:layout_gravity="center_vertical"
 android:text="秒"
 android:textColor="@color/color_646464" />
</LinearLayout>

附上源碼地址:點擊打開鏈接

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • flutter Bloc 更新后事件同步與異步詳解

    flutter Bloc 更新后事件同步與異步詳解

    這篇文章主要為大家介紹了flutter Bloc 更新后事件同步與異步詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • Android如何獲取視頻首幀圖片

    Android如何獲取視頻首幀圖片

    這篇文章主要為大家詳細(xì)介紹了Android如何獲取視頻首幀圖片的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • Android?Studio支持安卓手機(jī)投屏功能詳解

    Android?Studio支持安卓手機(jī)投屏功能詳解

    這篇文章主要給大家介紹了關(guān)于Android?Studio支持安卓手機(jī)投屏功能的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),對有需要的朋友具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2023-01-01
  • 詳解Android10的分區(qū)存儲機(jī)制(Scoped Storage)適配教程

    詳解Android10的分區(qū)存儲機(jī)制(Scoped Storage)適配教程

    這篇文章主要介紹了詳解Android10的分區(qū)存儲機(jī)制(Scoped Storage)適配教程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • Android版本更新實例詳解

    Android版本更新實例詳解

    這篇文章主要介紹了Android版本更新實例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Android左滑返回功能的實現(xiàn)示例代碼

    Android左滑返回功能的實現(xiàn)示例代碼

    本篇文章主要介紹了Android左滑返回的實現(xiàn)示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • Notification消息通知 自定義消息通知內(nèi)容布局

    Notification消息通知 自定義消息通知內(nèi)容布局

    這篇文章主要為大家詳細(xì)介紹了Notification消息通知,消息合并且顯示條數(shù),自定義消息通知內(nèi)容布局,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • Android 項目正式簽名打包教程分享

    Android 項目正式簽名打包教程分享

    這篇文章主要介紹了Android 項目正式簽名打包教程分享,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • Android VelocityTracker使用案例詳解

    Android VelocityTracker使用案例詳解

    這篇文章主要介紹了Android VelocityTracker使用案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • Android實現(xiàn)九宮格手勢密碼

    Android實現(xiàn)九宮格手勢密碼

    這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)九宮格手勢密碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06

最新評論

会昌县| 岑巩县| 桑植县| 延庆县| 德惠市| 吴桥县| 南和县| 广河县| 乃东县| 岳阳县| 松潘县| 彝良县| 共和县| 彰化市| 阿合奇县| 博野县| 武威市| 文昌市| 屯留县| 兴安县| 六枝特区| 武功县| 兰溪市| 富蕴县| 遂平县| 南召县| 棋牌| 胶州市| 通州区| 西畴县| 海盐县| 晋中市| 长垣县| 容城县| 常熟市| 新蔡县| 承德市| 海盐县| 连平县| 基隆市| 涡阳县|