1 /*
2  * Copyright 2021 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 language governing permissions and
14  * limitations under the License.
15  */
16 package androidx.leanback.widget;
17 
18 import android.annotation.SuppressLint;
19 
20 import androidx.leanback.widget.ItemAlignmentFacet.ItemAlignmentDef;
21 import androidx.recyclerview.widget.RecyclerView;
22 
23 import org.jspecify.annotations.NonNull;
24 import org.jspecify.annotations.Nullable;
25 
26 /**
27  * Interface for receiving notification when a child of this ViewGroup has been selected.
28  * There are two methods:
29  * <ul>
30  * <li>
31  *     {link {@link #onChildViewHolderSelected(RecyclerView, RecyclerView.ViewHolder, int, int)}}
32  *     is called when the view holder is about to be selected.  The listener could change size
33  *     of the view holder in this callback.
34  * </li>
35  * <li>
36  *     {link {@link #onChildViewHolderSelectedAndPositioned(RecyclerView, RecyclerView.ViewHolder,
37  *     int, int)} is called when view holder has been selected and laid out in RecyclerView.
38  * </li>
39  * </ul>
40  */
41 @SuppressLint("ListenerInterface")
42 public abstract class OnChildViewHolderSelectedListener {
43     /**
44      * Callback method to be invoked when a child of this ViewGroup has been selected. Listener
45      * might change the size of the child and the position of the child is not finalized. To get
46      * the final layout position of child, override {@link #onChildViewHolderSelectedAndPositioned(
47      *RecyclerView, RecyclerView.ViewHolder, int, int)}.
48      *
49      * @param parent      The RecyclerView where the selection happened.
50      * @param child       The ViewHolder within the RecyclerView that is selected, or null if no
51      *                    view is selected.
52      * @param position    The position of the view in the adapter, or NO_POSITION
53      *                    if no view is selected.
54      * @param subposition The index of which {@link ItemAlignmentDef} being used,
55      *                    0 if there is no ItemAlignmentDef defined for the item.
56      */
onChildViewHolderSelected(@onNull RecyclerView parent, RecyclerView.@Nullable ViewHolder child, int position, int subposition)57     public void onChildViewHolderSelected(@NonNull RecyclerView parent,
58             RecyclerView.@Nullable ViewHolder child,
59             int position, int subposition) {
60     }
61 
62     /**
63      * Callback method to be invoked when a child of this ViewGroup has been selected and
64      * positioned.
65      *
66      * @param parent      The RecyclerView where the selection happened.
67      * @param child       The ViewHolder within the RecyclerView that is selected, or null if no
68      *                    view is selected.
69      * @param position    The position of the view in the adapter, or NO_POSITION
70      *                    if no view is selected.
71      * @param subposition The index of which {@link ItemAlignmentDef} being used,
72      *                    0 if there is no ItemAlignmentDef defined for the item.
73      */
onChildViewHolderSelectedAndPositioned(@onNull RecyclerView parent, RecyclerView.@Nullable ViewHolder child, int position, int subposition)74     public void onChildViewHolderSelectedAndPositioned(@NonNull RecyclerView parent,
75             RecyclerView.@Nullable ViewHolder child, int position, int subposition) {
76     }
77 }
78