1
所以我做了一个类扩展BaseAdaper,看起来像这样:如何显示上下文菜单网格布局中的Android
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;
}
}
在我的活动有一个网格布局和它的适配器设置为ProfileTileAdapter的实例。在我的活动中,我想打开一个上下文菜单,当用户长按其中一个视图(在这种情况下是一个ProfileTile),但我不知道如何。当用户在上下文菜单中选择一个选项时,我还需要找出ProfileTile长时间按下的内容。所有的教程都在活动中使用静态视图进行操作,但不是这样。