onContextItemSelected() 中错误的 gridview 变量
Wrong gridview variable in onContextItemSelected()
我有几个使用 FragmentStatePagerAdapter 的页面,每个页面都包含 GridView。
问题是当我尝试在 onContextItemSelected() 中使用适配器(例如 myAdapter.remove(object))执行任何操作时 - 此操作由来自不同页面的适配器完成。
我尝试使用 getUserVisibleHint() 但没有成功。
我很困惑,因为在方法 onCreateContextMenu() 中长按 GridView 中的项目时,gridAdatper 来自实际页面,但在 onContextItemSelected 中,gridAdapter 来自不同的页面。
预期行为:
长按后有膨胀的上下文菜单,而不是从菜单列表中选择项目(例如删除),适当的 gridAdapter(来自当前页面上的片段)处理此操作。
当前行为:
上下文菜单膨胀后,onContextItemSeleted 保留来自不同页面的 gridAdapter,因此当我尝试从第 2 页的网格中删除项目时,项目将从第 1 页删除。
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
//super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getActivity().getMenuInflater();
AdapterView.AdapterContextMenuInfo info;
try {
// Casts the incoming data object into the type for AdapterView objects.
info = (AdapterView.AdapterContextMenuInfo) menuInfo;
} catch (ClassCastException e) {
// If the menu object can't be cast, logs an error.
Log.e(getClass().getName(), "bad menuInfo", e);
return;
}
Log.d("ContextCreate", "grid adapter count " + gridAdapter.getCount() + " gridview count " + gridView.getCount());
Scene scene = gridAdapter.getItem(info.position);
if (scene == null) {
// For some reason the requested item isn't available, do nothing
return;
}
menu.setHeaderTitle(scene.getLabel());
inflater.inflate(R.menu.grid_scene_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if (getUserVisibleHint()) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int positionInGrid = info.position;
switch (item.getItemId()) {
case R.id.action_edit_scene:
// get item from adapter and pass it to another class
return true;
case R.id.action_delete:
gridAdapter.remove(gridAdapter.getItem(positionInGrid));
gridAdapter.notifyDataSetChanged();
return true;
default:
return super.onContextItemSelected(item);
}
}
else
return false;
}
网格适配器:
public class SceneGridArrayAdapter extends ArrayAdapter<Scene> {
List<Scene> sceneList;
private static final char[] alphabet= "ABCDEFGHIJKLMNOP".toCharArray();
Context context;
boolean[] isPressed;
public SceneGridArrayAdapter(Context context, List<Scene> scenes) {
super(context, 0, scenes);
this.context=context;
this.sceneList=scenes;
isPressed= new boolean[scenes.size()];
}
@Override
public void add(Scene sceneToAdd) {
DriversSingleton.getCurrentDriver().getConfiguration().addActiveScene(sceneToAdd);
super.add(sceneToAdd);
}
@Override
public void remove(Scene scene) {
int sceneIndex=scene.getIndex();
DriversSingleton.getCurrentDriver().getConfiguration().removeSceneAt(scene.getIndex());
super.remove(scene);
new CommunicationService(context).writeSceneAt(DriversSingleton.getCurrentDriver(), sceneIndex, 0xFF, 0xFF);
notifyDataSetChanged();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Scene scene= getItem(position);
if (convertView==null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.grid_layout, parent, false);
}
final ImageView imageBackground=(ImageView) convertView.findViewById(R.id.image_rect);
TextView sceneTv = (TextView) convertView.findViewById(R.id.scene_label);
TextView groupTv = (TextView) convertView.findViewById(R.id.group);
sceneTv.setText(scene.getLabel());
if(scene.getGroup()<16) {
groupTv.setVisibility(View.VISIBLE);
groupTv.setText("Group " + alphabet[scene.getGroup()]);
}
else
groupTv.setVisibility(View.INVISIBLE);
if(isPressed[position]==true){
imageBackground.setImageResource(R.drawable.scene_item_selected);
isPressed[position]=true;
}
else if(isPressed[position]==false){
imageBackground.setImageResource(R.drawable.scene_item);
isPressed[position]=false;
}
return convertView;
}
public void setSelection (int position)
{
if(isPressed[position]==false){
isPressed[position]=true;
}
else if(isPressed[position]==true){
isPressed[position]=false;
}
this.notifyDataSetChanged();
}
public boolean isSelected (int position)
{
return isPressed[position];
}
}
我找到了我的错误。
在 onResume 中实例化新的 pagerAdapter 以刷新分页器标签顺序是丑陋的代码。
因此要使用上下文菜单和 pagerView 正确处理 gridAdapter 对象:
- 设置适配器后调用registerForContextMenu(View v)。
- 仅对可见片段提供关于项目 select 的说明,通过 getUserVisibleHint() 检查它,否则 return false。
- 不要每次都恢复pagerAdapter。
我有几个使用 FragmentStatePagerAdapter 的页面,每个页面都包含 GridView。 问题是当我尝试在 onContextItemSelected() 中使用适配器(例如 myAdapter.remove(object))执行任何操作时 - 此操作由来自不同页面的适配器完成。 我尝试使用 getUserVisibleHint() 但没有成功。
我很困惑,因为在方法 onCreateContextMenu() 中长按 GridView 中的项目时,gridAdatper 来自实际页面,但在 onContextItemSelected 中,gridAdapter 来自不同的页面。
预期行为: 长按后有膨胀的上下文菜单,而不是从菜单列表中选择项目(例如删除),适当的 gridAdapter(来自当前页面上的片段)处理此操作。
当前行为: 上下文菜单膨胀后,onContextItemSeleted 保留来自不同页面的 gridAdapter,因此当我尝试从第 2 页的网格中删除项目时,项目将从第 1 页删除。
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
//super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getActivity().getMenuInflater();
AdapterView.AdapterContextMenuInfo info;
try {
// Casts the incoming data object into the type for AdapterView objects.
info = (AdapterView.AdapterContextMenuInfo) menuInfo;
} catch (ClassCastException e) {
// If the menu object can't be cast, logs an error.
Log.e(getClass().getName(), "bad menuInfo", e);
return;
}
Log.d("ContextCreate", "grid adapter count " + gridAdapter.getCount() + " gridview count " + gridView.getCount());
Scene scene = gridAdapter.getItem(info.position);
if (scene == null) {
// For some reason the requested item isn't available, do nothing
return;
}
menu.setHeaderTitle(scene.getLabel());
inflater.inflate(R.menu.grid_scene_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if (getUserVisibleHint()) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int positionInGrid = info.position;
switch (item.getItemId()) {
case R.id.action_edit_scene:
// get item from adapter and pass it to another class
return true;
case R.id.action_delete:
gridAdapter.remove(gridAdapter.getItem(positionInGrid));
gridAdapter.notifyDataSetChanged();
return true;
default:
return super.onContextItemSelected(item);
}
}
else
return false;
}
网格适配器:
public class SceneGridArrayAdapter extends ArrayAdapter<Scene> {
List<Scene> sceneList;
private static final char[] alphabet= "ABCDEFGHIJKLMNOP".toCharArray();
Context context;
boolean[] isPressed;
public SceneGridArrayAdapter(Context context, List<Scene> scenes) {
super(context, 0, scenes);
this.context=context;
this.sceneList=scenes;
isPressed= new boolean[scenes.size()];
}
@Override
public void add(Scene sceneToAdd) {
DriversSingleton.getCurrentDriver().getConfiguration().addActiveScene(sceneToAdd);
super.add(sceneToAdd);
}
@Override
public void remove(Scene scene) {
int sceneIndex=scene.getIndex();
DriversSingleton.getCurrentDriver().getConfiguration().removeSceneAt(scene.getIndex());
super.remove(scene);
new CommunicationService(context).writeSceneAt(DriversSingleton.getCurrentDriver(), sceneIndex, 0xFF, 0xFF);
notifyDataSetChanged();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Scene scene= getItem(position);
if (convertView==null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.grid_layout, parent, false);
}
final ImageView imageBackground=(ImageView) convertView.findViewById(R.id.image_rect);
TextView sceneTv = (TextView) convertView.findViewById(R.id.scene_label);
TextView groupTv = (TextView) convertView.findViewById(R.id.group);
sceneTv.setText(scene.getLabel());
if(scene.getGroup()<16) {
groupTv.setVisibility(View.VISIBLE);
groupTv.setText("Group " + alphabet[scene.getGroup()]);
}
else
groupTv.setVisibility(View.INVISIBLE);
if(isPressed[position]==true){
imageBackground.setImageResource(R.drawable.scene_item_selected);
isPressed[position]=true;
}
else if(isPressed[position]==false){
imageBackground.setImageResource(R.drawable.scene_item);
isPressed[position]=false;
}
return convertView;
}
public void setSelection (int position)
{
if(isPressed[position]==false){
isPressed[position]=true;
}
else if(isPressed[position]==true){
isPressed[position]=false;
}
this.notifyDataSetChanged();
}
public boolean isSelected (int position)
{
return isPressed[position];
}
}
我找到了我的错误。
在 onResume 中实例化新的 pagerAdapter 以刷新分页器标签顺序是丑陋的代码。
因此要使用上下文菜单和 pagerView 正确处理 gridAdapter 对象:
- 设置适配器后调用registerForContextMenu(View v)。
- 仅对可见片段提供关于项目 select 的说明,通过 getUserVisibleHint() 检查它,否则 return false。
- 不要每次都恢复pagerAdapter。