在RecyclerView中突出显示CardView的行 [英] Highlighting rows of CardView in RecyclerView

查看:142
本文介绍了在RecyclerView中突出显示CardView的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注RecyclerView上的 Big Nerd Ranch教程有关RecyclerView的教程.我改变了几件事.我面临的问题是,当我单击该行时;该特定内容会突出显示,但随后我看不到其他未选择的行".

I am following the Big Nerd Ranch tutorial on RecyclerView tutorial on RecyclerView. I changed a few things and there. The problem I am facing is that when I click on the row; that particular gets highlighted but then I cant see the other 'unselected rows'.

在选择行之前:

选择行后

我正在通过支架实现点击. 下面是代码:

I am implementing my clicks in a holder. Below is the code:

public class CustomRecyclerViewHolder extends SwappingHolder implements View.OnClickListener, View.OnLongClickListener {

        private TextView mMsg1, mMsg2;
        //private ImageView mAvatarView;
        private CheckBox mCheckBox;
        private LinearLayout checkboxHolder;
        private ImageView mDeleteRow;
        private CardView cardView;
        private Category category;
        private CustomRecyclerViewHolder holder;

        public CustomRecyclerViewHolder(View itemView) {
            super(itemView, myFragment.mMultiSelector);
            mMsg1 = (TextView) itemView.findViewById(R.id.text_view1);
            mMsg2 = (TextView) itemView.findViewById(R.id.text_view2);
            //mAvatarView = (ImageView)itemView.findViewById(R.id.avatar_holder);
            mCheckBox = (CheckBox) itemView.findViewById(R.id.checkbox);
            checkboxHolder = (LinearLayout) itemView.findViewById(R.id.checkbox_holder);
            mDeleteRow = (ImageView) itemView.findViewById(R.id.delete_row);
            cardView = (CardView) itemView.findViewById(R.id.card_holder);
            itemView.setOnClickListener(this);
            itemView.setLongClickable(true);
            itemView.setOnLongClickListener(this);
        }

        public void bindCategory(Category category) {
            this.category = category;
            mMsg1.setText(category.getName());
            mMsg2.setText(category.getDescriptionName());
            if (category.getCompleted() == 1) {
                mCheckBox.setChecked(true);
                mCheckBox.setVisibility(View.VISIBLE);
            } else
                mCheckBox.setChecked(true);
        }

        @Override
        public void onClick(View v) {

            if (category == null) {
                return;
            }
            checkboxHolder.setVisibility(View.VISIBLE);
            mCheckBox.setChecked(isChecked);
            if (!myFragment.mMultiSelector.tapSelection(this)) {
                //selectCrime(mCrime);
            }

        }

        @Override
        public boolean onLongClick(View v) {

            ((AppCompatActivity) mContext).startSupportActionMode(myFragment.mDeleteMode);
            myFragment.mMultiSelector.setSelected(this, true);
            return true;
        }
    }

然后在我的片段中执行此操作:

public class CustomMultiSelectorCallback extends ModalMultiSelectorCallback {

        public CustomMultiSelectorCallback(MultiSelector multiSelector) {
            super(multiSelector);
        }

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

        @Override
        public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {

            switch (menuItem.getItemId()) {
                case R.id.menu_item_delete:
                {

                    for (int i = categoriesArrayList.size(); i >= 0; i--) {
                        if (mMultiSelector.isSelected(i, 0)) {
                            categoryDatabase = new CategoryDatabase(context);
                            Category category = prescriptionsArrayList.get(i);
                            categoryDatabase.deleteCategory(category);
                            categoryRecyclerView.getAdapter().notifyItemRemoved(i);
                        }
                    }


                    categoriesArrayList.clear();
                    mMultiSelector.clearSelections();
                    actionMode.finish();
                    return true;

                }
            }
            return false;
        }
    }

推荐答案

我邀请您检查我最近编写的实现多重选择的库(如果最终需要它们,也可以拖放).在库的一个示例中,还通过在ViewHolder顶部放置一个可绘制对象来实现选择.但是,所使用的方法是在ActionMode启动时激活可绘制对象,捕获此可绘制对象上的点击事件以切换选择状态,然后在ActionMode退出时将可绘制对象的可见性设置为GONE.

I invite you to check a library I wrote recently that implements multi-selection (drag&drop and swiping too, if you end up needing them). On one of the samples of the library there's also implemented selection by putting a drawable on top of the ViewHolder. However, the approach used is activating the drawable when the ActionMode starts, capturing click events on this drawable to toggle selection states, and then set the drawable visibility to GONE when the ActionMode exits.

也许通过检查源代码,您可以找到一些有趣的东西: https://github.com/MikiLoz92/FancyAdapters

Maybe by checking the source code you can find something of interest: https://github.com/MikiLoz92/FancyAdapters

这篇关于在RecyclerView中突出显示CardView的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆