RecyclerView Adapter Lint Error不将位置视为固定 [英] RecyclerView Adapter Lint Error do not treat position as fixed

查看:176
本文介绍了RecyclerView Adapter Lint Error不将位置视为固定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经遇到了这种类型的错误,但是我不知道该怎么办.

I have surf this type of error but I didn't get what can I do in my case.

我遇到了皮棉错误.

请勿将职位视为固定职位;仅立即使用并致电 holder.getAdapterPosition()以便以后查找.

Do not treat position as fixed; only use immediately and call holder.getAdapterPosition() to look it up later.

RecyclerView不会在以下位置再次调用onBindViewHolder 除非项目本身无效,否则项目将在数据集中更改 或无法确定新位置.因此,您应该 仅在获取相关数据项时使用position参数 在此方法中,并且不应保留其副本.

RecyclerView will not call onBindViewHolder again when the position of the item changes in the data set unless the item itself is invalidated or the new position cannot be determined. For this reason, you should only use the position parameter while acquiring the related data item inside this method, and should not keep a copy of it.

如果以后需要某个项目的位置(例如,单击一次 侦听器),请使用getAdapterPosition()来更新 适配器位置.

If you need the position of an item later on (e.g. in a click listener), use getAdapterPosition() which will have the updated adapter position.

我的RecyclerView适配器:

   /**
     * Right Content Adapter
     */
    public class RightContentAdapter
            extends RecyclerView.Adapter<RightContentAdapter.ViewHolder> {

        public final ArrayList<IdNamePair> mValues;
        public Activity context;
        private int pos;

        public RightContentAdapter(Activity context, ArrayList<IdNamePair> items) {
            mValues = items;
            this.context = context;

        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.activity_filter_checkbox, parent, false);

            Log.logInfo("onCreateViewHolder call");

            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(final ViewHolder holder, final int position) {
            final IdNamePair idNamePair = mValues.get(position);
            holder.onBind = true;
            Log.logError("onBind");
            if (idNamePair.getName() != null) {
                holder.mItemCheckBox.setText(idNamePair.getName());
                holder.mItemCheckBox.setChecked(idNamePair.isChecked());

                if (idNamePair.isChecked())
                    countFilter++;
            }

            holder.onBind = false;
            holder.mItemCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    Log.logInfo("isChecked : " + isChecked);
                    boolean check = false;
                    if (isChecked)
                        countFilter++;
                    else {
                        countFilter--;
                    }
                    if (filterAttributeArrayList != null) {
                        int filterAttribSize = filterAttributeArrayList.size();
                        for (int i = 0; i < filterAttribSize; i++) {
                            FilterAttribute fa = filterAttributeArrayList.get(i);
                            if (selectedAttributeName.equalsIgnoreCase(fa.getAttrName())) {
                                check = true;

                                filterAttributeArrayList.get(i).getAttrVal().get(position).setIsChecked(isChecked);
                                filterAttributeArrayList.get(i).setFilterCount(countFilter);
                                mValues.get(position).setIsChecked(isChecked);
                                //countFilter = 0;
                                // if(!holder.onBind)
                                // notifyItemChanged(position);


                                //mRightContentAdapter.notifyItemChanged(position);
                                break;
                            }
                        }
                    }
                    if (!check) {
                        if (staticFilterArrayList != null) {
                            int staticFilterSize = staticFilterArrayList.size();
                            for (int i = 0; i < staticFilterSize; i++) {
                                StaticFilterData sf = staticFilterArrayList.get(i);
                                if (selectedAttributeName.equalsIgnoreCase(sf.getDisplayName())) {

                                    staticFilterArrayList.get(i).getValue().get(position).setIsChecked(isChecked);
                                    staticFilterArrayList.get(i).setFilterCount(countFilter);
                                    mValues.get(position).setIsChecked(isChecked);
                                    //  countFilter = 0;
                                    //     if(!holder.onBind)
                                    //       notifyItemChanged(position);
                                    //mRightContentAdapter.notifyItemChanged(position);
                                    break;
                                }
                            }
                        }
                    }

                }
            });
        }

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

        //View Holder
        public class ViewHolder extends RecyclerView.ViewHolder {

            //  private final View mView;
            private CheckBox mItemCheckBox;

            ViewHolder(View view) {
                super(view);
                //     mView = view;
                mItemCheckBox = (CheckBox) view.findViewById(R.id.filter_checkbox);
                mItemCheckBox.setTypeface(Font.getMyRiadProRegular(context));
            }
        }
    }

我已经使用它来使Filter类型的Activity像Flipkart一样.有时,复选框在滚动时也会自动取消选中. 我的代码有什么问题吗?

I have used it to make Filter type Activity like Flipkart. Sometimes checkbox also getting unchecked automatically while scrolling. Is there anything wrong in my code?

谢谢.

推荐答案

不要在onBindViewHolder方法中将position设置为 final ,而不是仅将holder作为最终名称并使用无论您在哪里使用positionholder.getAdapterPosition().

Do not make position as final in onBindViewHolder method instead of that make only holder as final and make use of holder.getAdapterPosition() wherever you are using position.

甚至不要做这样的事情:int var = holder.getAdapterPosition(),立即使用holder.getAdapterPosition().您的第一个问题已解决. :)

Don't even do something like this: int var = holder.getAdapterPosition(), straight away use holder.getAdapterPosition(). Your first problem is solved. :)

接下来,当您滚动时,您的某些复选框不会被选中,因为recyclerView重用了itemView而不是创建new,这就是为什么它比listView更好的原因.因此,您需要正确处理它.如果您可以在onCheckChangedListener中准确说明您要做什么,那么我一定可以帮助您解决这个问题.

Next when you are scrolling then some of your checkboxes gets unchecked because recyclerView reuses the itemView instead of creating new that's why it is better than listView. So you need to handle it properly. If you can tell exactly what you are trying to do in onCheckChangedListener then I can surely help you out in handling this.

这篇关于RecyclerView Adapter Lint Error不将位置视为固定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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