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