如何在Android中显示GridLayout的上下文菜单(How to show up context menus for GridLayout in Android)

编程入门 行业动态 更新时间:2024-10-23 06:24:23
如何在Android中显示GridLayout的上下文菜单(How to show up context menus for GridLayout in Android)

所以我做了一个扩展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.

最满意答案

所以我最终搞清楚了答案。 显然,当您使用Activity.registerForContextMenu(GridView)在Activity中注册GridView以获取上下文菜单时,它会独立地注册您从适配器返回的每个视图。 所以这就是Activity的样子(适配器保持不变):

public class SMSForwarderActivity extends Activity { private GridView profilesGridView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.main); setUpProfilesGrid(); } private void setUpProfilesGrid() { profilesGridView = (GridView) this.findViewById(R.id.profilesGrid); this.registerForContextMenu(profilesGridView); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); AdapterContextMenuInfo aMenuInfo = (AdapterContextMenuInfo) menuInfo; ProfileTile tile = (ProfileTile) aMenuInfo.targetView;//This is how I get a grip on the view that got long pressed. } @Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item .getMenuInfo(); ProfileTile tile = (ProfileTile) info.targetView;//Here we get a grip again of the view that opened the Context Menu return super.onContextItemSelected(item); }

}

所以解决方案很简单,但有时我们会使事情变得复杂。

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):

public class SMSForwarderActivity extends Activity { private GridView profilesGridView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.main); setUpProfilesGrid(); } private void setUpProfilesGrid() { profilesGridView = (GridView) this.findViewById(R.id.profilesGrid); this.registerForContextMenu(profilesGridView); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); AdapterContextMenuInfo aMenuInfo = (AdapterContextMenuInfo) menuInfo; ProfileTile tile = (ProfileTile) aMenuInfo.targetView;//This is how I get a grip on the view that got long pressed. } @Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item .getMenuInfo(); ProfileTile tile = (ProfileTile) info.targetView;//Here we get a grip again of the view that opened the Context Menu return super.onContextItemSelected(item); }

}

So the solution was simple enough, but sometimes we over complicate things.

更多推荐

本文发布于:2023-08-05 20:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1437846.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上下文   菜单   如何在   Android   menus

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!