0

我在 PopupWindow A 中有一个 GridView A。

如果我单击 GridView A 中的一项,我只想关闭 PopupWindow A。

现在就像当我打开结果片段时,PopupWindow A 一直停留在屏幕上。

而且 PopupWindow A 也是从 GridView B 中的项目弹出的。

所以我已经实现AdapterView.OnItemClickListener 了 GridView B。

GridViewImplement.java:
public AdapterView.OnItemClickListener mOnItemClickListener = new AdapterView.OnItemClickListener(){
   @Override
   public void onItemClick(AdapterView<?> adapterView, View view, int i, long id){

      View popupView = activity.getLayoutInflater().inflate(R.layout.popup, null);
      final PopupWindow mPopupWindow = new PopupWindow(popupView, 800, 500, true); 

      if(id != 0){

        GridView gridViewA = (GridView)popupView.findViewById(R.id.gridview_a_popup);
        GridViewAdapter gridViewAdapter = new GridViewAdapter(activity, list, true);
        gridViewA.setAdapter(gridViewAdapter);

        GridViewImplement impl =  new GridViewImplement(activity).
        gridView.setOnItemClickListener(impl.mOnItemClickListener);

        Button closeButton = (Button) popupView.findViewById(R.id.btn_popup_close);
                if (closeButton != null) {
                    closeButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mPopupWindow.dismiss();// here is works.
                        }
                    });
                }

      }else{
        mPopupWindow.dismiss();// here not work.

        Bundle bundle = new Bundle();
        activity.replaceFragment(ResultFragment.newInstance(),bundle,R.id.fragment_result);

      }
   }
}

修改代码

GridViewImplement.java:

PopupWindow mPopupWindow;

public AdapterView.OnItemClickListener mOnItemClickListener = new AdapterView.OnItemClickListener(){
   @Override
   public void onItemClick(AdapterView<?> adapterView, View view, int i, long id){

      View popupView = activity.getLayoutInflater().inflate(R.layout.popup, null);


      if(id != 0){
        mPopupWindow = new PopupWindow(popupView, 800, 500, true);
        GridView gridViewA = (GridView)popupView.findViewById(R.id.gridview_a_popup);
        GridViewAdapter gridViewAdapter = new GridViewAdapter(activity, list, true);
        gridViewA.setAdapter(gridViewAdapter);

        GridViewImplement impl =  new GridViewImplement(activity).
        gridView.setOnItemClickListener(impl.mOnItemClickListener);

        Button closeButton = (Button) popupView.findViewById(R.id.btn_popup_close);
                if (closeButton != null) {
                    closeButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mPopupWindow.dismiss();// here will warning need key 'final'.
                        }
                    });
                }

      }else{
        mPopupWindow.dismiss();// here not work.

        Bundle bundle = new Bundle();
        activity.replaceFragment(ResultFragment.newInstance(),bundle,R.id.fragment_result);

      }
   }
}

作品代码来自我的朋友

GridViewImplement.java:
public AdapterView.OnItemClickListener mOnItemClickListener = new AdapterView.OnItemClickListener(){
   @Override
   public void onItemClick(AdapterView<?> adapterView, View view, int i, long id){

      View popupView = activity.getLayoutInflater().inflate(R.layout.popup, null);
      final PopupWindow mPopupWindow = new PopupWindow(popupView, 800, 500, true);

      if(id != 0){
        GridView gridViewA = (GridView)popupView.findViewById(R.id.gridview_a_popup);
        GridViewAdapter gridViewAdapter = new GridViewAdapter(activity, list, true);
        gridViewA.setAdapter(gridViewAdapter);

        //GridViewImplement impl =  new GridViewImplement(activity).
        //gridView.setOnItemClickListener(impl.mOnItemClickListener);// here just use a total new implement. 
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        mPopupWindow.dismiss();// works here.
                        Bundle bundle = new Bundle();
                        activity.replaceFragment(ResultFragment.newInstance(),bundle,R.id.fragment_result);

                    }
                });

        Button closeButton = (Button) popupView.findViewById(R.id.btn_popup_close);
                if (closeButton != null) {
                    closeButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mPopupWindow.dismiss();// works here
                        }
                    });
                }

      }else{
        //mPopupWindow.dismiss(); // here looks like not needed any more.

        Bundle bundle = new Bundle();
        activity.replaceFragment(ResultFragment.newInstance(),bundle,R.id.fragment_result);

      }
   }
}
4

1 回答 1

0

您可以为 GridView 设置 OnClickListener 并添加 popupWindow.dismiss() 以关闭弹出窗口。基本上做这样的事情:

gridView.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) {
    popupWindow.dismiss();
  }
}
于 2014-04-21T08:22:58.923 回答