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.recyclerview;
18 
19 import android.os.Bundle;
20 import android.support.v4.app.Fragment;
21 import android.support.v7.widget.GridLayoutManager;
22 import android.support.v7.widget.LinearLayoutManager;
23 import android.support.v7.widget.RecyclerView;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.widget.RadioButton;
28 
29 /**
30  * Demonstrates the use of {@link RecyclerView} with a {@link LinearLayoutManager} and a
31  * {@link GridLayoutManager}.
32  */
33 public class RecyclerViewFragment extends Fragment {
34 
35     private static final String TAG = "RecyclerViewFragment";
36     private static final String KEY_LAYOUT_MANAGER = "layoutManager";
37     private static final int SPAN_COUNT = 2;
38     private static final int DATASET_COUNT = 60;
39 
40     private enum LayoutManagerType {
41         GRID_LAYOUT_MANAGER,
42         LINEAR_LAYOUT_MANAGER
43     }
44 
45     protected LayoutManagerType mCurrentLayoutManagerType;
46 
47     protected RadioButton mLinearLayoutRadioButton;
48     protected RadioButton mGridLayoutRadioButton;
49 
50     protected RecyclerView mRecyclerView;
51     protected CustomAdapter mAdapter;
52     protected RecyclerView.LayoutManager mLayoutManager;
53     protected String[] mDataset;
54 
55     @Override
onCreate(Bundle savedInstanceState)56     public void onCreate(Bundle savedInstanceState) {
57         super.onCreate(savedInstanceState);
58 
59         // Initialize dataset, this data would usually come from a local content provider or
60         // remote server.
61         initDataset();
62     }
63 
64     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)65     public View onCreateView(LayoutInflater inflater, ViewGroup container,
66             Bundle savedInstanceState) {
67         View rootView = inflater.inflate(R.layout.recycler_view_frag, container, false);
68         rootView.setTag(TAG);
69 
70         // BEGIN_INCLUDE(initializeRecyclerView)
71         mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
72 
73         // LinearLayoutManager is used here, this will layout the elements in a similar fashion
74         // to the way ListView would layout elements. The RecyclerView.LayoutManager defines how
75         // elements are laid out.
76         mLayoutManager = new LinearLayoutManager(getActivity());
77 
78         mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
79 
80         if (savedInstanceState != null) {
81             // Restore saved layout manager type.
82             mCurrentLayoutManagerType = (LayoutManagerType) savedInstanceState
83                     .getSerializable(KEY_LAYOUT_MANAGER);
84         }
85         setRecyclerViewLayoutManager(mCurrentLayoutManagerType);
86 
87         mAdapter = new CustomAdapter(mDataset);
88         // Set CustomAdapter as the adapter for RecyclerView.
89         mRecyclerView.setAdapter(mAdapter);
90         // END_INCLUDE(initializeRecyclerView)
91 
92         mLinearLayoutRadioButton = (RadioButton) rootView.findViewById(R.id.linear_layout_rb);
93         mLinearLayoutRadioButton.setOnClickListener(new View.OnClickListener() {
94             @Override
95             public void onClick(View v) {
96                 setRecyclerViewLayoutManager(LayoutManagerType.LINEAR_LAYOUT_MANAGER);
97             }
98         });
99 
100         mGridLayoutRadioButton = (RadioButton) rootView.findViewById(R.id.grid_layout_rb);
101         mGridLayoutRadioButton.setOnClickListener(new View.OnClickListener() {
102             @Override
103             public void onClick(View v) {
104                 setRecyclerViewLayoutManager(LayoutManagerType.GRID_LAYOUT_MANAGER);
105             }
106         });
107 
108         return rootView;
109     }
110 
111     /**
112      * Set RecyclerView's LayoutManager to the one given.
113      *
114      * @param layoutManagerType Type of layout manager to switch to.
115      */
setRecyclerViewLayoutManager(LayoutManagerType layoutManagerType)116     public void setRecyclerViewLayoutManager(LayoutManagerType layoutManagerType) {
117         int scrollPosition = 0;
118 
119         // If a layout manager has already been set, get current scroll position.
120         if (mRecyclerView.getLayoutManager() != null) {
121             scrollPosition = ((LinearLayoutManager) mRecyclerView.getLayoutManager())
122                     .findFirstCompletelyVisibleItemPosition();
123         }
124 
125         switch (layoutManagerType) {
126             case GRID_LAYOUT_MANAGER:
127                 mLayoutManager = new GridLayoutManager(getActivity(), SPAN_COUNT);
128                 mCurrentLayoutManagerType = LayoutManagerType.GRID_LAYOUT_MANAGER;
129                 break;
130             case LINEAR_LAYOUT_MANAGER:
131                 mLayoutManager = new LinearLayoutManager(getActivity());
132                 mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
133                 break;
134             default:
135                 mLayoutManager = new LinearLayoutManager(getActivity());
136                 mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
137         }
138 
139         mRecyclerView.setLayoutManager(mLayoutManager);
140         mRecyclerView.scrollToPosition(scrollPosition);
141     }
142 
143     @Override
onSaveInstanceState(Bundle savedInstanceState)144     public void onSaveInstanceState(Bundle savedInstanceState) {
145         // Save currently selected layout manager.
146         savedInstanceState.putSerializable(KEY_LAYOUT_MANAGER, mCurrentLayoutManagerType);
147         super.onSaveInstanceState(savedInstanceState);
148     }
149 
150     /**
151      * Generates Strings for RecyclerView's adapter. This data would usually come
152      * from a local content provider or remote server.
153      */
initDataset()154     private void initDataset() {
155         mDataset = new String[DATASET_COUNT];
156         for (int i = 0; i < DATASET_COUNT; i++) {
157             mDataset[i] = "This is element #" + i;
158         }
159     }
160 }
161