Skip to content
This repository was archived by the owner on Jul 7, 2019. It is now read-only.
This repository was archived by the owner on Jul 7, 2019. It is now read-only.

How to add Endless scrolling to Grid Sectioned Recyclerview #10

@SAGARSURI

Description

@SAGARSURI

I am trying to add Endless scrolling to the Grid Sectioned Recyclerview which you have implemented in sample. Initially I am able to add first 9 items. But when I scroll I am getting below mentioned error when I am adding new data to the Adapter.

java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{d8797bd position=18 id=-1, oldPos=9, pLpos:9 scrap [attachedScrap] tmpDetached no parent}
                                                                                      at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:5297)
                                                                                      at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5479)
                                                                                      at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5440)
                                                                                      at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5436)
                                                                                      at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2224)
                                                                                      at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:556)
                                                                                      at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511)
                                                                                      at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595)
                                                                                      at android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:170)
                                                                                      at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3534)
                                                                                      at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3310)
                                                                                      at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1648)
                                                                                      at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:343)
                                                                                      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
                                                                                      at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                                                                                      at android.view.Choreographer.doFrame(Choreographer.java:603)
                                                                                      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
                                                                                      at android.os.Handler.handleCallback(Handler.java:739)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                      at android.os.Looper.loop(Looper.java:152)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5497)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

I am using [EndlessRecyclerViewScrollListener](https://github.com/codepath/android_guides/wiki/Endless-Scrolling-with-AdapterViews-and-RecyclerView) to implement infinite scroll.

This is the Adapter code:

public class BaseAdapter extends SectionedRecyclerViewAdapter<BaseAdapter.SubheaderHolder, BaseAdapter.KolamViewHolder> {


    RequestManager manager;
    Context context;
    List<Gallery> galleryList;
    private OnItemClickListener onItemClickListener;
    // private static ViewPropertyAnimation.Animator animationObject;

    public interface OnItemClickListener {
        void onItemClicked(Gallery gallery);
    }

    public BaseAdapter(Context context, RequestManager glide) {
        this.context = context;
        this.galleryList = new ArrayList<>();
        this.manager = glide;
    }

    public void newItems(List<Gallery> newItems) {

        int currentSize = galleryList.size()-1;
        galleryList.addAll(newItems);
        notifyItemRangeInserted(currentSize, galleryList.size());
    }

    static class SubheaderHolder extends RecyclerView.ViewHolder {

        TextView matrixText;

        public SubheaderHolder(View itemView) {
            super(itemView);
            this.matrixText = (TextView) itemView.findViewById(R.id.matrixText);

        }
    }

    static class KolamViewHolder extends RecyclerView.ViewHolder {

        AppCompatImageView kolamImage;
        //        LottieAnimationView loader;
        ProgressBar progressBar;

        public KolamViewHolder(View itemView) {
            super(itemView);
            this.kolamImage = (AppCompatImageView) itemView.findViewById(R.id.kolamImage);
            this.progressBar = (ProgressBar) itemView.findViewById(R.id.progressBar);
        }
    }

    @Override
    public boolean onPlaceSubheaderBetweenItems(int position) {
        final Gallery image = galleryList.get(position);
        final Gallery nextImage = galleryList.get(position + 1);
        return !image.getMatrixType().equals(nextImage.getMatrixType());
    }

    @Override
    public KolamViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
        return new KolamViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.header_view, parent, false));
    }

    @Override
    public SubheaderHolder onCreateSubheaderViewHolder(ViewGroup parent, int viewType) {
        return new SubheaderHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.subheader_view, parent, false));
    }

    //this is the method to set image in the gallery
    @Override
    public void onBindItemViewHolder(final KolamViewHolder holder, final int itemPosition) {
        Log.e("BaseAdapter", galleryList.get(itemPosition).getImage());
        manager.load(galleryList.get(itemPosition).getImage()).asBitmap()
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .override(200, 200)
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        holder.kolamImage.setImageBitmap(resource);
                        holder.progressBar.setVisibility(View.GONE);
                        Log.e("BaseAdapter", "Ready");
                        //click listener to pass the gallery image to the bot
                        holder.kolamImage.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                onItemClickListener.onItemClicked(galleryList.get(itemPosition));
                            }
                        });
                    }
                });

    }

    //this is the method to change the content of the heading
    @Override
    public void onBindSubheaderViewHolder(SubheaderHolder subheaderHolder, int nextItemPosition) {
        Log.e("MatrixSize", galleryList.get(nextItemPosition).getMatrixType());
        if (galleryList.get(nextItemPosition).getMatrixType().equalsIgnoreCase("r")) {
            subheaderHolder.matrixText.setText("Rhombus");
        } else {
            subheaderHolder.matrixText.setText(galleryList.get(nextItemPosition).getMatrixType() + " x " + galleryList.get(nextItemPosition).getMatrixType());
        }
    }

    @Override
    public int getItemSize() {
        return galleryList.size();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}

I am passing new data into newItems() method

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions