Android ListView中的onItemClick事件在特定位置显示弹出窗口。

huangapple go评论73阅读模式
英文:

Android ListView onItemClick show Popup on exact position

问题

我有一个PopupWindow,当我点击ListView上的一个Item时,它会显示为一个下拉菜单:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    View v = getLayoutInflater().inflate(R.layout.popup_click_menu, null);
    final PopupWindow mypopupWindow = new PopupWindow(v, 300, RelativeLayout.LayoutParams.WRAP_CONTENT, true);
    mypopupWindow.showAsDropDown(view, -153, 0);
}

结果如下:

Android ListView中的onItemClick事件在特定位置显示弹出窗口。

我的问题是:我想让弹出窗口的位置是动态的。因此,如果我点击Item的中间部分,它应该显示在中间。我该如何做到这一点?

英文:

I have a PopupWindow which is shown as a drop down when I click a Item on my ListView:

@Override
        public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
            View v = getLayoutInflater().inflate(R.layout.popup_click_menu, null);
            final PopupWindow mypopupWindow = new PopupWindow(v,300, RelativeLayout.LayoutParams.WRAP_CONTENT, true);
            mypopupWindow.showAsDropDown(view, -153, 0);
        }

The result is this:

Android ListView中的onItemClick事件在特定位置显示弹出窗口。

My question is: I want to have a dynamic position of my pop up. So if I click on the middle of the Item then it should be shown in the middle. How can I do that?

答案1

得分: 0

你可以通过使用 TouchListener 来解决这个问题:

list.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View view, MotionEvent event) {
        positionX = (int) event.getX();

        return false; // 未消耗,传递给 onClick
    }
});

然后你可以在 onItemClick 中使用得到的准确的 x 位置来移动 PopupWindow

mypopupWindow.showAsDropDown(view, positionX, 0);
英文:

You can solve this with a TouchListener:

list.setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(View view, MotionEvent event) {
                positionX = (int) event.getX();

                return false; // not consumed; forward to onClick
            }
        });

Then you have the exact x-position and you can move the PopupWindow by onItemClick:

mypopupWindow.showAsDropDown(view, positionX, 0);

huangapple
  • 本文由 发表于 2020年9月14日 18:02:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63882138.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定