ListView中元素的精确y位置

时间:2017-12-27 10:10:32

标签: android listview

我的listView元素中的按钮上有一个onClickListener,需要在自己下面显示一个弹出窗口。

我尝试实现它的方法是在onClickListener中简单地使用v.getBottom()。但是,这不会返回单击按钮的正确位置。

然后我尝试使用此答案计算listItem的yPosition以获得确切的滚动位置:https://stackoverflow.com/a/10808454/3963566

View c = listView.getChildAt(0);
int yPosition = (int) listView.getChildAt(position).getBottom() - (-c.getTop() + listView.getFirstVisiblePosition() * c.getHeight());
popupWindow.showAtLocation(listView, Gravity.CENTER, 0, yPosition);

适配器代码:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
    rowView = inflater.inflate(R.layout.search_result, parent, false);
    listView = (ListView) parent;
    mainContainer = (LinearLayout) parent.getParent().getParent().getParent();

    ibEmployment = (SquareImageButton) rowView.findViewById(R.id.ib_employment);
    ibEmployment.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showEmploymentPopup(v, position);
        }
    });
}

private void showEmploymentPopup(View v, int position) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.popup_categories,null);
    PopupWindow pw = new PopupWindow(layout, 400, 600, true);
    // display the popup in the center
    pw.showAtLocation(v, Gravity.TOP, 0, v.getTop() + v.getMeasuredHeight() + 300);
    initializeCategoriesList(position);
    setCertificateStatus(position);
    pw.showAsDropDown(v);
}

但是只要列表没有滚动,yPosition只对前两个列表项准确。

2 个答案:

答案 0 :(得分:2)

使用Listview项目视图尝试此操作。

创建在adaper类中显示popupwindow的方法。

 private void initiatePopupWindow(View v) {
    try {
        //We need to get the instance of the LayoutInflater, use the context of this activity
        LayoutInflater inflater = (LayoutInflater) ctx
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //Inflate the view from a predefined XML layout
        View layout = inflater.inflate(R.layout.popup,null);
        // create a 300px width and 470px height PopupWindow
        pw = new PopupWindow(layout, 400, 600, true);
        // display the popup in the center
        pw.showAtLocation(v, Gravity.TOP, 0, v.getTop() + v.getMeasuredHeight() + 300); //300 is haif of popwindow height to place popupwindow top line below item bottomline.
        TextView mResultText = (TextView) layout.findViewById(R.id.result_txt);
        Button cancelButton = (Button) layout.findViewById(R.id.btn_cancel);
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pw.dismiss();
            }
        });

    } catch (Exception e) {
        e.printStackTrace();
    }
}

添加点击事件以在列表视图中的项目中查看。

 textView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initiatePopupWindow(itemView);//itemview is your entire row view of listview.
            }

<强>输出

enter image description here

希望它有所帮助。!

答案 1 :(得分:0)

listView.getChildAt(position)可能是错误的,因为您正在尝试访问适配器位置,而listview的子视图数可能小于适配器数。
我想你可能想尝试更简单的方法:

    yourBtn.setOnClickListener(new View.OnClickListener() {    
        @Override    
        public void onClick(View v) {
            int positions[] = new int[2];
            v.getLocationOnScreen(positions);
            // Use positions for calculation
            // ...

        }
    });
相关问题