Cannot change whether this adapter has stable IDs while the adapter has registered observers

后端 未结 4 1414
难免孤独
难免孤独 2020-12-17 09:06

I am using RecyclerView in my android project and its performance has been really awful. From the answers here, I tried adding adapter.setHasStableIds(true); to

4条回答
  •  执念已碎
    2020-12-17 09:18

    as the recyclerView source code:

        /**
         * Indicates whether each item in the data set can be represented with a unique identifier
         * of type {@link java.lang.Long}.
         *
         * @param hasStableIds Whether items in data set have unique identifiers or not.
         * @see #hasStableIds()
         * @see #getItemId(int)
         */
        public void setHasStableIds(boolean hasStableIds) {
            if (hasObservers()) {
                throw new IllegalStateException("Cannot change whether this adapter has "
                        + "stable IDs while the adapter has registered observers.");
            }
            mHasStableIds = hasStableIds;
        } .   
    

    so you can workaround it .

        if (!adapter.hasObservers()) {
            adapter.setHasStableIds(true)
        }     
    

提交回复
热议问题