2012-07-24 21 views
6

में पूर्ण दृश्य स्थिति प्राप्त करें मैं एक सूची दृश्य के अंदर क्लिक किए गए आइटम के ऊपर/नीचे एक पॉपअप विंडो दिखाई देने की कोशिश कर रहा हूं। हालांकि, समस्या यह है कि OnItemClick विधि से आने वाला दृश्य केवल मुझे सूची Xiew के सापेक्ष अपने एक्स & वाई मान दे रहा है। मैंने सूची दृश्य को भी चेक किया और यह मुझे इस तथ्य के बावजूद x = 0 y = 0 भी दे रहा है कि इसके ऊपर अन्य विचार भी हैं।ListView

मैं पदानुक्रम में सभी मूल्यों के माध्यम से भाग गया, लेकिन उन मूल्यों को नहीं देखा जिन्हें मैं ढूंढ रहा था। (और मुझे फिर से काम करने में बड़ी समस्याएं नहीं आ रही हैं)।

कोई सलाह?

@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]); 
} 

सूची में दोनों आइटम पॉपअप को उसी स्थान पर प्रदर्शित कर रहे हैं। Popup Not Appearing In desired positions

उत्तर

8

द्वारा उर y स्थिति यह काम करना चाहिए

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

स्थान सरणी में दृश्य के x और y मान होना चाहिए। 'v' दृश्य वस्तु है ITemClickListener पर पारित किया गया है।

मैं कुछ हिस्सों को जोड़ रहा हूं जो मैंने अपनी परियोजना के लिए उपयोग किया था। यह सहायक हो सकता है। मेरे पास सूचीदृश्य के शीर्ष पर एक एक्शनबार था और यह कोड ठीक काम करने लग रहा था।

आवश्यकता एक छोटा मेनू या तो सूची आइटम के ऊपर या नीचे लाने की आवश्यकता थी। तो जब कोई आइटम चुना जाता है, तो मैं जांचता हूं कि चयनित सूची आइटम स्क्रीन के ऊपरी हिस्से में है, यदि ऐसा है तो सूची आइटम के नीचे मेनू डालें अन्यथा इसे सूची आइटम के शीर्ष पर रखें। कोड यह

ListItem क्लिक कोड

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); 
    } 
} 

यह PopupHelper वर्ग मैं जो इस मुद्दे कोशिश उपयोग पर चिपके रहते हैं इस कोड स्निपेट के लिए

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

इसके लिए धन्यवाद, लेकिन मैंने कोशिश की और यह केवल मुझे सापेक्ष स्थिति देता है। तो पहला आइटम सूची 0,0 है। यह करीब है, क्योंकि मैंने सोचा था कि समाधान भी था। मेरा मानना ​​है कि मुझे कहीं कुछ अनुवाद समस्याएं आ रही हैं। –

+0

क्या आप अपना लेआउट एक्सएमएल डाल सकते हैं। मुझे थोड़ी आवश्यकता थी, लेकिन मेरे लेआउट में केवल एक सूचीदृश्य था। – blessenm

+0

मैंने अपना जवाब अपडेट कर दिया है। – blessenm

0

इस प्रयास करें और देखें अगर यह काम करता है ..

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(); 
    } 

}

यहाँ // listItem.getMeasuredHeight() यू प्रत्येक सूची आइटम की ऊंचाई मिल जाएगा ... यू प्राप्त कर सकते हैं ऊंचाई * clickedPosition

+0

प्रतिक्रिया के लिए धन्यवाद। मुझे सूचीदृश्य की ऊंचाई प्राप्त करने में कोई समस्या नहीं है। मैं एक दृश्य के स्क्रीन निर्देशांक प्राप्त करने की कोशिश कर रहा हूं ताकि मैं इसके द्वारा एक पॉपअपविंडो दिखा सकूं। –

1

का उपयोग

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

इस मदद की उम्मीद है।