Home | History | Annotate | Download | only in widget
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific languag`e governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.support.v7.widget;
     18 import android.view.View;
     19 
     20 /**
     21  * Helper class that keeps temporary state while {LayoutManager} is filling out the empty
     22  * space.
     23  */
     24 class LayoutState {
     25 
     26     final static String TAG = "LayoutState";
     27 
     28     final static int LAYOUT_START = -1;
     29 
     30     final static int LAYOUT_END = 1;
     31 
     32     final static int INVALID_LAYOUT = Integer.MIN_VALUE;
     33 
     34     final static int ITEM_DIRECTION_HEAD = -1;
     35 
     36     final static int ITEM_DIRECTION_TAIL = 1;
     37 
     38     final static int SCOLLING_OFFSET_NaN = Integer.MIN_VALUE;
     39 
     40     /**
     41      * Number of pixels that we should fill, in the layout direction.
     42      */
     43     int mAvailable;
     44 
     45     /**
     46      * Current position on the adapter to get the next item.
     47      */
     48     int mCurrentPosition;
     49 
     50     /**
     51      * Defines the direction in which the data adapter is traversed.
     52      * Should be {@link #ITEM_DIRECTION_HEAD} or {@link #ITEM_DIRECTION_TAIL}
     53      */
     54     int mItemDirection;
     55 
     56     /**
     57      * Defines the direction in which the layout is filled.
     58      * Should be {@link #LAYOUT_START} or {@link #LAYOUT_END}
     59      */
     60     int mLayoutDirection;
     61 
     62     /**
     63      * Used if you want to pre-layout items that are not yet visible.
     64      * The difference with {@link #mAvailable} is that, when recycling, distance rendered for
     65      * {@link #mExtra} is not considered not to recycle visible children.
     66      */
     67     int mExtra = 0;
     68 
     69     /**
     70      * @return true if there are more items in the data adapter
     71      */
     72     boolean hasMore(RecyclerView.State state) {
     73         return mCurrentPosition >= 0 && mCurrentPosition < state.getItemCount();
     74     }
     75 
     76     /**
     77      * Gets the view for the next element that we should render.
     78      * Also updates current item index to the next item, based on {@link #mItemDirection}
     79      *
     80      * @return The next element that we should render.
     81      */
     82     View next(RecyclerView.Recycler recycler) {
     83         final View view = recycler.getViewForPosition(mCurrentPosition);
     84         mCurrentPosition += mItemDirection;
     85         return view;
     86     }
     87 }
     88