onClickListener用于popupWindow之外的触摸

时间:2017-01-06 06:46:57

标签: java android android-sdk-tools

从问题Issue dismissing popup window我学习了如何隐藏popupWindow(在此之前,以下代码中的popupWindow.dismiss();无效)。

private void initFAB(){
        fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {

        boolean showPopupWindow = false;

        public void onClick(View view) {

            LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
                    .getSystemService(LAYOUT_INFLATER_SERVICE);
            View popupWindowContent = layoutInflater.inflate(R.layout.popup_window_content, null);
            final PopupWindow popupWindow = new PopupWindow(popupWindowContent,
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            popupWindow.setOutsideTouchable(true);

            if (!showPopupWindow){
                rotateFabForward();

                View.OnClickListener onClickListener = new View.OnClickListener() {
                    public void onClick(View view) {
                        //...
                    }
                };

                // ...

                popupWindow.showAtLocation(fab, Gravity.END | Gravity.BOTTOM, 50, 400);
                showPopupWindow = true;
            }
            else{
                rotateFabBackward();
                popupWindow.dismiss();
                showPopupWindow = false;
            }
        }
    });
}

现在,当我在popupWindow外面点击它消失时,FloatingActionButton不会向后旋转(当+被隐藏并旋转时,它会有popupWindow图标当×可见时,degress和图标变为popupWindow

任何允许在popupWindow外注册触摸并执行rotateFabBackward()的侦听器?

P上。 S.另外,感谢关​​于FAB轮换的this问题的答案。

1 个答案:

答案 0 :(得分:1)

你应该像那样添加onDismiss监听器

popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
    @Override
    public void onDismiss() {
        rotateFabBackward();
        showPopupWindow = false;
        // end may TODO anything else                   
    }
});