0

我正在开发一个具有网格视图的 android 项目,并且正在使用 MultiChoiceModeListener 进行设置。

当我长按时,它成功创建了上下文操作栏,但是setBackgroundResource在创建 CAB 的初始项目上设置没有任何影响,但是所有后续项目被选中,后台资源被成功应用。下面是我正在使用的代码:

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的第一个项目没有成功申请后台资源,但对所有后续选择都可以正常工作。

4

1 回答 1

0

我已经知道为什么这现在不起作用了。创建 CAB 后,它会触发getView要在我的适配器中再次调用的函数,因此最终会将视图重置回原来的样子。

因此,我在getView方法中更改了我的适配器,以便它检查项目是否标记为已检查,并设置后台资源。

于 2018-12-15T11:30:53.203 回答