英文:
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);
}
结果如下:
我的问题是:我想让弹出窗口的位置是动态的。因此,如果我点击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<?> 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:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论