我试图让一个弹出窗口出现在ListView内部单击的项目上方/下方.
然而,问题是从OnItemClick方法进入的View只给了我X& Y值相对于ListView本身.我还检查了ListView,这也给了我x = 0 y = 0,尽管它上面还有其他视图.
然而,问题是从OnItemClick方法进入的View只给了我X& Y值相对于ListView本身.我还检查了ListView,这也给了我x = 0 y = 0,尽管它上面还有其他视图.
我遍历了hierarchyviewer中的所有值,但没有看到我正在寻找的值. (而不是我在重新开始工作时遇到重大问题).
有什么建议?
- @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,location[1]);
- }
列表中的两个项目都使Popup出现在同一个位置.
解决方法
这应该工作
- //Activity windows height
- int 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);
- }
- }