FocusDelegate.java revision f27b1ffc67228d73326ec3426fef4c9db75cd6fd
1c1b53ee5d095a74b84c21257fb4cd00e33f748baYigit Boyar/*
264db0cc15b78b62a1d44a70fc8b4552e660d952cYigit Boyar * Copyright 2017 The Android Open Source Project
3c1b53ee5d095a74b84c21257fb4cd00e33f748baYigit Boyar *
4c1b53ee5d095a74b84c21257fb4cd00e33f748baYigit Boyar * Licensed under the Apache License, Version 2.0 (the "License");
5c1b53ee5d095a74b84c21257fb4cd00e33f748baYigit Boyar * you may not use this file except in compliance with the License.
6c1b53ee5d095a74b84c21257fb4cd00e33f748baYigit Boyar * You may obtain a copy of the License at
7c1b53ee5d095a74b84c21257fb4cd00e33f748baYigit Boyar *
8c1b53ee5d095a74b84c21257fb4cd00e33f748baYigit Boyar *      http://www.apache.org/licenses/LICENSE-2.0
9c1b53ee5d095a74b84c21257fb4cd00e33f748baYigit Boyar *
10c1b53ee5d095a74b84c21257fb4cd00e33f748baYigit Boyar * Unless required by applicable law or agreed to in writing, software
11c1b53ee5d095a74b84c21257fb4cd00e33f748baYigit Boyar * distributed under the License is distributed on an "AS IS" BASIS,
12c1b53ee5d095a74b84c21257fb4cd00e33f748baYigit Boyar * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c1b53ee5d095a74b84c21257fb4cd00e33f748baYigit Boyar * See the License for the specific language governing permissions and
14c1b53ee5d095a74b84c21257fb4cd00e33f748baYigit Boyar * limitations under the License.
15c1b53ee5d095a74b84c21257fb4cd00e33f748baYigit Boyar */
16c1b53ee5d095a74b84c21257fb4cd00e33f748baYigit Boyar
1764db0cc15b78b62a1d44a70fc8b4552e660d952cYigit Boyarpackage androidx.recyclerview.selection;
18c1b53ee5d095a74b84c21257fb4cd00e33f748baYigit Boyar
1927015a7478beac9e231135207e3e705784cee508Sergey Vasilinetsimport android.support.annotation.NonNull;
2027015a7478beac9e231135207e3e705784cee508Sergey Vasilinetsimport android.support.v7.widget.RecyclerView;
21c1b53ee5d095a74b84c21257fb4cd00e33f748baYigit Boyar
2264db0cc15b78b62a1d44a70fc8b4552e660d952cYigit Boyarimport androidx.recyclerview.selection.ItemDetailsLookup.ItemDetails;
2364db0cc15b78b62a1d44a70fc8b4552e660d952cYigit Boyar
2427015a7478beac9e231135207e3e705784cee508Sergey Vasilinets/**
25c1b53ee5d095a74b84c21257fb4cd00e33f748baYigit Boyar * Override methods in this class to provide application specific behaviors
26c1b53ee5d095a74b84c21257fb4cd00e33f748baYigit Boyar * related to focusing item.
27c1b53ee5d095a74b84c21257fb4cd00e33f748baYigit Boyar *
28c43ce90b803cdfa033dfc94fa4161371ed6f3ec6Sergey Vasilinets * @param <K> Selection key type. @see {@link StorageStrategy} for supported types.
29c43ce90b803cdfa033dfc94fa4161371ed6f3ec6Sergey Vasilinets */
30c43ce90b803cdfa033dfc94fa4161371ed6f3ec6Sergey Vasilinetspublic abstract class FocusDelegate<K> {
31c1b53ee5d095a74b84c21257fb4cd00e33f748baYigit Boyar
32e69e470336d0b6a1b4a16fe1783af17143d0c426Sergey Vasilinets    static <K> FocusDelegate<K> dummy() {
33e13540a25fe5460240b6db0e18858c32d16f5399Sergey Vasilinets        return new FocusDelegate<K>() {
34e69e470336d0b6a1b4a16fe1783af17143d0c426Sergey Vasilinets            @Override
35e13540a25fe5460240b6db0e18858c32d16f5399Sergey Vasilinets            public void focusItem(@NonNull ItemDetails<K> item) {
3627015a7478beac9e231135207e3e705784cee508Sergey Vasilinets            }
3727015a7478beac9e231135207e3e705784cee508Sergey Vasilinets
3827015a7478beac9e231135207e3e705784cee508Sergey Vasilinets            @Override
3927015a7478beac9e231135207e3e705784cee508Sergey Vasilinets            public boolean hasFocusedItem() {
40e69e470336d0b6a1b4a16fe1783af17143d0c426Sergey Vasilinets                return false;
41e69e470336d0b6a1b4a16fe1783af17143d0c426Sergey Vasilinets            }
4227015a7478beac9e231135207e3e705784cee508Sergey Vasilinets
4327015a7478beac9e231135207e3e705784cee508Sergey Vasilinets            @Override
4427015a7478beac9e231135207e3e705784cee508Sergey Vasilinets            public int getFocusedPosition() {
45e69e470336d0b6a1b4a16fe1783af17143d0c426Sergey Vasilinets                return RecyclerView.NO_POSITION;
46e69e470336d0b6a1b4a16fe1783af17143d0c426Sergey Vasilinets            }
47e69e470336d0b6a1b4a16fe1783af17143d0c426Sergey Vasilinets
4889b6198c76e51a505556d7f4e57d3e48e24d5eceYigit Boyar            @Override
4989b6198c76e51a505556d7f4e57d3e48e24d5eceYigit Boyar            public void clearFocus() {
5089b6198c76e51a505556d7f4e57d3e48e24d5eceYigit Boyar            }
51c1b53ee5d095a74b84c21257fb4cd00e33f748baYigit Boyar        };
52e69e470336d0b6a1b4a16fe1783af17143d0c426Sergey Vasilinets    }
53e69e470336d0b6a1b4a16fe1783af17143d0c426Sergey Vasilinets
54e69e470336d0b6a1b4a16fe1783af17143d0c426Sergey Vasilinets    /**
55c1b53ee5d095a74b84c21257fb4cd00e33f748baYigit Boyar     * If environment supports focus, focus {@code item}.
56c1b53ee5d095a74b84c21257fb4cd00e33f748baYigit Boyar     */
57e69e470336d0b6a1b4a16fe1783af17143d0c426Sergey Vasilinets    public abstract void focusItem(@NonNull ItemDetails<K> item);
58c1b53ee5d095a74b84c21257fb4cd00e33f748baYigit Boyar
59c1b53ee5d095a74b84c21257fb4cd00e33f748baYigit Boyar    /**
60c43ce90b803cdfa033dfc94fa4161371ed6f3ec6Sergey Vasilinets     * @return true if there is a focused item.
6134e5031083f735db3a395b0f6aa430880b072d71Yigit Boyar     */
62abf6c87826e1a86fed71d945dc7e7f1aa643ea6cSergey Vasilinets    public abstract boolean hasFocusedItem();
6334e5031083f735db3a395b0f6aa430880b072d71Yigit Boyar
646ca8525782f16bcb4403dc692a1fefd77934aa31Sergey Vasilinets    /**
65c1b53ee5d095a74b84c21257fb4cd00e33f748baYigit Boyar     * @return the position of the currently focused item, if any.
66c1b53ee5d095a74b84c21257fb4cd00e33f748baYigit Boyar     */
67e13540a25fe5460240b6db0e18858c32d16f5399Sergey Vasilinets    public abstract int getFocusedPosition();
687c41ee306fc78feaa0043c65bf82e65935bd9d35Sergey Vasilinets
697c41ee306fc78feaa0043c65bf82e65935bd9d35Sergey Vasilinets    /**
707c41ee306fc78feaa0043c65bf82e65935bd9d35Sergey Vasilinets     * If the environment supports focus and something is focused, unfocus it.
717c41ee306fc78feaa0043c65bf82e65935bd9d35Sergey Vasilinets     */
727c41ee306fc78feaa0043c65bf82e65935bd9d35Sergey Vasilinets    public abstract void clearFocus();
73b86bef286718da421268bc52cf4fab7cccb3104cSergey Vasilinets}
74b86bef286718da421268bc52cf4fab7cccb3104cSergey Vasilinets