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 */ 16 17 package com.example.android.supportv7.widget; 18 19 import android.support.v4.view.ViewCompat; 20 import android.support.v7.widget.DividerItemDecoration; 21 import android.support.v7.widget.LinearLayoutManager; 22 import android.support.v7.widget.LinearSnapHelper; 23 import android.support.v7.widget.RecyclerView; 24 25 import com.example.android.supportv7.R; 26 import com.example.android.supportv7.widget.util.ConfigToggle; 27 28 /** 29 * A sample activity that uses {@link LinearLayoutManager}. 30 */ 31 public class LinearLayoutManagerActivity extends BaseLayoutManagerActivity<LinearLayoutManager> { 32 private DividerItemDecoration mDividerItemDecoration; 33 private LinearSnapHelper mLinearSnapHelper; 34 private boolean mSnapHelperAttached; 35 36 @Override 37 protected LinearLayoutManager createLayoutManager() { 38 return new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false); 39 } 40 41 @Override 42 protected void onRecyclerViewInit(RecyclerView recyclerView) { 43 mDividerItemDecoration = new DividerItemDecoration(this, mLayoutManager.getOrientation()); 44 recyclerView.addItemDecoration(mDividerItemDecoration); 45 mLinearSnapHelper = new LinearSnapHelper(); 46 } 47 48 @Override 49 protected ConfigToggle[] createConfigToggles() { 50 return new ConfigToggle[]{ 51 new ConfigToggle(this, R.string.checkbox_orientation) { 52 @Override 53 public boolean isChecked() { 54 return mLayoutManager.getOrientation() == LinearLayoutManager.HORIZONTAL; 55 } 56 57 @Override 58 public void onChange(boolean newValue) { 59 mLayoutManager.setOrientation(newValue ? LinearLayoutManager.HORIZONTAL 60 : LinearLayoutManager.VERTICAL); 61 if (mDividerItemDecoration != null) { 62 mDividerItemDecoration.setOrientation(mLayoutManager.getOrientation()); 63 } 64 65 } 66 }, 67 new ConfigToggle(this, R.string.checkbox_reverse) { 68 @Override 69 public boolean isChecked() { 70 return mLayoutManager.getReverseLayout(); 71 } 72 73 @Override 74 public void onChange(boolean newValue) { 75 mLayoutManager.setReverseLayout(newValue); 76 } 77 }, 78 new ConfigToggle(this, R.string.checkbox_layout_dir) { 79 @Override 80 public boolean isChecked() { 81 return ViewCompat.getLayoutDirection(mRecyclerView) == 82 ViewCompat.LAYOUT_DIRECTION_RTL; 83 } 84 85 @Override 86 public void onChange(boolean newValue) { 87 ViewCompat.setLayoutDirection(mRecyclerView, newValue ? 88 ViewCompat.LAYOUT_DIRECTION_RTL : ViewCompat.LAYOUT_DIRECTION_LTR); 89 } 90 }, 91 new ConfigToggle(this, R.string.checkbox_stack_from_end) { 92 @Override 93 public boolean isChecked() { 94 return mLayoutManager.getStackFromEnd(); 95 } 96 97 @Override 98 public void onChange(boolean newValue) { 99 mLayoutManager.setStackFromEnd(newValue); 100 } 101 }, 102 new ConfigToggle(this, R.string.checkbox_snap) { 103 @Override 104 public boolean isChecked() { 105 return mSnapHelperAttached; 106 } 107 108 @Override 109 public void onChange(boolean newValue) { 110 mLinearSnapHelper.attachToRecyclerView(newValue ? mRecyclerView : null); 111 mSnapHelperAttached = newValue; 112 } 113 } 114 }; 115 } 116 } 117