上下文操作栏 - 在现有操作栏上方添加操作栏(两个操作栏)

时间:2016-09-23 16:42:49

标签: android android-recyclerview toolbar android-cardview contextual-action-bar

我有一个RecyclerView,其中包含多个CardView,当用户长按一个CardView时,会显示一个上下文操作栏,其中包含“删除“选项。

我使用recyclerview-multiselect库来执行此操作,并按照教程进行操作。

VideosAdapter:

public class VideosAdapter extends RecyclerView.Adapter<VideosAdapter.MyViewHolder> {

    private Context context;
    private List<VideoItem> videoList;
    private ModalMultiSelectorCallback mActionModeCallback;
    private MultiSelector mMultiSelector;

    public VideosAdapter(Context context, List<VideoItem> videoList, ModalMultiSelectorCallback mActionModeCallback, MultiSelector mMultiSelector) {
        this.context = context;
        this.videoList = videoList;
        this.mActionModeCallback = mActionModeCallback;
        this.mMultiSelector = mMultiSelector;
    }


    public class MyViewHolder extends SwappingHolder
            implements View.OnLongClickListener, View.OnClickListener {
        public ImageView thumbnail;
        private MultiSelector mMultiSelector;

        public MyViewHolder(View itemView, MultiSelector mMultiSelector) {
            super(itemView, mMultiSelector);
            this.mMultiSelector = mMultiSelector;
            itemView.setLongClickable(true);
            thumbnail = (ImageView) itemView.findViewById(R.id.thumbnail);
            setSelectionModeBackgroundDrawable(ResourcesCompat.getDrawable(context.getResources(), R.drawable.background_activated, null));
            thumbnail.setOnLongClickListener(this);
            thumbnail.setOnClickListener(this);
        }


        @Override
        public boolean onLongClick(View view) {
            if (!mMultiSelector.isSelectable()) {
                ((AppCompatActivity) context).startSupportActionMode(mActionModeCallback);
                mMultiSelector.setSelectable(true);
                mMultiSelector.setSelected(MyViewHolder.this, true);
                return true;
            }
            return false;
        }

        @Override
        public void onClick(View view) {
            if (mMultiSelector.tapSelection(this)) {
                // Selection is on, so tapSelection() toggled item selection.
            } else {
                // Selection is off; handle normal item click here.
            }
        }
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.video_card, parent, false);

        return new MyViewHolder(itemView, mMultiSelector);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        VideoItem video = videoList.get(position);
        // loading album cover using Glide library
        Glide.with(context).load(video.getThumbnail()).into(holder.thumbnail);
    }


    @Override
    public int getItemCount() {
        return videoList.size();
    }
}

GalleryFragment:

public class GalleryFragment extends Fragment {

    private RecyclerView recyclerView;
    private VideosAdapter adapter;
    private List<VideoItem> videoList;

    private MultiSelector mMultiSelector = new MultiSelector();
    private ModalMultiSelectorCallback mActionModeCallback
            = new ModalMultiSelectorCallback(mMultiSelector) {

        @Override
        public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
            super.onCreateActionMode(actionMode, menu);
            getActivity().getMenuInflater().inflate(R.menu.toolbar_delete_manu, menu);
            return true;
        }

        @Override
        public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
            if (menuItem.getItemId() == R.id.toolbar_item_delete) {
                actionMode.finish();

                for (int i = videoList.size(); i >= 0; i--) {
                    if (mMultiSelector.isSelected(i, 0)) {
                        // remove item from list
                        recyclerView.getAdapter().notifyItemRemoved(i);
                    }
                }

                mMultiSelector.clearSelections();
                return true;

            }
            return false;
        }
    };



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_gallery, container, false);

        recyclerView = (RecyclerView) root.findViewById(R.id.recycler_view);

        videoList = new ArrayList<>();
        adapter = new VideosAdapter(getActivity(), videoList, mActionModeCallback, mMultiSelector);

        recyclerView.setAdapter(adapter);

        prepareVideos(); // getting videos objects for the adapter
        return root;
    }
}

我遇到的问题是 - 当我选择CardView时,应该会发生菜单/工具栏(toolbar_delete_manu.xml)显示在/上而不是当前菜单/工具栏。

实际上发生的事情是这样的:

enter image description here

上下文工具栏超过现有工具栏。

我该如何解决?我想要它而不是

1 个答案:

答案 0 :(得分:1)

只需将<item name="android:windowActionModeOverlay">true</item>添加到AppTheme / Style

即可