美文网首页最近需要做的RecyclerView
Android利用RecyclerView实现列表倒计时

Android利用RecyclerView实现列表倒计时

作者: _xiangpan | 来源:发表于2018-03-21 17:55 被阅读109次

    利用RecyclerView实现一个非常简单的列表倒计时


    一、先上效果图

    倒计时

    二、GitHub

    代码地址,欢迎指正https://github.com/MNXP/Countdown

    三、思路

     在adapter中设置timer倒计时,然后利用RecyclerView更新单个item特性实现
    

    四、实现

    1)变量

     useTime倒计时时间
     timeFlag用来判断是否有倒计时,倒计时结束更新UI
    

    2)倒计时

    遍历list,设置时间useTime-1000,然后判断时间和timeFlag更新显示
    
       private void startTime() {
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    ((Activity) mContext).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            for (int i = 0; i < mTimeDownBeanList.size(); i++) {
                                long useTime = mTimeDownBeanList.get(i).getUseTime();
                                if (useTime > 1000) {
                                    useTime -= 1000;
                                    mTimeDownBeanList.get(i).setUseTime(useTime);
                                    if (useTime <= 1000 && mTimeDownBeanList.get(i).isTimeFlag()) {
                                        mTimeDownBeanList.get(i).setTimeFlag(false);
                                        CountDownAdapter.this.notifyItemChanged(i);
                                    }else {
                                        mTimeDownBeanList.get(i).setTimeFlag(true);
                                        CountDownAdapter.this.notifyItemChanged(i);
                                    }
    
                                }
    
                            }
    
                        }
                    });
    
    
                }
            }, 0, 1000);
        }
    

    3)常规操作(设置倒计时时间)

      private void setTime(ViewHolder holder, int position) throws ParseException{
            TimeDownBean timeDownBean = mTimeDownBeanList.get(position);
            if (timeDownBean.getUseTime() > 1000) {
                holder.timeTv.setVisibility(View.VISIBLE);
                long useTime = timeDownBean.getUseTime();
                useTime = useTime / 1000;
                setTimeShow(useTime, holder);
    
            }else {
                holder.timeTv.setVisibility(View.GONE);
            }
    
        }
    
        private void setTimeShow(long useTime, ViewHolder holder) {
            int hour = (int) (useTime / 3600 );
            int min = (int) (useTime / 60 % 60);
            int second = (int) (useTime % 60);
            int day = (int) (useTime / 3600 / 24);
            String mDay, mHour, mMin, mSecond;//天,小时,分钟,秒
            second--;
            if (second < 0) {
                min--;
                second = 59;
                if (min < 0) {
                    min = 59;
                    hour--;
                }
            }
            if (hour < 10) {
                mHour = "0" + hour;
            } else {
                mHour = "" + hour;
            }
            if (min < 10) {
                mMin = "0" + min;
            } else {
                mMin = "" + min;
            }
            if (second < 10) {
                mSecond = "0" + second;
            } else {
                mSecond = "" + second;
            }
            String strTime = "上架倒计时 " +mHour + ":" + mMin + ":" + mSecond + "";
            holder.timeTv.setText(strTime);
    
        }
    

    4)注意

    防止更新item时防止抖动
    ((SimpleItemAnimator) mRecycleListView.getItemAnimator()).setSupportsChangeAnimations(false);
    

    5)Adapter完整代码

    public class CountDownAdapter extends RecyclerView.Adapter {
        private Context mContext;
        private List<TimeDownBean> mTimeDownBeanList;
        public CountDownAdapter(Context context,List<TimeDownBean> mTimeDownBeanList){
            this.mContext = context;
            this.mTimeDownBeanList = mTimeDownBeanList;
            startTime();
        }
    
    
        /**
         * 列表倒计时
         */
        private void startTime() {
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    ((Activity) mContext).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            for (int i = 0; i < mTimeDownBeanList.size(); i++) {
                                long useTime = mTimeDownBeanList.get(i).getUseTime();
                                if (useTime > 1000) {
                                    useTime -= 1000;
                                    mTimeDownBeanList.get(i).setUseTime(useTime);
                                    if (useTime <= 1000 && mTimeDownBeanList.get(i).isTimeFlag()) {
                                        mTimeDownBeanList.get(i).setTimeFlag(false);
                                        CountDownAdapter.this.notifyItemChanged(i);
                                    }else {
                                        mTimeDownBeanList.get(i).setTimeFlag(true);
                                        CountDownAdapter.this.notifyItemChanged(i);
                                    }
    
                                }
    
                            }
    
                        }
                    });
    
    
                }
            }, 0, 1000);
        }
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = View.inflate(mContext,R.layout.count_down_item,null);
            return new ViewHolder(view);
        }
    
        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            if (holder instanceof ViewHolder){
                ViewHolder viewHolder = (ViewHolder) holder;
                TimeDownBean timeDownBean = mTimeDownBeanList.get(position);
                viewHolder.contentTv.setText(timeDownBean.getContent());
                try {
                    setTime(viewHolder, position);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
    
    
        }
    
        private void setTime(ViewHolder holder, int position) throws ParseException{
            TimeDownBean timeDownBean = mTimeDownBeanList.get(position);
            if (timeDownBean.getUseTime() > 1000) {
                holder.timeTv.setVisibility(View.VISIBLE);
                long useTime = timeDownBean.getUseTime();
                useTime = useTime / 1000;
                setTimeShow(useTime, holder);
    
            }else {
                holder.timeTv.setVisibility(View.GONE);
            }
    
        }
    
        private void setTimeShow(long useTime, ViewHolder holder) {
            int hour = (int) (useTime / 3600 );
            int min = (int) (useTime / 60 % 60);
            int second = (int) (useTime % 60);
            int day = (int) (useTime / 3600 / 24);
            String mDay, mHour, mMin, mSecond;//天,小时,分钟,秒
            second--;
            if (second < 0) {
                min--;
                second = 59;
                if (min < 0) {
                    min = 59;
                    hour--;
                }
            }
            if (hour < 10) {
                mHour = "0" + hour;
            } else {
                mHour = "" + hour;
            }
            if (min < 10) {
                mMin = "0" + min;
            } else {
                mMin = "" + min;
            }
            if (second < 10) {
                mSecond = "0" + second;
            } else {
                mSecond = "" + second;
            }
            String strTime = "上架倒计时 " +mHour + ":" + mMin + ":" + mSecond + "";
            holder.timeTv.setText(strTime);
    
        }
    
    
        @Override
        public int getItemCount() {
            return mTimeDownBeanList!=null?mTimeDownBeanList.size():0;
        }
    
    
    
    
        private class ViewHolder extends RecyclerView.ViewHolder{
            private TextView contentTv;
            private TextView timeTv;
            private ViewHolder(View itemView) {
                super(itemView);
                init(itemView);
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (itemClickListener!=null){
                            itemClickListener.onItemClick(v,getLayoutPosition());
                        }
                    }
                });
            }
    
            private void init(View itemView) {
                contentTv = (TextView)itemView.findViewById(R.id.content_tv);
                timeTv = (TextView)itemView.findViewById(R.id.time_tv);
            }
        }
    
    
    
        //对外部暴漏点击事件接口
        public interface OnItemClickListener{
            void onItemClick(View v,int position);
        }
    
        public OnItemClickListener itemClickListener;
    
        public void setItemClickListener(OnItemClickListener itemClickListener) {
            this.itemClickListener = itemClickListener;
        }
    }
    
    

    如有意见和建议,及时沟通。

    相关文章

      网友评论

        本文标题:Android利用RecyclerView实现列表倒计时

        本文链接:https://www.haomeiwen.com/subject/vczgqftx.html