簡體   English   中英

Android RecyclerView平滑滾動以查看其動畫高度

[英]Android RecyclerView smooth scroll to view that's animating their height

我有一個帶有可擴展子視圖的RecyclerView,當單擊子ViewGroup時,它會將ViewGroup高度從0調整為測量視圖組高度的視圖數量膨脹,如下面的gif所示:

例

問題是:我在recyclerView上調用smoothScrollToPosition,它平滑滾動到視圖位置,但是在上面的gif中我認為當前的視圖高度仍然沒有展開,我正在回收在recyclerview的視圖下面,由於視圖已經可見,因此不會滾動到位置,但是當我再次觸摸(再次調用smoothscrolltoposition)時,它會將視圖滾動到正確的位置,因為視圖已經展開。

有沒有辦法將視圖滾動到屏幕頂部或只是滾動以使內容可見?

對於引用:這是調用以擴展視圖的方法:

collapsible_content.removeAllViews();
for(int i = 0; i < 5; i++) {
        View link_view = getLayoutInflater().inflate(R.layout.list_item_timeline_step_link, collapsible_content, false);
        TextView text = (TextView) link_view.findViewById(R.id.step_link_text);
        text.setText("Test");
        collapsible_content.addView(link_view);
    }

這是我擴展的方法:

public void toggle() {
        collapsible_content.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        Animation a;
        if (mExpanded) {
            a = new ExpandAnimation(collapsible_content.getLayoutParams().height, 0);
        } else {
            a = new ExpandAnimation(collapsible_content.getLayoutParams().height, getMeasuredHeight());
        }
        a.setDuration(mAnimationDuration);
        collapsible_content.startAnimation(a);
        mExpanded = !mExpanded;
    }

和動畫:

private class ExpandAnimation extends Animation {
        private final int mStartHeight;
        private final int mDeltaHeight;

        public ExpandAnimation(int startHeight, int endHeight) {
            mStartHeight = startHeight;
            mDeltaHeight = endHeight - startHeight;
        }

        @Override
        protected void applyTransformation(float interpolatedTime,
                                           Transformation t) {
            final int newHeight = (int) (mStartHeight + mDeltaHeight *
                    interpolatedTime);
            collapsible_content getLayoutParams().height = newHeight;

            if (newHeight <= 0) {
                collapsible_content setVisibility(View.GONE);
            } else {
                collapsible_content setVisibility(View.VISIBLE);
            }
            collapsible_content requestLayout();
        }


        @Override
        public boolean willChangeBounds() {
            return true;
        }
    }

我的解決方案是在applyTransformation方法中不斷檢查視圖底部,並將其與RecyclerView高度進行比較,如果底部高於RV高度,我滾動diff值:

final int bottom = collapsible_content.getBottom();
final int listViewHeight = mRecyclerView.getHeight();
if (bottom > listViewHeight) {
    final int top = collapsible_content.getTop();
    if (top > 0) {
        mRecyclerView.smoothScrollBy(0, Math.min(bottom - listViewHeight + mRecyclerView.getPaddingBottom(), top));
    }
}

訣竅是使用Math.min來獲取頂部視圖,因此它不會向上滾動使頂部不可見。

基於ListViewAnimations的解決方案

添加animationlistener並在展開動畫完成后開始滾動recyclelerview。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM