Why ListView expand/collapse animation appears much slower in DialogFragment than in Activity

前端 未结 1 2003
离开以前
离开以前 2021-01-06 13:38

Recently, I find out the ListView expand/collapse animation appears much slower in DialogFragment than in Activity.

In

1条回答
  •  天涯浪人
    2021-01-06 14:19

    I find out the key solution to this problem is, fix the dialog size always. Once we fix the size, no resource will be spent to perform size computation during animation.

        final ViewTreeObserver vto = view.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    
            @SuppressLint("NewApi")
            @SuppressWarnings("deprecation")
            @Override
            public void onGlobalLayout() {
                // Key area to perform optimization. Fix the dialog size!
                // During ListView's rows animation, dialog will never need to 
                // perform computation again.
                int width = dialog.getWindow().getDecorView().getWidth();
                int height = dialog.getWindow().getDecorView().getHeight();
                dialog.getWindow().setLayout(width, height);
    
                ViewTreeObserver obs = view.getViewTreeObserver();
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
                    obs.removeOnGlobalLayoutListener(this);
                } else {
                    obs.removeGlobalOnLayoutListener(this);
                }
            }
        });
    

    0 讨论(0)
提交回复
热议问题