在Popupwindow布局中嵌套ScrollView,滑动内容时视图出现反复闪烁的问题

解决办法:

将显示PopupWindow的方法由showAsDropDown()改为showAtLocation()

代码如下

private PopupWindow popupWindow_scrollview;
private void showPopupwindow(String message) {
        View contentView = View.inflate(context,R.layout.dialog_content_view, null);
        DialogViewHolder viewHolder = new DialogViewHolder(contentView);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
        viewHolder.tvGameMessageDialogContent.setText(Html.fromHtml(message));
        } else {
            viewHolder.tvGameMessageDialogContent.setText(Html.fromHtml(message, Html.FROM_HTML_MODE_COMPACT));
        }
        viewHolder.ivGameMessageDialogClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popupWindow_scrollview.isShowing()) {
                    popupWindow_scrollview.setFocusable(false);
                    popupWindow_scrollview.dismiss();
                }
            }
        });
        popupWindow_scrollview = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT,true);
        popupWindow_scrollview.getContentView().setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                popupWindow_scrollview.setFocusable(false);
                return true;
            }
        });
        if (!popupWindow_scrollview.isShowing() && !mActivity.isFinishing()) {
            //以下三种方法内容在滑动时PopupWindow均会出现反复闪烁的问题
            //popupWindow_list.showAsDropDown(contentView);
            //popupWindow_list.showAsDropDown(contentView, 0, 0);
            //popupWindow_list.showAsDropDown(contentView, 0, 0,Gravity.CENTER);

            //经过测试改为以下方法后滑动正常
            popupWindow_scrollview.showAtLocation(contentView, Gravity.CENTER, 0, 0);
        }
    }

你可能感兴趣的:(android)