2017-10-14 24 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

糟糕!對不起,我沒有正確讀取的問題。對不起,這.. –