2017-10-14 7 views
1

RecycleViewコンテナ内にポップアップするカスタムポップアップメニューを使用しています。コンテナの外側にポップアップメニューを調整する方法

タッチしたビューが下部にある場合、ポップアップメニューはRecycleViewのコントロールを隠します。

この場合、ポップアップの位置を調整して、ポップアップを正確な距離に設定して、メニューがコンテナの中に入るようにします。

これは簡単ですが、私は座標を取得し、アイテムのクリックを扱うAdapterの内部から計算を適用するのが難しいです。ここで

は、私がこれまで管理するものである:

void show(FragmentActivity activity, View touchedView, DataItem item) { 

     LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     final View popupView = layoutInflater.inflate(R.layout.popup_menu, null); 

     PopupWindow popupWindow = new PopupWindow(
       popupView, 
       ViewGroup.LayoutParams.WRAP_CONTENT, 
       ViewGroup.LayoutParams.WRAP_CONTENT); 

     popupWindow.setOutsideTouchable(true); 

     bind(popupWindow, popupView, item); 

     //draws the menu perfectly bellow the touched element,but doesn't take in account the parent view area 
     popupWindow.showAsDropDown(touchView); 

    } 

答えて

0

見つけた。 2物事は途中であり、コメントで説明されています。

void show(FragmentActivity activity, View touchView, IDataItem dataItem) { 

     LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     final View popupView = layoutInflater.inflate(R.layout.popup_menu, null); 

     PopupWindow popupWindow = new PopupWindow(
       popupView, 
       ViewGroup.LayoutParams.WRAP_CONTENT, 
       ViewGroup.LayoutParams.WRAP_CONTENT); 

     popupWindow.setOutsideTouchable(true); 

     bind(popupWindow, popupView, dataItem); 

     //1º problem: View item is inside ViewHolder.Item, so container is 2 levels up and touched item one level up. 
     View container = (View) touchView.getParent().getParent(); 
     View item = (View) touchView.getParent(); 

     int containerHeight = container.getHeight(); 

     //2º problem: to get size before the popup view is displayed: ref:https://stackoverflow.com/a/15862282/2700303 
     popupView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), 
       View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); 

     int yOff=0; 

     //if popup view will draw pass the bottom, get the amount needed to adjust the y coordinate 
     if (ContainerHeight < item.getHeight() + item.getTop() + popupView.getMeasuredHeight()) 
      yOff = ContainerHeight- (item.getTop() + item.getHeight() + popupView.getMeasuredHeight()); 

     popupWindow.showAsDropDown(touchView,0,yOff); 

    } 
0

は、これは私がやったことです:

private void showPopupMenu(final View view, final int position) { 
    // inflate menu 
    final PopupMenu popup = new PopupMenu(view.getContext(), view); 
    popup.setGravity(Gravity.END); 
    MenuInflater inflater = popup.getMenuInflater(); 
    inflater.inflate(R.menu.popup_menu, popup.getMenu()); 
    popup.show(); 
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { 

    final EditText editText = (EditText) promptView.findViewById(R.id.input_text); 
        // setup a dialog window 
     alertDialogBuilder.setCancelable(false) 
     .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog, int id) { 

そして、ここでは結果である:

enter image description here

enter image description here

+0

申し訳ありませんが、質問はコンテナ内にメニューを保持する方法です。私はあなたがそれにどのように反応するかは分かりません。 – ByteArtisan

+0

ああスナップ、申し訳ありませんが、私は質問を正しく読まなかった。そのために残念.. –

関連する問題