onItemCheckStateChanged 中的 setBackgroundResource 不适用于第一次选择
setBackgroundResource in onItemCheckStateChanged doesn't work for the first selection
我正在开发一个 android 项目,其中有一个网格视图,我正在使用 MultiChoiceModeListener 对其进行设置。
当我长按时,它成功创建了上下文操作栏,但是在创建 CAB 的初始项目上设置 setBackgroundResource
没有任何影响,但是所有后续项目被选中,背景资源申请成功。下面是我使用的代码:
gridView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);
gridView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener()
{
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked)
{
Log.d("DirectoryPicker-CAB", "CheckedStateChanged");
//findViewById((int)gridView.getItemIdAtPosition(position)).setBackgroundColor(ContextCompat.getColor(DirectoryPicker.this, R.color.appPrimaryColour));
//Toast.makeText(DirectoryPicker.this, "Item CAB Clicked: Checked: " + (checked ? "true" : "false"), Toast.LENGTH_LONG).show();
Toast.makeText(DirectoryPicker.this, "Position: " + position, Toast.LENGTH_LONG).show();
if (checked)
{
//gridView.getChildAt(position).setBackgroundColor(ContextCompat.getColor(DirectoryPicker.this, R.color.appPrimaryColour));
Log.d("DirectoryPicker-View", gridView.getChildAt(position).toString());
gridView.getChildAt(position).setBackgroundResource(R.color.appPrimaryColour);
mode.invalidate();
directoryAdapter.getItem(position).setSelected(true);
selectedDirectories.add(directoryAdapter.getItem(position));
}
else
{
directoryAdapter.getItem(position).setSelected(false);
FileManager.DirectoryOrFileInfo selectedDirectory = directoryAdapter.getItem(position);
for (int i = 0; i < selectedDirectories.size(); i++)
{
if (selectedDirectories.get(i) == selectedDirectory)
{
selectedDirectories.remove(i);
selectedDirectory.setSelected(false);
}
}
gridView.getChildAt(position).setBackgroundColor(ContextCompat.getColor(DirectoryPicker.this, android.R.color.transparent));
}
mode.setTitle(selectedDirectories.size() + " selected");
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.file_dir_cab_menu, menu);
Log.d("DirectoryPicker-CAB", "OnActionModeCreate");
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu)
{
return true;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item)
{
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode)
{
selectedDirectories.clear();
}
});
为什么创建 CAB 的长按第一个项目没有成功应用背景资源,但它对所有后续选择都能正常工作。
我明白为什么这现在不起作用了。创建 CAB 时,它会触发 getView
函数在我的适配器中再次调用,因此它最终将视图重置回原来的状态。
因此,我在 getView
方法中更改了我的适配器,以便它检查项目是否标记为已选中,并设置后台资源。
我正在开发一个 android 项目,其中有一个网格视图,我正在使用 MultiChoiceModeListener 对其进行设置。
当我长按时,它成功创建了上下文操作栏,但是在创建 CAB 的初始项目上设置 setBackgroundResource
没有任何影响,但是所有后续项目被选中,背景资源申请成功。下面是我使用的代码:
gridView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);
gridView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener()
{
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked)
{
Log.d("DirectoryPicker-CAB", "CheckedStateChanged");
//findViewById((int)gridView.getItemIdAtPosition(position)).setBackgroundColor(ContextCompat.getColor(DirectoryPicker.this, R.color.appPrimaryColour));
//Toast.makeText(DirectoryPicker.this, "Item CAB Clicked: Checked: " + (checked ? "true" : "false"), Toast.LENGTH_LONG).show();
Toast.makeText(DirectoryPicker.this, "Position: " + position, Toast.LENGTH_LONG).show();
if (checked)
{
//gridView.getChildAt(position).setBackgroundColor(ContextCompat.getColor(DirectoryPicker.this, R.color.appPrimaryColour));
Log.d("DirectoryPicker-View", gridView.getChildAt(position).toString());
gridView.getChildAt(position).setBackgroundResource(R.color.appPrimaryColour);
mode.invalidate();
directoryAdapter.getItem(position).setSelected(true);
selectedDirectories.add(directoryAdapter.getItem(position));
}
else
{
directoryAdapter.getItem(position).setSelected(false);
FileManager.DirectoryOrFileInfo selectedDirectory = directoryAdapter.getItem(position);
for (int i = 0; i < selectedDirectories.size(); i++)
{
if (selectedDirectories.get(i) == selectedDirectory)
{
selectedDirectories.remove(i);
selectedDirectory.setSelected(false);
}
}
gridView.getChildAt(position).setBackgroundColor(ContextCompat.getColor(DirectoryPicker.this, android.R.color.transparent));
}
mode.setTitle(selectedDirectories.size() + " selected");
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.file_dir_cab_menu, menu);
Log.d("DirectoryPicker-CAB", "OnActionModeCreate");
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu)
{
return true;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item)
{
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode)
{
selectedDirectories.clear();
}
});
为什么创建 CAB 的长按第一个项目没有成功应用背景资源,但它对所有后续选择都能正常工作。
我明白为什么这现在不起作用了。创建 CAB 时,它会触发 getView
函数在我的适配器中再次调用,因此它最终将视图重置回原来的状态。
因此,我在 getView
方法中更改了我的适配器,以便它检查项目是否标记为已选中,并设置后台资源。