12d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar/*
22d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar * Copyright (C) 2014 The Android Open Source Project
32d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar *
42d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar * Licensed under the Apache License, Version 2.0 (the "License");
52d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar * you may not use this file except in compliance with the License.
62d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar * You may obtain a copy of the License at
72d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar *
82d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar *      http://www.apache.org/licenses/LICENSE-2.0
92d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar *
102d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar * Unless required by applicable law or agreed to in writing, software
112d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar * distributed under the License is distributed on an "AS IS" BASIS,
122d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c9a859537b0871f84afeeb706a5b425fe3f2b4ddAurimas Liutikas * See the License for the specific language governing permissions and
142d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar * limitations under the License.
152d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar */
162d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar
172d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyarpackage android.support.v7.widget;
184143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
192d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyarimport android.view.View;
202d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar
212d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar/**
222d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar * Helper class that keeps temporary state while {LayoutManager} is filling out the empty
232d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar * space.
242d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar */
252d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyarclass LayoutState {
262d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar
271e827caa16440590647019b9c1338f6309c5be7aAurimas Liutikas    static final String TAG = "LayoutState";
282d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar
291e827caa16440590647019b9c1338f6309c5be7aAurimas Liutikas    static final int LAYOUT_START = -1;
302d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar
311e827caa16440590647019b9c1338f6309c5be7aAurimas Liutikas    static final int LAYOUT_END = 1;
322d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar
331e827caa16440590647019b9c1338f6309c5be7aAurimas Liutikas    static final int INVALID_LAYOUT = Integer.MIN_VALUE;
342d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar
351e827caa16440590647019b9c1338f6309c5be7aAurimas Liutikas    static final int ITEM_DIRECTION_HEAD = -1;
362d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar
371e827caa16440590647019b9c1338f6309c5be7aAurimas Liutikas    static final int ITEM_DIRECTION_TAIL = 1;
382d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar
394143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    /**
404143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar     * We may not want to recycle children in some cases (e.g. layout)
414143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar     */
424143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    boolean mRecycle = true;
431e827caa16440590647019b9c1338f6309c5be7aAurimas Liutikas
442d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar    /**
452d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar     * Number of pixels that we should fill, in the layout direction.
462d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar     */
472d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar    int mAvailable;
482d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar
492d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar    /**
502d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar     * Current position on the adapter to get the next item.
512d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar     */
522d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar    int mCurrentPosition;
532d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar
542d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar    /**
552d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar     * Defines the direction in which the data adapter is traversed.
562d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar     * Should be {@link #ITEM_DIRECTION_HEAD} or {@link #ITEM_DIRECTION_TAIL}
572d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar     */
582d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar    int mItemDirection;
592d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar
602d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar    /**
612d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar     * Defines the direction in which the layout is filled.
622d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar     * Should be {@link #LAYOUT_START} or {@link #LAYOUT_END}
632d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar     */
642d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar    int mLayoutDirection;
652d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar
662d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar    /**
67e05fbd9cfe05496e82a3abe19e07e8745985e9a5Yigit Boyar     * This is the target pixel closest to the start of the layout that we are trying to fill
682d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar     */
69e05fbd9cfe05496e82a3abe19e07e8745985e9a5Yigit Boyar    int mStartLine = 0;
70e05fbd9cfe05496e82a3abe19e07e8745985e9a5Yigit Boyar
71e05fbd9cfe05496e82a3abe19e07e8745985e9a5Yigit Boyar    /**
72e05fbd9cfe05496e82a3abe19e07e8745985e9a5Yigit Boyar     * This is the target pixel closest to the end of the layout that we are trying to fill
73e05fbd9cfe05496e82a3abe19e07e8745985e9a5Yigit Boyar     */
74e05fbd9cfe05496e82a3abe19e07e8745985e9a5Yigit Boyar    int mEndLine = 0;
752d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar
762d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar    /**
77f89e1b82c74b7b85df3a349340a643f62fc5bfa1Yigit Boyar     * If true, layout should stop if a focusable view is added
78f89e1b82c74b7b85df3a349340a643f62fc5bfa1Yigit Boyar     */
79f89e1b82c74b7b85df3a349340a643f62fc5bfa1Yigit Boyar    boolean mStopInFocusable;
80f89e1b82c74b7b85df3a349340a643f62fc5bfa1Yigit Boyar
81f89e1b82c74b7b85df3a349340a643f62fc5bfa1Yigit Boyar    /**
824143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar     * If the content is not wrapped with any value
834143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar     */
844143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    boolean mInfinite;
854143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
864143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    /**
872d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar     * @return true if there are more items in the data adapter
882d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar     */
892d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar    boolean hasMore(RecyclerView.State state) {
902d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar        return mCurrentPosition >= 0 && mCurrentPosition < state.getItemCount();
912d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar    }
922d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar
932d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar    /**
942d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar     * Gets the view for the next element that we should render.
952d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar     * Also updates current item index to the next item, based on {@link #mItemDirection}
962d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar     *
972d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar     * @return The next element that we should render.
982d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar     */
992d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar    View next(RecyclerView.Recycler recycler) {
1002d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar        final View view = recycler.getViewForPosition(mCurrentPosition);
1012d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar        mCurrentPosition += mItemDirection;
1022d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar        return view;
1032d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar    }
104e05fbd9cfe05496e82a3abe19e07e8745985e9a5Yigit Boyar
105e05fbd9cfe05496e82a3abe19e07e8745985e9a5Yigit Boyar    @Override
106e05fbd9cfe05496e82a3abe19e07e8745985e9a5Yigit Boyar    public String toString() {
1071e827caa16440590647019b9c1338f6309c5be7aAurimas Liutikas        return "LayoutState{"
1081e827caa16440590647019b9c1338f6309c5be7aAurimas Liutikas                + "mAvailable=" + mAvailable
1091e827caa16440590647019b9c1338f6309c5be7aAurimas Liutikas                + ", mCurrentPosition=" + mCurrentPosition
1101e827caa16440590647019b9c1338f6309c5be7aAurimas Liutikas                + ", mItemDirection=" + mItemDirection
1111e827caa16440590647019b9c1338f6309c5be7aAurimas Liutikas                + ", mLayoutDirection=" + mLayoutDirection
1121e827caa16440590647019b9c1338f6309c5be7aAurimas Liutikas                + ", mStartLine=" + mStartLine
1131e827caa16440590647019b9c1338f6309c5be7aAurimas Liutikas                + ", mEndLine=" + mEndLine
1141e827caa16440590647019b9c1338f6309c5be7aAurimas Liutikas                + '}';
115e05fbd9cfe05496e82a3abe19e07e8745985e9a5Yigit Boyar    }
1162d2e8d88103866b631eb0f3805da137ebcfb0275Yigit Boyar}
117