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 import com.android.car.ui.recyclerview.CarUiRecyclerView.Size; 27 28 /** 29 * CarUi proxy class for {@link GridLayoutManager} 30 */ 31 public final class CarUiGridLayoutStyle implements CarUiLayoutStyle { 32 33 private int mSpanCount = 1; 34 @Orientation 35 private int mLayoutOrientation = VERTICAL; 36 private boolean mReverseLayout = false; 37 @Size 38 private int mSize = CarUiRecyclerView.SIZE_LARGE; 39 @NonNull 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 @Override 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 @Override getLayoutType()74 public int getLayoutType() { 75 return CarUiRecyclerViewLayout.GRID; 76 } 77 78 @Override getOrientation()79 public int getOrientation() { 80 return mLayoutOrientation; 81 } 82 83 /** sets layout direction {@link CarUiLayoutStyle.Orientation} */ setOrientation(@rientation int orientation)84 public void setOrientation(@Orientation int orientation) { 85 mLayoutOrientation = orientation; 86 } 87 88 @Override getReverseLayout()89 public boolean getReverseLayout() { 90 return mReverseLayout; 91 } 92 93 /** sets if layout is reversed */ setReverseLayout(boolean reverseLayout)94 public void setReverseLayout(boolean reverseLayout) { 95 mReverseLayout = reverseLayout; 96 } 97 98 /** Returns a wrapper {@link androidx.recyclerview.widget.GridLayoutManager.SpanSizeLookup} */ 99 @NonNull getSpanSizeLookup()100 public SpanSizeLookup getSpanSizeLookup() { 101 return mSpanSizeLookup; 102 } 103 104 /** Returns a wrapper {@link androidx.recyclerview.widget.GridLayoutManager.SpanSizeLookup} */ setSpanSizeLookup(@onNull SpanSizeLookup spanSizeLookup)105 public void setSpanSizeLookup(@NonNull SpanSizeLookup spanSizeLookup) { 106 mSpanSizeLookup = spanSizeLookup; 107 } 108 109 @Override getSize()110 public int getSize() { 111 return mSize; 112 } 113 114 /** 115 * @param size CarUiRecyclerView size 116 */ setSize(int size)117 public void setSize(int size) { 118 mSize = size; 119 } 120 } 121