获取绝对视图位置中的ListView [英] Get Absolute View Position in ListView

查看:222
本文介绍了获取绝对视图位置中的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让一个弹出窗口出现高于/低于被点击的ListView里面的一个项目。 然而,问题是,这是从OnItemClick法即将在视图只给了我它的X'放大器; Y值相对于ListView控件本身。我也查了ListView和那也给我X = 0 Y = 0尽管有上面的其他意见。

I am trying to make a popup window appear above/below an item that is clicked inside of a ListView. However, the problem is that the View that is coming in from the OnItemClick method is only giving me its X & Y values relative to the ListView itself. I also checked the ListView and that is also giving me x=0 y=0 despite the fact that there are other views above it.

我跑通过hierarchyviewer所有值,但没有看到我一直在寻找的值。 (而不是我在得到它重新工作的重大问题)。

I ran through all the values in hierarchyviewer, but didn't see the values I was looking for. (And not I'm having major problems getting it to work again).

有什么建议?

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

列表中的两个项目正在弹出出现在同一个地方。

Both items in the list are making the Popup appear in the same place.

推荐答案

这应该工作

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

的位置阵列应具有视图的x和y值。 V是对onItemClickListener通过视图对象。

The location array should have the x and y values of the view. 'v' is the view object passed on the onItemClickListener.

林添加一些地方我用我的项目。它可能会有所帮助。我对列表视图顶部的动作条,这code似乎正常工作。

Im adding some parts I used for my project. It might be helpful. I had an actionbar on the top of the listview and this code seemed to work fine.

要求是将一个小菜单无论是在顶部或下方的列表项。因此,当一个项目被选中,我检查,如果选中的列表项是在屏幕的上半部分,如果是的话就把下面的列表项的菜单,否则把它放在列表项的顶部。 这里的code

The requirement was to bring a small menu either on top or below a list item. So when an item is selected, I check if the selected list item is in the upper half of the screen, if so put the menu below the list item otherwise put it on top of the list item. Here's the code

列表项点击code

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类

This the PopupHelper class I use

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

}

这篇关于获取绝对视图位置中的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆