上下文操作菜单位于片段中的操作栏上方

时间:2018-06-08 05:43:02

标签: android listview android-fragments contextmenu

我有一个片段活动,其中包含一个学生列表视图。当我长按时,我想要在视觉上超越Action Bar显示一个Action菜单。我能够显示菜单,但是它的位置没有超过动作栏。看图像1,我想要像图像2中那样的东西。 enter image description here

我找到了this question,我尝试了他们的建议。但仍然没有运气。

这是我的Fragment类;

public class StudentsFragment extends Fragment
{
 List<Student> studentList
 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) 
 {
    studentView = inflater.inflate(R.layout.students_layout,container,false);
    studentList = ((MainActivity) getActivity()).getStudentList();
    displayStudents(this.getActivity());
    return studentView;
   }

private void displayStudents(Context context)
{

  final StudentsAdapter studentsAdapter;
  final ListView listView;
  ...
  studentsAdapter = new StudentsAdapter(getActivity(), studentList);
    listView = (ListView) 
  studentView.findViewById(R.id.students_student_list);
  listView.setAdapter(studentsAdapter);

  listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
            @Override
            public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
                // Capture total checked items
                final int checkedCount = listView.getCheckedItemCount();
                // Set the CAB title according to total checked items
                mode.setTitle(checkedCount + " Selected");
                // Calls toggleSelection method from ListViewAdapter Class
                studentsAdapter.toggleSelection(position);
            }

            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                mode.getMenuInflater().inflate(R.menu.student_list_action_menu, menu);
                return true;
            }

            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                switch (item.getItemId()) {
                    // Calls getSelectedIds method from ListViewAdapter Class

                    case R.id.group_email:
                        ...
                        mode.finish();
                        return true;
                    default:
                        return false;
             }
            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {
                studentsAdapter.removeSelection();
            }
        });
  }
}

这是我的主要活动主题;

    <style name="AppTheme.NoActionBar">
     <item name="windowActionBar">false</item>
     <item name="windowNoTitle">true</item>
     <item name="android:windowActionModeOverlay">true</item>
    </style>

这是必需的菜单;

<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <item
    android:id="@+id/group_email"
    android:title="Email"/>
  <item
    android:id="@+id/bulk_sms"
    android:title="SMS"/>
</menu>

非常感谢任何帮助或想法。

1 个答案:

答案 0 :(得分:1)

AppTheme.NoActionBar添加此内容:

    <item name="windowActionModeOverlay">true</item>
    <item name="actionModeBackground">@color/colorPrimary</item>

您无需仅android:windowActionModeOverlay

添加windowActionModeOverlay