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