我有几页使用FragmentStatePagerAdapter,每个页面都包含GridView。 问题是当我尝试在onContextItemSelected()中使用适配器(例如myAdapter.remove(object))执行任何操作时 - 此操作由来自不同页面的适配器完成。 我试过使用getUserVisibleHint()但没有成功。
我感到很困惑,因为GridView中的项目在方法onCreateContextMenu()中长时间被克隆,而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;
}
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];
}
}
答案 0 :(得分:2)
我发现了我的错误。
在onResume中设置新的pagerAdapter来刷新寻呼机标签顺序是一个丑陋的代码。
所以要使用上下文菜单和pagerView正确处理gridAdapter对象: