recyclerview定期UI更新的孩子 [英] recyclerview periodic ui child updates

查看:194
本文介绍了recyclerview定期UI更新的孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目recyclerView是,(等等),包含从WS检索的游戏的持续时间的时间戳。为了显示本场比赛的时间,我必须得到当前时间戳,执行垫,并将其转换为可读的格式(例如:5分钟5秒)。只有一些看法需要更新

I have a recyclerView of items that, (among others), contains a timeStamp of a duration of a game retrieved from a WS. To show the duration of the match, i have to get the current timeStamp, do the mat and convert it to a readable format (for eg: 5 minutes 5s). Only some of the views need to be updated.

每个childview需要每隔XXX秒进行更新。

Each childview needs to be updated every xxx seconds.


  • 我的第一个形式给出了创建绑定到了需​​要更新的视图对象的列表。在我的片段,我就会宣布一个处理程序,并调用notifyItemChanged(位置)列表中的每个对象。每个处理程序调用notifyItemChanged时间,整个视图重新渲染,并有一个小的闪烁效果。我不希望它发生,所以我的第二个方法是唯一的改变需要更新每个childView的TextView的。这里是code:

  • My first aproach was create a list of Objects binded to the view that needed to be updated. On my fragment i would declare a Handler and call notifyItemChanged(position) for every object in the list. Each time the handler calls notifyItemChanged, the whole view is re-rendered, and there's a small blink effect. I don't want it to happen, so my second approach was to only change the TextView of every childView that needed to be updated. Here is the code:

@Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
if (needsUpdate) {
            holder.startRepeatingTask();
        } else {
            holder.stopRepeatingTask();
        }
}
class MyViewHolder extends RecyclerView.ViewHolder {
        private final int mHandlerInterval = 6000;
        private Handler mHandler;
        private Runnable mStatusChecker;

public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.inject(this, itemView);
            mHandler = new Handler();
            mStatusChecker = new Runnable() {
                @Override
                public void run() {
                    String gameStatusToPrint = current.getGameStatusToPrint();
                    gameStatus.setText(gameStatusToPrint);
                    mHandler.postDelayed(mStatusChecker, mHandlerInterval);
                }
            };
        }

public void startRepeatingTask() {
            mStatusChecker.run();

}

公共无效STO prepeatingTask(){
            mHandler.removeCallbacks(mStatusChecker);
}
}

public void stopRepeatingTask() { mHandler.removeCallbacks(mStatusChecker); } }

我不知道这是否是最好的方法,需要对这一建议。

I'm not sure if this is the best approach and need recommendation on this.

推荐答案

随着ViewHolder是要由RecyclerView进行回收。你需要确保你保持当前游戏,并在列表上显示的项目之间的稳定性。尽管如此,我认为您的解决方案是好的。

As the ViewHolder is going to be recycled by the RecyclerView. You'll need to make sure you're maintaining the stability between the "current game" and the item shown on the list. Despite that i think your solution is fine.

下面是该修补程序:

@Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        holder.game = mGameList.get(position);
        if (needsUpdate) {
            holder.startRepeatingTask();
        } else {
            holder.stopRepeatingTask();
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private final int mHandlerInterval = 6000;
        private Handler mHandler;
        private Runnable mStatusChecker;

        public Game game;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.inject(this, itemView);
            mHandler = new Handler();
            mStatusChecker = new Runnable() {
                @Override
                public void run() {
                    String gameStatusToPrint = game.getGameStatusToPrint();
                    gameStatus.setText(gameStatusToPrint);
                    mHandler.postDelayed(mStatusChecker, mHandlerInterval);
                }
            };
        }

        public void startRepeatingTask() {
            mStatusChecker.run();
        }

这篇关于recyclerview定期UI更新的孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆