不使用notifyDataSetChanged更新ListView

有时候,我们在开发ListView的时候,可能item中只有一个控件要频繁刷新(1次/1秒),这时候,如果使用notifyDataSetChanged代价太大了,这就会导致我们在滑动ListView的时候出现卡顿。比如我们要实现如下界面效果的话,部分代码为:

private View view;
    private CurrentTask currentTask;
    private Handler timeHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (mlv_customed_task != null) {
                int size = mlv_customed_task.getChildCount();
                for (int i = 0; i < size; i++) {
                    view = mlv_customed_task.getChildAt(i);
                    currentTask = currentTasks.get(i);

                    TextView tv_day = (TextView) view.findViewById(R.id.tv_day);
                    TextView tv_hour = (TextView) view.findViewById(R.id.tv_hour);
                    TextView tv_minute = (TextView) view.findViewById(R.id.tv_minute);
                    TextView tv_second = (TextView) view.findViewById(R.id.tv_second);

                    String overTimestamp = currentTask.getOver_time();
                    if (!TextUtils.isEmpty(overTimestamp)) {
                        long over = Long.parseLong(overTimestamp);
                        long remainTimes = over - System.currentTimeMillis()/1000;
                        String[] date = DateTool.getOverTimeBySeconds(remainTimes);
                        tv_day.setText(String.format(mActivity.getResources().getString(R.string.current_task_day), date[0]));
                        tv_hour.setText(String.format(mActivity.getResources().getString(R.string.current_task_hour), date[1]));
                        tv_minute.setText(String.format(mActivity.getResources().getString(R.string.current_task_minute), date[2]));
                        tv_second.setText(String.format(mActivity.getResources().getString(R.string.current_task_second), date[3]));
                    }
                }
            }
        }
    };


    private ScheduledExecutorService timeExecutor = Executors.newScheduledThreadPool(3);
    private Runnable timeRunnable;

原理就是遍历ListView中的child,然后找到该child中对应的控件,进行刷新即可。

这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ithouse

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值