简体   繁体   English

如何在 Android 中显示 GridLayout 的上下文菜单

[英]How to show up context menus for GridLayout in Android

So I made a class extend BaseAdaper that looks like this:所以我做了一个 class 扩展 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;
}

} }

In my activity a have a GridLayout and set its adapter to an instance of ProfileTileAdapter.在我的活动中,有一个 GridLayout 并将其适配器设置为 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.在我的活动中,当用户长按其中一个视图(在本例中为 ProfileTile)时,我想打开一个上下文菜单,但我不知道如何操作。 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.我还需要找出当用户在上下文菜单中选择一个选项时 ProfileTile 被长按的内容所有的教程都在活动中使用 static 视图进行操作,但不是这样。

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.因此,显然,当您使用Activity.registerForContextMenu(GridView)将 GridView 注册到 Activity 中的上下文菜单时,它会独立注册您从适配器返回的每个视图。 So this is how the Activity looks like (the adapter stays unchanged):所以这就是 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 the solution was simple enough, but sometimes we over complicate things.所以解决方案很简单,但有时我们把事情复杂化了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM