如何为 ListItem 制作自定义上下文菜单(如 Baconreader)?
因此,我有兴趣在长按每个列表项时为它们创建自定义上下文菜单。我在 Baconreader 应用程序中看到了这一点,并认为它会很简单:
- 填充列表视图时创建一个 LinearLayout (或其他任何内容)
- 当长按某个项目时,隐藏该项目(View.GONE)并将 LinearLayout 添加到列表项的父项。
- 需要时,显示列表项并从其父项中删除 LinearLayout。
我成功地隐藏了 ItemLongClick 上的列表项,但事实证明您无法将视图添加到 ListView(哦)。但这就是 Baconreader 的做法。我想不通。这是我尝试过的一些代码:
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0,
View arg1, int arg2, long arg3) {
arg1.setVisibility(View.GONE); // hide the list item, works
// trying to add a TextView after a list
// item's position, doesn't work.
listView.addView(textView, arg2);
return true;
}
});
这是一个示例,它应该是什么样子:
所以列表项被隐藏,并且自定义上下文菜单(看起来像 ViewGroup)直接放置在列表项的位置上方。但如何呢?
编辑:已解决。更新后的代码:
list_item.xml
<TextView
android:id="@+id/list_item_title"
.....
/>
</FrameLayout>
Java 代码
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0,
View arg1, int arg2, long arg3) {
((FrameLayout)arg1).addView(w);
return true;
}
});
当然,您必须使用 SimpleAdapter 而不是 ArrayAdapter。
So I'm interested in creating a custom context menu for each of my list items when they are long clicked. I saw this implemented in the Baconreader app, and thought it would be as simple as:
- Create one LinearLayout (or whatsoever) when populating the listview
- When an item is long-clicked, hide the item (View.GONE) and add the LinearLayout to the list item's parent.
- When needed, show the list item and remove the LinearLayout from it's parent.
I successfully managed to hide the list items onItemLongClick, but it turns out that you can't add Views to a ListView (d'oh). But that's got to be the way Baconreader does it. I can't figure it out. Here's some code I tried:
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0,
View arg1, int arg2, long arg3) {
arg1.setVisibility(View.GONE); // hide the list item, works
// trying to add a TextView after a list
// item's position, doesn't work.
listView.addView(textView, arg2);
return true;
}
});
Here's a sample what it should look like:
So the List Item is hidden and a custom context menu (seems like a ViewGroup) is placed directly over the list item's position. But how?
Edit: Solved. The updated code:
list_item.xml
<TextView
android:id="@+id/list_item_title"
.....
/>
</FrameLayout>
The java code
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0,
View arg1, int arg2, long arg3) {
((FrameLayout)arg1).addView(w);
return true;
}
});
And of course you have to use SimpleAdapter instead of ArrayAdapter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只是一个快速提示:如果您不隐藏列表项并将上下文菜单添加到列表中,而是将上下文菜单添加到列表项中,该怎么办?
例如,将列表项的当前布局包装到 FrameLayout 中。那么当长按时,只需将上下文菜单添加到此 FrameLayout 即可? (如果需要,隐藏 FrameLayout 的第一个子级)。这还可以保证上下文菜单与列表项具有相同的大小。
Just a quick tip: what if you don't hide the list item and add the context menu to the list, but instead add the context menu to the list item?
For example, wrap the current layout of the list item into a FrameLayout. Then when long-clicked, just add the context menu to this FrameLayout instead? (and if needed hide the first child of the FrameLayout). This wouldd also guarantee that the context menu will have the same size as the list item.