然而,问题是从OnItemClick方法进入的VIEw只给了我X& Y值相对于ListVIEw本身.我还检查了ListVIEw,这也给了我x = 0 y = 0,尽管它上面还有其他视图.
我遍历了hIErarchyvIEwer中的所有值,但没有看到我正在寻找的值. (而不是我在重新开始工作时遇到重大问题).
有什么建议?
@OverrIDepublic 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,location[1]);}
列表中的两个项目都使Popup出现在同一个位置.
解决方法 这应该工作//Activity windows heightint totalHeight = getwindowManager().getDefaultdisplay().getHeight();int[] location = new int[2];v.getLocationOnScreen(location);
位置数组应具有视图的x和y值.
‘v’是onItemClickListener上传递的视图对象.
我添加了一些我用于项目的部分.它可能会有所帮助.我在ListvIEw的顶部有一个 *** 作栏,这段代码似乎运行正常.
要求是在列表项的顶部或下方放置一个小菜单.因此,当选择一个项目时,我检查所选列表项是否在屏幕的上半部分,如果是这样,则将菜单放在列表项目下面,否则将其放在列表项目的顶部.
这是代码
ListItem单击代码
ListVIEw.setonItemClickListener(new OnItemClickListener() { public voID onItemClick(AdapterVIEw<?> parent,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(),PopupHelper.UPPER_HALF); } else { PopupHelper.showlikequickaction(window,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); }}总结
以上是内存溢出为你收集整理的android – 在ListView中获取绝对视图位置全部内容,希望文章能够帮你解决android – 在ListView中获取绝对视图位置所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)