Search code examples
androidandroid-layoutandroid-recyclerview

Android - Default value of Recyclerview mHasFixedSize


I understand the concept of the mHasFixedSize field, however, I'm not sure what it's default value is (true or false). I could create a simple PoC and test, but:

  1. Having the answer here can help other people (might end up creating a PoC and answer my own question);
  2. I'd like to understand how someone found out (I tried looking at the code of RecyclerView.java, but it's always hard for me to find things on those codes and having a perception of how someone looked at this code (or any other place) to find it can be helpfull).

Solution

  • The way you can find out what is this default value is exactly how you were doing: Looking it up on the RecyclerView class you are using in your project. It's important that the code you are inspecting is the one in your project, not a version of this class you got online, for example, because it could be from a different version you are using in your project. A good way to go to this class, on windows, it would be to CTRL + Right Click above the name of the class in a place it's been used.

    Once you are in there, you can look for the mHasFixedSize field and see it's usages. In my case (RecyclerViewVersion 1.1.0) it was used in those places along the class file:

    public class RecyclerView extends ViewGroup implements ScrollingView, NestedScrollingChild2, NestedScrollingChild3 {
    
        // ..
    
        boolean mHasFixedSize;
    
        // ..
    
        public void setHasFixedSize(boolean hasFixedSize) {
            mHasFixedSize = hasFixedSize;
        }
    
        // ..
    
        public boolean hasFixedSize() {
            return mHasFixedSize;
        }
    
        protected void onMeasure(int widthSpec, int heightSpec) {
    
            // ..
    
            if (mLayout.isAutoMeasureEnabled()) {
        
                // ..
        
            } else {
                if (mHasFixedSize) {
                
                    // ...
                
                }
        
                // ...
    
            }
    
            // ...
        
            void triggerUpdateProcessor() {
                if (POST_UPDATES_ON_ANIMATION && mHasFixedSize && mIsAttached) {
    
                    // ...
        
                } else {
        
                    // ...
    
                }
            }
        }
    

    Now, when we look at this code, we see that the only place where this variable is being updated is on it's setter (setHasFixedSize) and getting used to kotlin, a person might think that the atribute mHasFixedSize has no default value because of that, however, in Java, after defining a primitive value without setting it's to an specific value on its declaration, a default value will be set automaticaly, and on the case of a boolean atribute, it will be false. So, in this context, if the user does not set it to true, the RecyclerView class treats it as false.