Ravindra Kushwaha
Ravindra Kushwaha

Reputation: 8042

On scroll the RecyclerView getting exception : -" java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 21"

I am using the RecyclerView with Sqlite in my app. As i have load the RecyclerView from the SQlite and than after call service in background and try to notify the RecyclerView by notifyDataSetChanged() method but getting the unexpected exception while scrolling the RecyclerView( when RecyclerView is updating or notifying).

Please check my setAdapter() which i am using to notify and set the adapter on RecyclerView

  private void setAdapter() {

        if (adapter == null) {
            adapter = new EmojiAdapter(stickerArrayList, getActivity(), "Sticker", stickerIdArrayList);
            RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
            rvEmoji.setLayoutManager(mLayoutManager);
            rvEmoji.setItemAnimator(new DefaultItemAnimator());
            rvEmoji.setAdapter(adapter);
        } else {

            adapter.notifyDataSetChanged();

        }

    }

I have tried all RecyclerView.stopScroll(), RecyclerView.getRecycledViewPool().clear() but problem is not short out

Exception

java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 21(offset:21).state:32 at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5546) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5482) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5478) at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2215) at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1542) at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1502) at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1316) at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1061) at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:4769) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) at android.view.Choreographer.doCallbacks(Choreographer.java:574) at android.view.Choreographer.doFrame(Choreographer.java:543) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5095) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) at dalvik.system.NativeStart.main(Native Method)

There are alots of simliar question but i am not getting the expected result.Here is the link which i have visited before posting the question here.
1. First link
2. Second link
3. Third link
4. Forth link
5. Fifth link
6. Sixth link

I have visited all the link and tried their best solutions , but it is not working for me..

I am using the following gradle version for the RecyclerView

compile 'com.android.support:recyclerview-v7:26.+'

Please check the Adapter class which i am using as follow:-

public class EmojiAdapter extends RecyclerView.Adapter<EmojiAdapter.MyViewHolder> {

    private Activity activity;
    private HashMap<String, ArrayList<EmojiBean>> arrayList = new HashMap<>();
    private String sticker_emoji;
    private ArrayList<String> keyArrayList = new ArrayList<>();

    public class MyViewHolder extends RecyclerView.ViewHolder {

        private RecyclerView rvItemCreation;
        private TextView txtStickerName, txtBy, txtCreator;
        private ImageView imgForward, imgDownload;

        private MyViewHolder(View view) {
            super(view);

            rvItemCreation = (RecyclerView) view.findViewById(R.id.rvItemCreation);
            txtStickerName = (TextView) view.findViewById(R.id.txtStickerName);
            txtBy = (TextView) view.findViewById(R.id.txtBy);
            txtCreator = (TextView) view.findViewById(R.id.txtCreator);
            imgForward = (ImageView) view.findViewById(R.id.imgForward);
            imgDownload = (ImageView) view.findViewById(R.id.imgDownload);

            txtStickerName.setTypeface(Constant.setCustomFont(activity, "Montserrat-SemiBold.otf"));
            txtBy.setTypeface(Constant.setCustomFont(activity, "Montserrat_Regular.otf"));
            txtCreator.setTypeface(Constant.setCustomFont(activity, "Montserrat_Regular.otf"));

        }
    }


    public EmojiAdapter(HashMap<String, ArrayList<EmojiBean>> arrayList, Activity activity, String sticker_emoji, ArrayList keyArrayList) {
        this.arrayList = arrayList;
        this.activity = activity;
        this.sticker_emoji = sticker_emoji;
        this.keyArrayList = keyArrayList;


    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.my_media_item, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {


        if (sticker_emoji.equalsIgnoreCase("Sticker")) {
            holder.imgForward.setImageResource(R.drawable.forward_icon);
        } else {
            holder.imgForward.setImageResource(R.drawable.delete_icon);
        }


        if (arrayList.size() > position) {
            MyCreationItemAdapter mAdapter = new MyCreationItemAdapter(arrayList.get(keyArrayList.get(position)), activity);
            RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false);
            holder.rvItemCreation.setLayoutManager(mLayoutManager);
            holder.rvItemCreation.setItemAnimator(new DefaultItemAnimator());
            holder.rvItemCreation.setAdapter(mAdapter);
        }


    }

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


}

Please help me to short out from the problem.Thanks

Update
What i have done to resolve this conflict that I have done disable user interface(Show some progress bar) while loading the data.

Upvotes: 1

Views: 996

Answers (2)

Deep Lathia
Deep Lathia

Reputation: 770

Try commenting this line of code:

rvEmoji.setItemAnimator(new DefaultItemAnimator());

Check if it works now. If it does then the error you faced is because the PredictiveAnimations are enabled. To use your code with animation you'll have to subclass the specific layout manager that you are using and override the supportsPredictiveItemAnimations() method and return false.

@Override 
public boolean supportsPredictiveItemAnimations() { 
       return false; 
}

For detailed understanding have a look here.

Upvotes: 1

Gxz
Gxz

Reputation: 11

I had the same question with "java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 6(offset:6).state". This crash is show with pullrefrash , when you had loadmore the list is more than you first loaded . then you pulltorfrash new data and clear the old list data ,at this moment you adapter’s data size is not more than you new data size.

Upvotes: 1

Related Questions