我有几个使用FragmentStatePagerAdapter的页面,每个页面都包含GridView。
问题是当我尝试使用onContextItemSelected()中的适配器(例如myAdapter.remove(object))执行任何操作时-此操作是由不同页面中的适配器完成的。
我尝试使用getUserVisibleHint()没有成功。

我很困惑,因为当GridView中的项在方法onCreateContextMenu()中经过长时间爬升时,Gr​​idAdatper来自实际页面,而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;

    }


GridAdapter:

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)。
仅在可见片段上给出关于项目选择的指令,请通过-getUserVisibleHint()检查它,否则返回false。
不要每次都重新设置pagerAdapter。

10-08 18:25