1 /* 2 * Copyright (C) 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 com.android.car.ui.recyclerview; 17 18 import androidx.annotation.NonNull; 19 import androidx.annotation.Nullable; 20 import androidx.recyclerview.widget.GridLayoutManager; 21 import androidx.recyclerview.widget.GridLayoutManager.DefaultSpanSizeLookup; 22 import androidx.recyclerview.widget.GridLayoutManager.SpanSizeLookup; 23 import androidx.recyclerview.widget.RecyclerView.LayoutManager; 24 25 import com.android.car.ui.recyclerview.CarUiRecyclerView.CarUiRecyclerViewLayout; 26 27 /** 28 * CarUi proxy class for {@link GridLayoutManager} 29 */ 30 public final class CarUiGridLayoutStyle implements CarUiLayoutStyle { 31 32 private int mSpanCount = 1; 33 @CarUiRecyclerViewLayout 34 private int mLayoutType = CarUiRecyclerViewLayout.GRID; 35 @Orientation 36 private int mLayoutOrientation = VERTICAL; 37 private boolean mReverseLayout = false; 38 private int mSize = CarUiRecyclerView.SIZE_LARGE; 39 @Nullable 40 private SpanSizeLookup mSpanSizeLookup = new DefaultSpanSizeLookup(); 41 42 /** 43 * @param layoutManager 44 * @return instance {@link CarUiLayoutStyle} using the passed {@link LayoutManager} 45 */ 46 @Nullable from(@ullable LayoutManager layoutManager)47 public static CarUiGridLayoutStyle from(@Nullable LayoutManager layoutManager) { 48 if (layoutManager == null) return null; 49 if (!(layoutManager instanceof GridLayoutManager)) { 50 throw new AssertionError("GridLayoutManager required."); 51 } 52 53 CarUiGridLayoutStyle layoutStyle = new CarUiGridLayoutStyle(); 54 layoutStyle.setSpanCount(((GridLayoutManager) layoutManager).getSpanCount()); 55 layoutStyle.setReverseLayout(((GridLayoutManager) layoutManager).getReverseLayout()); 56 layoutStyle.setSpanSizeLookup(((GridLayoutManager) layoutManager).getSpanSizeLookup()); 57 return layoutStyle; 58 } 59 60 /** Returns number of recyclerview spans */ getSpanCount()61 public int getSpanCount() { 62 return mSpanCount; 63 } 64 65 /** sets number of recyclerview columns */ setSpanCount(int spanCount)66 public void setSpanCount(int spanCount) { 67 if (spanCount <= 0) { 68 throw new AssertionError("Span count must be bigger than 0"); 69 } 70 mSpanCount = spanCount; 71 } 72 73 /** Returns {@link CarUiRecyclerViewLayout} */ 74 @CarUiRecyclerViewLayout getLayoutType()75 public int getLayoutType() { 76 return CarUiRecyclerViewLayout.GRID; 77 } 78 79 /** Returns layout direction {@link Orientation} */ 80 @Orientation getOrientation()81 public int getOrientation() { 82 return mLayoutOrientation; 83 } 84 85 /** sets layout direction {@link Orientation} */ setOrientation(@rientation int orientation)86 public void setOrientation(@Orientation int orientation) { 87 mLayoutOrientation = orientation; 88 } 89 90 /** Returns true if layout is reversed */ getReverseLayout()91 public boolean getReverseLayout() { 92 return mReverseLayout; 93 } 94 95 /** sets if layout is reversed */ setReverseLayout(boolean reverseLayout)96 public void setReverseLayout(boolean reverseLayout) { 97 mReverseLayout = reverseLayout; 98 } 99 100 /** Returns a wrapper {@link androidx.recyclerview.widget.GridLayoutManager.SpanSizeLookup} */ 101 @Nullable getSpanSizeLookup()102 public SpanSizeLookup getSpanSizeLookup() { 103 return mSpanSizeLookup; 104 } 105 106 /** Returns a wrapper {@link androidx.recyclerview.widget.GridLayoutManager.SpanSizeLookup} */ setSpanSizeLookup(@onNull SpanSizeLookup spanSizeLookup)107 public void setSpanSizeLookup(@NonNull SpanSizeLookup spanSizeLookup) { 108 mSpanSizeLookup = spanSizeLookup; 109 } 110 111 /** 112 * @return CarUiRecyclerView size 113 */ getSize()114 public int getSize() { 115 return mSize; 116 } 117 118 /** 119 * @param size CarUiRecyclerView size 120 */ setSize(int size)121 public void setSize(int size) { 122 mSize = size; 123 } 124 } 125