android – 在ListView中获取绝对视图位置

前端之家收集整理的这篇文章主要介绍了android – 在ListView中获取绝对视图位置前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图让一个弹出窗口出现在ListView内部单击的项目上方/下方.
然而,问题是从OnItemClick方法进入的View只给了我X& Y值相对于ListView本身.我还检查了ListView,这也给了我x = 0 y = 0,尽管它上面还有其他视图.

我遍历了hierarchyviewer中的所有值,但没有看到我正在寻找的值. (而不是我在重新开始工作时遇到重大问题).

有什么建议?

  1. @Override
  2. public void onListItemClick(ListView listView,View view,int position,long id) {
  3. LayoutInflater inflater = getLayoutInflater(null);
  4. PopupWindow quickRail = new PopupWindow(
  5. inflater.inflate(R.layout.quanitity_controls,null),view.getMeasuredWidth(),view.getMeasuredHeight());
  6.  
  7. int[] location = {
  8. 0,0
  9. };
  10.  
  11. // This doesn't place this window right on top of the view
  12. quickRail.showAtLocation(view,Gravity.CENTER,location[1]);
  13. }

列表中的两个项目都使Popup出现在同一个位置.

解决方法

这应该工作
  1. //Activity windows height
  2. int totalHeight = getWindowManager().getDefaultDisplay().getHeight();
  3. int[] location = new int[2];
  4. v.getLocationOnScreen(location);

位置数组应具有视图的x和y值.
‘v’是onItemClickListener上传递的视图对象.

添加了一些我用于项目的部分.它可能会有所帮助.我在listview的顶部有一个操作栏,这段代码似乎运行正常.

要求是在列表项的顶部或下方放置一个小菜单.因此,当选择一个项目时,我检查所选列表项是否在屏幕的上半部分,如果是这样,则将菜单放在列表项目下面,否则将其放在列表项目的顶部.
这是代码

ListItem单击代码

  1. listView.setOnItemClickListener(new OnItemClickListener() {
  2. public void onItemClick(AdapterView<?> parent,long id) {
  3. showQuickActionMenu(position,view);
  4. }
  5. });
  6.  
  7. private void showQuickActionMenu(int pos,View v){
  8. LayoutInflater inflater =
  9. (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  10.  
  11. //This is just a view with buttons that act as a menu.
  12. View popupView = inflater.inflate(R.layout.ticket_list_menu,null);
  13. popupView.findViewById(R.id.menu_view).setTag(pos);
  14. popupView.findViewById(R.id.menu_change_status).setTag(pos);
  15. popupView.findViewById(R.id.menu_add_note).setTag(pos);
  16. popupView.findViewById(R.id.menu_add_attachment).setTag(pos);
  17.  
  18. window = PopupHelper.newBasicPopupWindow(TicketList.this);
  19. window.setContentView(popupView);
  20. int totalHeight = getWindowManager().getDefaultDisplay().getHeight();
  21. int[] location = new int[2];
  22. v.getLocationOnScreen(location);
  23.  
  24. if (location[1] < (totalHeight / 2.0)) {
  25. PopupHelper.showLikeQuickAction(window,popupView,v,getWindowManager(),PopupHelper.UPPER_HALF);
  26. } else {
  27. PopupHelper.showLikeQuickAction(window,PopupHelper.LOWER_HALF);
  28. }
  29. }

这是我使用的PopupHelper类

  1. public class PopupHelper {
  2. public static final int UPPER_HALF = 0;
  3. public static final int LOWER_HALF = 1;
  4.  
  5. public static PopupWindow newBasicPopupWindow(Context context) {
  6. final PopupWindow window = new PopupWindow(context);
  7.  
  8. // when a touch even happens outside of the window
  9. // make the window go away
  10. window.setTouchInterceptor(new OnTouchListener() {
  11. public boolean onTouch(View v,MotionEvent event) {
  12. if(event.getAction() == MotionEvent.ACTION_OUTSIDE) {
  13. window.dismiss();
  14. return true;
  15. }
  16. return false;
  17. }
  18. });
  19.  
  20. window.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
  21. window.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
  22. window.setTouchable(true);
  23. window.setFocusable(true);
  24. window.setOutsideTouchable(true);
  25.  
  26. window.setBackgroundDrawable(
  27. new ColorDrawable(android.R.color.darker_gray));
  28. return window;
  29. }
  30.  
  31. /**
  32. * Displays like a QuickAction from the anchor view.
  33. *
  34. * @param xOffset
  35. * offset in the X direction
  36. * @param yOffset
  37. * offset in the Y direction
  38. */
  39. public static void showLikeQuickAction(PopupWindow window,View root,View anchor,WindowManager windowManager,int xOffset,int yOffset,int section) {
  40.  
  41. //window.setAnimationStyle(R.style.Animations_GrowFromBottomRight);
  42.  
  43. int[] location = new int[2];
  44. anchor.getLocationOnScreen(location);
  45.  
  46. Rect anchorRect = new Rect(location[0],location[1],location[0] +
  47. anchor.getWidth(),location[1] + anchor.getHeight());
  48.  
  49. root.measure(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
  50.  
  51. int rootWidth = root.getMeasuredWidth();
  52. int rootHeight = root.getMeasuredHeight();
  53.  
  54. int screenWidth = windowManager.getDefaultDisplay().getWidth();
  55. int screenHeight = windowManager.getDefaultDisplay().getHeight();
  56.  
  57. int xPos = ((screenWidth - rootWidth) / 2) + xOffset;
  58. int yPos = anchorRect.top - rootHeight + yOffset;
  59.  
  60. xPos = (screenWidth - rootWidth);
  61. if(section == UPPER_HALF){
  62. yPos = anchorRect.top + anchor.getMeasuredHeight();
  63. } else {
  64. yPos = anchorRect.top - rootHeight;
  65. }
  66. window.showAtLocation(anchor,Gravity.NO_GRAVITY,xPos,yPos);
  67. }
  68.  
  69. }

猜你在找的Android相关文章