2017-12-27 12 views
0

내 listView 요소 안에있는 버튼에 onClickListener가 있습니다.이 요소는 자체에서 팝업을 표시해야합니다.ListView에서 요소의 정확한 y 위치

나는 그것을 달성하기 위해 노력한 방법은 단순히 v.getBottom()을 onClickListener 안에 사용하는 것이 었습니다. 그러나 이렇게하면 클릭 한 버튼의 정확한 위치가 반환되지 않습니다. https://stackoverflow.com/a/10808454/3963566

View c = listView.getChildAt(0); 
int yPosition = (int) listView.getChildAt(position).getBottom() - (-c.getTop() + listView.getFirstVisiblePosition() * c.getHeight()); 
popupWindow.showAtLocation(listView, Gravity.CENTER, 0, yPosition); 

어댑터 코드 :

난 후 정확한 스크롤 위치를 얻기 위해이 답변을 사용되는 listItem의 yPosition을 계산하여 위치를 얻기 위해 노력

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE); 
    rowView = inflater.inflate(R.layout.search_result, parent, false); 
    listView = (ListView) parent; 
    mainContainer = (LinearLayout) parent.getParent().getParent().getParent(); 

    ibEmployment = (SquareImageButton) rowView.findViewById(R.id.ib_employment); 
    ibEmployment.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      showEmploymentPopup(v, position); 
     } 
    }); 
} 

private void showEmploymentPopup(View v, int position) { 
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View layout = inflater.inflate(R.layout.popup_categories,null); 
    PopupWindow pw = new PopupWindow(layout, 400, 600, true); 
    // display the popup in the center 
    pw.showAtLocation(v, Gravity.TOP, 0, v.getTop() + v.getMeasuredHeight() + 300); 
    initializeCategoriesList(position); 
    setCertificateStatus(position); 
    pw.showAsDropDown(v); 
} 

을하지만 yPosition입니다 목록이 스크롤되지 않는 한 처음 두 목록 항목에 대해서만 정확합니다.

+0

showAtLocation 메서드 내에서 listview 대신 childview를 전달하여 팝업을 표시하려고 했습니까? –

+0

@MohamedMohaideenAH 예. 팝업은 항상 listView – Max90

+0

의 맨 위에 표시됩니다. 내 대답을 확인하십시오. –

답변

2

이 목록보기 itemview 이것을보십시오.

adaper 클래스에 popupwindow를 표시하는 메소드를 만듭니다.

private void initiatePopupWindow(View v) { 
    try { 
     //We need to get the instance of the LayoutInflater, use the context of this activity 
     LayoutInflater inflater = (LayoutInflater) ctx 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     //Inflate the view from a predefined XML layout 
     View layout = inflater.inflate(R.layout.popup,null); 
     // create a 300px width and 470px height PopupWindow 
     pw = new PopupWindow(layout, 400, 600, true); 
     // display the popup in the center 
     pw.showAtLocation(v, Gravity.TOP, 0, v.getTop() + v.getMeasuredHeight() + 300); //300 is haif of popwindow height to place popupwindow top line below item bottomline. 
     TextView mResultText = (TextView) layout.findViewById(R.id.result_txt); 
     Button cancelButton = (Button) layout.findViewById(R.id.btn_cancel); 
     cancelButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       pw.dismiss(); 
      } 
     }); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

목록보기에 항목을 보려면 클릭 이벤트를 추가하십시오.

textView1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       initiatePopupWindow(itemView);//itemview is your entire row view of listview. 
      } 

출력

enter image description here

는 도움이되기를 바랍니다.!

+0

불행히도 이것도 효과가 없습니다. 팝업이 목록 상단에 표시됩니다. – Max90

+0

인수에 itemview를 전달 했습니까? –

+0

네,하지만 작동하지 않았습니다 – Max90

0

listView.getChildAt(position)은 목록보기의 하위보기 수가 어댑터 수보다 적지 만 어댑터 위치에 액세스하려고하기 때문에 잘못되었을 수 있습니다.
난 당신이 간단한 방법을 시도 할 수 있습니다 생각 :

yourBtn.setOnClickListener(new View.OnClickListener() {  
     @Override  
     public void onClick(View v) { 
      int positions[] = new int[2]; 
      v.getLocationOnScreen(positions); 
      // Use positions for calculation 
      // ... 

     } 
    });