Highlighting rows of CardView in RecyclerView

邮差的信 提交于 2019-12-25 04:43:20

问题


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'.

Before Selection of row:

After Selection of row

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;
        }
    }

And then in my fragment I do this:

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;
        }
    }

回答1:


So after tons of research, I finally found an answer. If you are using CardView + Big Nerd Ranch MultiSelector. When you highlight the CardView by clicking on it, only that particular card is visible and not the rest of the cards in the RecyclerView. It's because SwappingHolder works by swapping out the drawables of the top-level View. But CardView uses its own drawables, so it will not work if you use it as the top-level View.

CardView uses its own background. SwappingHolder needs to be able to replace that background to do its work.

One option would be to implement the SelectableHolder interface yourself, and choose how you want to display a selected item.

Another option is that we can make CardView child of a FrameLayout. Something like this: <FrameLayout ... > <CardView ... > </CardView> </FrameLayout>

I have summarised what's basically said here




回答2:


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.

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



来源:https://stackoverflow.com/questions/38998167/highlighting-rows-of-cardview-in-recyclerview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!