1/* 2 * Copyright (C) 2014 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 */ 16package com.example.android.supportv7.widget; 17 18import com.example.android.supportv7.Cheeses; 19import com.example.android.supportv7.R; 20import com.example.android.supportv7.widget.adapter.SimpleStringAdapter; 21import com.example.android.supportv7.widget.util.ConfigToggle; 22 23import android.support.v4.view.ViewCompat; 24import android.support.v7.widget.GridLayoutManager; 25import android.support.v7.widget.LinearLayoutManager; 26import android.support.v7.widget.RecyclerView; 27import android.view.View; 28import android.view.ViewGroup; 29 30/** 31 * A sample Activity to demonstrate capabilities of {@link GridLayoutManager}. 32 */ 33public class GridLayoutManagerActivity extends BaseLayoutManagerActivity<GridLayoutManager> { 34 SimpleStringAdapter mAdapter; 35 @Override 36 protected GridLayoutManager createLayoutManager() { 37 GridLayoutManager lm = new GridLayoutManager(this, 3); 38 lm.setReverseLayout(true); 39 lm.setSpanSizeLookup(mSpanSizeLookup); 40 return lm; 41 } 42 43 GridLayoutManager.SpanSizeLookup mSpanSizeLookup = new GridLayoutManager.SpanSizeLookup() { 44 @Override 45 public int getSpanSize(int position) { 46 String item = mAdapter.getValueAt(position); 47 return 1 + (Math.abs(item.hashCode()) % mLayoutManager.getSpanCount()); 48 } 49 }; 50 51 @Override 52 protected ConfigToggle[] createConfigToggles() { 53 return new ConfigToggle[]{ 54 new ConfigToggle(this, R.string.checkbox_orientation) { 55 @Override 56 public boolean isChecked() { 57 return mLayoutManager.getOrientation() == LinearLayoutManager.HORIZONTAL; 58 } 59 60 @Override 61 public void onChange(boolean newValue) { 62 mLayoutManager.setOrientation(newValue ? LinearLayoutManager.HORIZONTAL 63 : LinearLayoutManager.VERTICAL); 64 } 65 }, 66 new ConfigToggle(this, R.string.checkbox_reverse) { 67 @Override 68 public boolean isChecked() { 69 return mLayoutManager.getReverseLayout(); 70 } 71 72 @Override 73 public void onChange(boolean newValue) { 74 mLayoutManager.setReverseLayout(newValue); 75 } 76 }, 77 new ConfigToggle(this, R.string.checkbox_layout_dir) { 78 @Override 79 public boolean isChecked() { 80 return ViewCompat.getLayoutDirection(mRecyclerView) == 81 ViewCompat.LAYOUT_DIRECTION_RTL; 82 } 83 84 @Override 85 public void onChange(boolean newValue) { 86 ViewCompat.setLayoutDirection(mRecyclerView, newValue ? 87 ViewCompat.LAYOUT_DIRECTION_RTL : ViewCompat.LAYOUT_DIRECTION_LTR); 88 } 89 }, 90 new ConfigToggle(this, R.string.checkbox_stack_from_end) { 91 @Override 92 public boolean isChecked() { 93 return mLayoutManager.getStackFromEnd(); 94 } 95 96 @Override 97 public void onChange(boolean newValue) { 98 mLayoutManager.setStackFromEnd(newValue); 99 } 100 } 101 }; 102 } 103 104 @Override 105 protected void scrollToPositionWithOffset(boolean smooth, int position, int offset) { 106 if (smooth) { 107 super.scrollToPositionWithOffset(smooth, position, offset); 108 } else { 109 mLayoutManager.scrollToPositionWithOffset(position, offset); 110 } 111 } 112 113 protected RecyclerView.Adapter createAdapter() { 114 mAdapter = new SimpleStringAdapter(this, Cheeses.sCheeseStrings) { 115 @Override 116 public SimpleStringAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, 117 int viewType) { 118 final SimpleStringAdapter.ViewHolder vh = super 119 .onCreateViewHolder(parent, viewType); 120 vh.itemView.setOnClickListener(new View.OnClickListener() { 121 @Override 122 public void onClick(View v) { 123 final int pos = vh.getAdapterPosition(); 124 if (pos == RecyclerView.NO_POSITION) { 125 return; 126 } 127 if (pos + 1 < getItemCount()) { 128 swap(pos, pos + 1); 129 } 130 } 131 }); 132 return vh; 133 } 134 }; 135 return mAdapter; 136 } 137} 138