2012-07-24 17 views
6

Próbuję utworzyć okno wyskakujące powyżej/poniżej elementu klikniętego w widoku listy. Jednak problem polega na tym, że widok, który nadchodzi z metody OnItemClick, daje mi tylko X wartości X & względem samego ListView. Sprawdziłem również ListView i to również daje mi x = 0 y = 0 pomimo faktu, że są nad nim inne widoki.Uzyskaj pozycję widoku absolutnego na liście ViewView

Przebiegłem wszystkie wartości w widoku hierarchii, ale nie widziałem wartości, których szukałem. (I nie mam większych problemów z uruchomieniem go ponownie).

Każda rada?

@Override 
public void onListItemClick(ListView listView, View view, int position, long id) { 
    LayoutInflater inflater = getLayoutInflater(null); 
    PopupWindow quickRail = new PopupWindow(
      inflater.inflate(R.layout.quanitity_controls, null), view.getMeasuredWidth(), 
      view.getMeasuredHeight()); 

    int[] location = { 
      0, 0 
    }; 

    // This doesn't place this window right on top of the view 
    quickRail.showAtLocation(view, Gravity.CENTER, 0, location[1]); 
} 

Obie pozycje na liście powodują wyświetlanie wyskakującego okienka w tym samym miejscu. Popup Not Appearing In desired positions

Odpowiedz

8

ta powinna działać

//Activity windows height 
int totalHeight = getWindowManager().getDefaultDisplay().getHeight(); 
int[] location = new int[2]; 
v.getLocationOnScreen(location); 

Tablica lokalizacji powinna mieć wartości X i Y widoku. 'v' jest obiektem widoku przekazywanym na onItemClickListener.

Im dodaję części, których użyłem do mojego projektu. To może być pomocne. Miałem pasek akcji na górze listy i ten kod wydawał się działać dobrze.

Wymagane było przyniesienie małego menu na górze lub pod elementem listy. Po wybraniu elementu sprawdzam, czy wybrany element listy znajduje się w górnej połowie ekranu, jeśli tak, to ułóż menu poniżej pozycji na liście, inaczej ustaw na pozycji na liście. Oto kod

kod ListItem kliknięcie

listView.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> parent, View view, int position 
      , long id) { 
     showQuickActionMenu(position,view); 
    } 
}); 

private void showQuickActionMenu(int pos, View v){ 
    LayoutInflater inflater = 
      (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    //This is just a view with buttons that act as a menu. 
    View popupView = inflater.inflate(R.layout.ticket_list_menu, null); 
    popupView.findViewById(R.id.menu_view).setTag(pos); 
    popupView.findViewById(R.id.menu_change_status).setTag(pos); 
    popupView.findViewById(R.id.menu_add_note).setTag(pos); 
    popupView.findViewById(R.id.menu_add_attachment).setTag(pos); 

    window = PopupHelper.newBasicPopupWindow(TicketList.this); 
    window.setContentView(popupView); 
    int totalHeight = getWindowManager().getDefaultDisplay().getHeight(); 
    int[] location = new int[2]; 
    v.getLocationOnScreen(location); 

    if (location[1] < (totalHeight/2.0)) { 
     PopupHelper.showLikeQuickAction(window, popupView, v 
       , getWindowManager(),0,0,PopupHelper.UPPER_HALF); 
    } else { 
     PopupHelper.showLikeQuickAction(window, popupView, v 
       , getWindowManager(),0, 0,PopupHelper.LOWER_HALF); 
    } 
} 

ten Klasa PopupHelper używam

public class PopupHelper { 
    public static final int UPPER_HALF = 0; 
    public static final int LOWER_HALF = 1; 

    public static PopupWindow newBasicPopupWindow(Context context) { 
     final PopupWindow window = new PopupWindow(context); 

     // when a touch even happens outside of the window 
     // make the window go away 
     window.setTouchInterceptor(new OnTouchListener() { 
      public boolean onTouch(View v, MotionEvent event) { 
       if(event.getAction() == MotionEvent.ACTION_OUTSIDE) { 
        window.dismiss(); 
        return true; 
       } 
       return false; 
      } 
     }); 

     window.setWidth(WindowManager.LayoutParams.WRAP_CONTENT); 
     window.setHeight(WindowManager.LayoutParams.WRAP_CONTENT); 
     window.setTouchable(true); 
     window.setFocusable(true); 
     window.setOutsideTouchable(true); 

     window.setBackgroundDrawable(
       new ColorDrawable(android.R.color.darker_gray));   
     return window; 
    } 

    /** 
    * Displays like a QuickAction from the anchor view. 
    * 
    * @param xOffset 
    *   offset in the X direction 
    * @param yOffset 
    *   offset in the Y direction 
    */ 
    public static void showLikeQuickAction(PopupWindow window, View root, 
      View anchor, WindowManager windowManager, int xOffset 
      ,int yOffset,int section) { 

     //window.setAnimationStyle(R.style.Animations_GrowFromBottomRight); 

     int[] location = new int[2]; 
     anchor.getLocationOnScreen(location); 

     Rect anchorRect = new Rect(location[0], location[1], location[0] + 
       anchor.getWidth(), location[1] + anchor.getHeight()); 

     root.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 

     int rootWidth = root.getMeasuredWidth(); 
     int rootHeight = root.getMeasuredHeight(); 

     int screenWidth = windowManager.getDefaultDisplay().getWidth(); 
     int screenHeight = windowManager.getDefaultDisplay().getHeight(); 

     int xPos = ((screenWidth - rootWidth)/2) + xOffset; 
     int yPos = anchorRect.top - rootHeight + yOffset; 

     xPos = (screenWidth - rootWidth); 
     if(section == UPPER_HALF){ 
      yPos = anchorRect.top + anchor.getMeasuredHeight();  
     } else { 
      yPos = anchorRect.top - rootHeight; 
     } 
     window.showAtLocation(anchor, Gravity.NO_GRAVITY, xPos, yPos); 
    } 

} 
+0

Dzięki za to, ale spróbowałem tego i to tylko daje mi względną pozycję. Pierwszy element to lista 0,0. Jest blisko, ponieważ myślałem, że to również jest rozwiązanie. Wierzę, że mam gdzieś jakieś problemy z tłumaczeniem. –

+0

można umieścić w układzie ur xml. Miałem takie same wymagania, ale mój układ miał tylko listę. – blessenm

+0

Zaktualizowałem swoją odpowiedź. – blessenm

0

Spróbuj i zobacz czy działa ..

private void setListViewHeightBasedOnChildren(ListView listView) { 
    ListAdapter listAdapter = listView.getAdapter(); 
    if (listAdapter == null) { 
     // pre-condition 
     return; 
    } 
    int totalHeight = 0; 
    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST); 
    for (int i = 0; i < listAdapter.getCount(); i++) { 
     View listItem = listAdapter.getView(i, null, listView); 
     listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED); 
     totalHeight += listItem.getMeasuredHeight(); 
    } 

}

// tutaj listItem.getMeasuredHeight() dostanie u wysokość każdego elementu listy ... U można uzyskać ur pozycji Y przez wysokość * clickedPosition

+0

Dzięki za odpowiedź. Nie mam problemu z uzyskaniem wysokości listy. Próbuję uzyskać współrzędne ekranu dla widoku, aby można było wyświetlić okno PopupWindow tuż obok niego. –

1

dla tych, którzy trzymać w tej sprawie użytku wypróbować ten fragment kodu

popWindow.showAsDropDown(v);//v is your listview or recyclerview item Element that clicked. 

nadzieję, że ta pomoc.

Powiązane problemy