[英]Kotlin/RecyclerView: scrollToPositionWithOffset Not Showing Up
[英]RecyclerView scrollToPositionWithOffset with animation
我試圖設置卡片視圖的位置動畫,並同時調整其大小以適合屏幕。 當用戶單擊卡片視圖內的按鈕時,卡片視圖應擴展到其容器的大小,並向上滾動以同時完全可見
在我的自定義動畫師類中,即時通訊使用以下功能:
@Override
protected void applyTransformation(float interpolatedTimeTransformation t) {
int newHeight = (int) (startHeight + (targetHeight - startHeight) *interpolatedTime);
view.getLayoutParams().height = newHeight;
((LinearLayoutManager) MainPage.mainGroupRecycler.getLayoutManager()).scrollToPositionWithOffset(MainPage.mainGroupRecycler.getChildAdapterPosition((CardView) view.getParent()), 0);
view.requestLayout();
}
在以下情況下,recyclerview會立即在可見區域的頂部顯示cardview而不顯示滾動動畫,然后為調整大小動畫。 我需要它在調整大小的同時滾動。
我試圖調用另一個滾動功能:
MainPage.mainGroupRecycler.scrollToPosition(MainPage.mainGroupRecycler.getChildAdapterPosition((CardView) view.getParent()));
但是,這樣做的問題是,滾動僅在調整大小后才會動畫。
我需要同時調整大小和滾動。
任何幫助將不勝感激
為了使兩個效果同時發生,必須啟動視圖的動畫,並同時調用scrollToPosition
。
同樣,從動畫的applyTransformation
方法中刪除對“ scrollToPosition
”的調用。 這很不好,因為它告訴布局管理器它需要在動畫的每次迭代中滾動到某個位置,這可能會發生數百次。 您只需要調用一次scrollToPosition
。
另外,可以使用smoothScrollToPosition
而不是scrollToPosition
來獲得良好的平滑滾動效果。
我想出了解決方案。
在我的動畫師課程中:
@Override
protected void applyTransformation(float interpolatedTime, Transformation t){
calculatedOffset = (int) (tempOffset*interpolatedTime);
tempOffset = tempOffset-calculatedOffset;
recyclerView.scrollBy(0,calculatedOffset);
int newHeight = (int) (startHeight + (targetHeight - startHeight) * interpolatedTime);
LinearLayout ll = (LinearLayout)view.findViewById(R.id.cardchild);
ll.setLayoutParams(new CardView.LayoutParams(ll.getWidth(),newHeight));
}
給使用類似方法的任何人的注釋,如果您有大量項目,請確保設置recyclerView.setItemViewCacheSize(itemList.size),因為如果您不這樣做,則緩存的對象將被重用,從而調整多個項目的大小,而不僅僅是項目u點擊
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.