在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Android  HTML/ 請問,如何獲取到RecyclerView中每個item在屏幕上顯示的時長

請問,如何獲取到RecyclerView中每個item在屏幕上顯示的時長

有個需求,要能獲取到RecyclerView中每個item在屏幕上顯示的時長,哪位大神知道

回答
編輯回答
情皺
只要找到View(ViewHolder)顯示隱藏的調(diào)用函數(shù),就能計算出每個View(ViewHolder)的顯示時長。

很幸運的時,RecyclerView提供了這樣的接口函數(shù),而且還是兩對接口函數(shù):

1.RecyclerView#Adapter提供了一對函數(shù):onViewAttachedToWindow(VH)onViewDetachedFromWindow(VH)

public abstract static class Adapter<VH extends ViewHolder> {
    ... ...
    
    /**
     * Called when a view created by this adapter has been attached to a window.
     *
     * <p>This can be used as a reasonable signal that the view is about to be seen
     * by the user. If the adapter previously freed any resources in
     * {@link #onViewDetachedFromWindow(RecyclerView.ViewHolder) onViewDetachedFromWindow}
     * those resources should be restored here.</p>
     *
     * @param holder Holder of the view being attached
     */
    public void onViewAttachedToWindow(VH holder) {
    }

    /**
     * Called when a view created by this adapter has been detached from its window.
     *
     * <p>Becoming detached from the window is not necessarily a permanent condition;
     * the consumer of an Adapter's views may choose to cache views offscreen while they
     * are not visible, attaching and detaching them as appropriate.</p>
     *
     * @param holder Holder of the view being detached
     */
    public void onViewDetachedFromWindow(VH holder) {
    }

    ... ...
}

2.RecyclerView提供了OnChildAttachStateChangeListener接口:

public interface OnChildAttachStateChangeListener {

    /**
     * Called when a view is attached to the RecyclerView.
     *
     * @param view The View which is attached to the RecyclerView
     */
    void onChildViewAttachedToWindow(View view);

    /**
     * Called when a view is detached from RecyclerView.
     *
     * @param view The View which is being detached from the RecyclerView
     */
    void onChildViewDetachedFromWindow(View view);
}

上面的兩對接口函數(shù),根據(jù)情況選用其一就好了,基本實現(xiàn)邏輯:

  1. 設(shè)置兩個變量,一個用來保存AttachedToWindow被調(diào)用時的時間戳,一個用來保存顯示的總時長
  2. DetachedFromWindow被調(diào)用時,計算與AttachedToWindow的時間戳差值,并將AttachedToWindow的時間戳清零,然后總時長加上這個差值
  3. AttachedToWindow后,DetachedFromWindow未被調(diào)用前,只需獲取當(dāng)前系統(tǒng)時間戳,然后計算與AttachedToWindow的時間戳差值,再加上總時長,就是總時長
2018年4月3日 22:36
編輯回答
詆毀你

可以使用HashMap記錄, 在bind時, position 為key, value為一個entity, 含start和end兩個成員,設(shè)置一個標(biāo)記, 記錄是否已經(jīng)記錄過時間。

2017年2月25日 17:02
編輯回答
局外人

嘗試使用,這個監(jiān)聽:

ViewTreeObserver.OnGlobalLayoutListener

參考:

2018年5月22日 07:19
編輯回答
骨殘心

第一次顯示的時候獲取系統(tǒng)時間來作為初始時間,再次顯示的時候已當(dāng)前系統(tǒng)時間減去第一次的時間

2017年3月10日 01:31