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      * This is the target pixel closest to the start of the layout that we are trying to fill
64      */
65     int mStartLine = 0;
66 
67     /**
68      * This is the target pixel closest to the end of the layout that we are trying to fill
69      */
70     int mEndLine = 0;
71 
72     /**
73      * @return true if there are more items in the data adapter
74      */
hasMore(RecyclerView.State state)75     boolean hasMore(RecyclerView.State state) {
76         return mCurrentPosition >= 0 && mCurrentPosition < state.getItemCount();
77     }
78 
79     /**
80      * Gets the view for the next element that we should render.
81      * Also updates current item index to the next item, based on {@link #mItemDirection}
82      *
83      * @return The next element that we should render.
84      */
next(RecyclerView.Recycler recycler)85     View next(RecyclerView.Recycler recycler) {
86         final View view = recycler.getViewForPosition(mCurrentPosition);
87         mCurrentPosition += mItemDirection;
88         return view;
89     }
90 
91     @Override
toString()92     public String toString() {
93         return "LayoutState{" +
94                 "mAvailable=" + mAvailable +
95                 ", mCurrentPosition=" + mCurrentPosition +
96                 ", mItemDirection=" + mItemDirection +
97                 ", mLayoutDirection=" + mLayoutDirection +
98                 ", mStartLine=" + mStartLine +
99                 ", mEndLine=" + mEndLine +
100                 '}';
101     }
102 }
103