如何在 Android 中显示 GridLayout 的上下文菜单
因此,我创建了一个扩展 BaseAdaper 的类,如下所示:
public class ProfileTileAdapter extends BaseAdapter {
private Context context;
private ForwardingProfile[] profiles;
public ProfileTileAdapter(Context context, ForwardingProfile[] profiles) {
this.context = context;
this.profiles = profiles;
}
@Override
public int getCount() {
return profiles.length;
}
@Override
public Object getItem(int position) {
return profiles[position];
}
@Override
public long getItemId(int position) {
return profiles[position].getID();
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
ProfileTile tile = null;
if (convertView == null) {
tile = new ProfileTile(context, profiles[position]);
LayoutParams lp = new GridView.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
tile.setLayoutParams(lp);
} else {
tile = (ProfileTile) convertView;
}
return tile;
}
}
在我的活动中有一个 GridLayout 并将其适配器设置为 ProfileTileAdapter 的实例。在我的活动中,我想在用户长按其中一个视图(在本例中为 ProfileTile)时打开上下文菜单,但我不知道如何打开。我还需要找出当用户在上下文菜单中选择一个选项时,ProfileTile 被长按的内容。所有教程都在活动中使用静态视图进行操作,但不是这样。
So I made a class extend BaseAdaper that looks like this:
public class ProfileTileAdapter extends BaseAdapter {
private Context context;
private ForwardingProfile[] profiles;
public ProfileTileAdapter(Context context, ForwardingProfile[] profiles) {
this.context = context;
this.profiles = profiles;
}
@Override
public int getCount() {
return profiles.length;
}
@Override
public Object getItem(int position) {
return profiles[position];
}
@Override
public long getItemId(int position) {
return profiles[position].getID();
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
ProfileTile tile = null;
if (convertView == null) {
tile = new ProfileTile(context, profiles[position]);
LayoutParams lp = new GridView.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
tile.setLayoutParams(lp);
} else {
tile = (ProfileTile) convertView;
}
return tile;
}
}
In my activity a have a GridLayout and set its adapter to an instance of ProfileTileAdapter. In my activity I want to open a context menu when the user long presses on one of the views (in this case a ProfileTile) but I don't know how. I also need to find out what ProfileTile got long pressed when the user chooses an option in the context menu All the tutorials out there keep doing it with static views in the activity but not like this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以我最终找到了答案。显然,当您使用 Activity.registerForContextMenu(GridView) 为 Activity 中的上下文菜单注册 GridView 时,它会独立注册从适配器返回的每个视图。所以这就是 Activity 的样子(适配器保持不变):
}
所以解决方案很简单,但有时我们使事情变得过于复杂。
So I ended up figuring out the answer. So apparently when you register a GridView to for context menu in your Activity using
Activity.registerForContextMenu(GridView)
it registers each view you return from adapter independently. So this is how the Activity looks like (the adapter stays unchanged):}
So the solution was simple enough, but sometimes we over complicate things.