0

PopupWindow A に GridView A があります。

そして、GridView A の 1 つの項目をクリックすると、PopupWindow A を閉じたいだけです。

結果の Fragment を開くと、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 に答える