IndexOutOfBoundsException Несоответствие обнаружено RecyclerView.onLayout () - PullRequest
0 голосов
/ 06 января 2020

Этот вид брака sh происходит слишком много !!!

enter image description here

Нашел эту проблему сегодня.

В последнее время я получаю упомянутое выше слово cra sh на нескольких устройствах. Я думаю, что это как-то связано с RecyclerView внутри ScrollView ??

Но я не уверен, так как он месяцами работал нормально, но внезапно начал это видеть. Recycler View Несоответствие Обнаружена ошибка, возникающая при быстрой прокрутке или прокрутка при загрузке большего количества элементов.

Я получаю его в Fabri c Crashlytics.

Fatal Exception: java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{2e20039 position=4 id=-1, oldPos=-1, pLpos:-1 no parent} android.support.v7.widget.RecyclerView{9d0ee32 VFED..... .F...... 0,0-1440,191 #7f0a037d app:id/recylerCategoryList}, adapter:ahs@8a75b3d, layout:android.support.v7.widget.LinearLayoutManager@b3c9f32, context:com.ui.activity.NEWBusinessCardMainActivity@fe48a6e
       at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition + 5715(RecyclerView.java:5715)
       at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline + 5898(RecyclerView.java:5898)
       at android.support.v7.widget.GapWorker.prefetchPositionWithDeadline + 286(GapWorker.java:286)
       at android.support.v7.widget.GapWorker.flushTaskWithDeadline + 343(GapWorker.java:343)
       at android.support.v7.widget.GapWorker.flushTasksWithDeadline + 359(GapWorker.java:359)
       at android.support.v7.widget.GapWorker.prefetch + 366(GapWorker.java:366)
       at android.support.v7.widget.GapWorker.run + 397(GapWorker.java:397)
       at android.os.Handler.handleCallback + 751(Handler.java:751)
       at android.os.Handler.dispatchMessage + 95(Handler.java:95)
       at android.os.Looper.loop + 154(Looper.java:154)
       at android.app.ActivityThread.main + 6682(ActivityThread.java:6682)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run + 1520(ZygoteInit.java:1520)
       at com.android.internal.os.ZygoteInit.main + 1410(ZygoteInit.java:1410)

Адаптер:

public class CategoryHomeAdapter extends RecyclerView.Adapter<CategoryHomeAdapter.ItemViewHolder> {

    public static final String TAG = "CategoryAdapter";
    private Context activity;
    private ArrayList<NEWCategoryList> categoryList = new ArrayList<>();
    private ArrayList<NEWCategoryList> originalList = new ArrayList<>();
    private RecyclerViewItemInterface viewItemInterface;
    int i = 0;


    int defaultSelectedValue;

    private int selectedPosition = 0;


    public CategoryHomeAdapter(Context activity, ArrayList<NEWCategoryList> categoryList) {
        this.categoryList = categoryList;
        this.activity = activity;
        this.defaultSelectedValue = defaultSelectedValue;

        Log.i(TAG, "categoryList Size :" + categoryList.size());

    }


    public void setViewItemInterface(RecyclerViewItemInterface viewItemInterface) {
        Log.i(TAG, "setViewItemInterface: ");
        this.viewItemInterface = viewItemInterface;
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_home_category_list, parent, false);
        return new ItemViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull final ItemViewHolder holder, final int position) {
        try {
            final NEWCategoryList category = categoryList.get(position);
            holder.categoryName.setText(category.getTagName());


            holder.itemView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.i(TAG, "[onClick] getAdapterPosition:" + holder.getAdapterPosition());
                    try {
                        if (holder.getAdapterPosition() != -1 && category.getSubCategoryTagId() != null) {
                            Log.i(TAG, "onClick: Category ID selected :" + category.getSubCategoryTagId());

                            if (categoryList != null && categoryList.size() > 0) {

                                for (NEWCategoryList newList : categoryList) {

                                    if (!newList.getSubCategoryTagId().equals(category.getSubCategoryTagId())) {
                                        Log.i(TAG, "onClick: FALSE");
                                        holder.CategoryImage.setVisibility(View.GONE);
                                        newList.setSelected(false);
                                    } else {
                                        holder.CategoryImage.setVisibility(View.VISIBLE);

                                       Log.i(TAG, "onClick: TRUE");
                                        newList.setSelected(true);
                                    }

                                }
                            }
                            Log.i(TAG, "Category Name : " + category.getTagName() + "Category Id : " + category.getSubCategoryTagId());
                            if (viewItemInterface != null) {
                                viewItemInterface.onItemClick(holder.getAdapterPosition(), categoryList.get(holder.getAdapterPosition()));
                            }

                        } else {
                            Log.i(TAG, "onClick: category Id is Wrong....");
                        }
                    } catch (Throwable e) {
//            e.printStackTrace();
                        AppUtils.throwFatalException(e);
                    }
                }
            });

        } catch (Exception e) {
//            e.printStackTrace();
            AppUtils.throwFatalException(e);
        }


    }

    public void changeImage(int index) {
        categoryList.get(index).setSelected(true);
        notifyItemChanged(index);

    }


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

    class ItemViewHolder extends RecyclerView.ViewHolder {
        private TextView categoryName;
        private ImageView CategoryImage;

        public ItemViewHolder(View view) {
            super(view);
            this.categoryName = view.findViewById(R.id.textCaregoryname);
            this.CategoryImage = view.findViewById(R.id.CategoryImage);

        }
    }

    public void makeSearchList() {
        this.originalList.clear();
        this.originalList.addAll(this.categoryList);
    }


}

Фрагмент:

public class Fragment extends DefaultFragment implements OnLoadMoreListener, RecyclerViewItemInterface {

@Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Log.e(TAG, "******   onAttach   *******");
        this.activity = baseActivity;

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_sample_img_list_home, container, false);

        }

        @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        populateList();
        populatedCategoryList();
        loadCategory();

        }

 private void populateList() {
        if (AppUtils.isValidContext(activity) && isAdded()) {
            try {
                sampleJsonList.clear();
                LinearLayoutManager mLayoutManager = new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false);
                listBgImg.setLayoutManager(mLayoutManager);

                bgImageAdapterNEW = new CategoryImageAdapterNew(activity, listBgImg, new GlideImageLoader(activity.getApplicationContext()), sampleJsonList, freeSampleList);
                listBgImg.setAdapter(bgImageAdapterNEW);

                bgImageAdapterNEW.setViewItemInterface(new RecyclerViewItemInterface() {

                bgImageAdapterNEW.setPageAppendListener(new PageAppendListener() {
                    @Override
                    public void onPageAppendClick(final int page) {
                        Log.i(TAG, "onPageAppendClick : " + page);

                        listBgImg.post(new Runnable() {
                            public void run() {
                                // Remove Append Button
                                try {
                                    Log.i(TAG, "List Size : " + sampleJsonList.size());
                                    sampleJsonList.remove(sampleJsonList.size() - 1);
                                    bgImageAdapterNEW.notifyItemRemoved(sampleJsonList.size());
                                    onLoadMore(page, true);
                                } catch (Throwable e) {
                                    e.printStackTrace();
                                }
                            }
                        });


                    }

                    @Override
                    public void onPageAppendClick() {
                        Log.i(TAG, "btnLoadMore button Click ");
                    }

                    @Override
                    public void showBottomToTop(boolean isShow) {

                    }
                });

                bgImageAdapterNEW.setOnLoadMoreListener(this);

            } catch (IndexOutOfBoundsException e) {
                e.printStackTrace();
            }
          }
       }
    }

    private void populatedCategoryList() {
        if (AppUtils.isValidContext(activity) && isAdded()) {
            try {
                Log.i(TAG, "populatedCategoryList: ");
                categoryList.clear();
                LinearLayoutManager mLayoutManager = new LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false);
                CategoryRecylerView.setLayoutManager(mLayoutManager);
                categoryHomeAdapter = new CategoryHomeAdapter(activity, categoryList);
                categoryHomeAdapter.setViewItemInterface(this);
                CategoryRecylerView.setAdapter(categoryHomeAdapter);
            } catch (Throwable e) {
                e.printStackTrace();
            }

        }
    }

     private void loadCategory() {
        try {
            categoryList.clear();
            ArrayList<NEWCategoryList> localList = new ArrayList<>();
            String s = SessionManager.getInstance().getKeyCatelogTagtemplate();

            Log.i(TAG, "loadCategory: " + s);

            if (!s.isEmpty()) {
                CategorySampleData data = new Gson().fromJson(s, CategorySampleData.class);
                if (data != null && data.getCategorylist() != null && data.getCategorylist().size() > 0) {
                    localList = data.getCategorylist();
                }
            }

        } catch (JsonSyntaxException e) {
            e.printStackTrace();
            AppUtils.throwFatalException(e);
        }
    }

    /*Recyler View */

    @Override
    public void onItemClick(int position, Object obj) {
        try {
            Log.i(TAG, "onItemClick: Object");
            if (categoryHomeAdapter != null && obj instanceof NEWCategoryList) {
                if (((NEWCategoryList) obj).getSubCategoryTagId() != null) {
                    categoryHomeAdapter.notifyDataSetChanged();
                    NEWCategoryList CategoryList = (NEWCategoryList) obj;
                    if (subCategoryId != CategoryList.getSubCategoryTagId()) {
                        subCategoryId = CategoryList.getSubCategoryTagId();
                        Log.i(TAG, "onItemClick: subCategoryId" + subCategoryId);

                        if (!sampleJsonList.isEmpty()) {
                            sampleJsonList.clear();
                            if (bgImageAdapterNEW != null) {
                                bgImageAdapterNEW.notifyDataSetChanged();
                            }
                        }

                        tagName = CategoryList.getTagName();
                        Log.i(TAG, "onItemClick: Tag Name is : - " + tagName);
                        cancelOldRequest();
                        getAllSample(1, true, subCategoryId, tagName, false);
                    }
                }
            }

        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}

Как это исправить?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...