• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.LinearLayoutManager;
22 import android.support.v7.widget.RecyclerView;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26 
27 /**
28  * Demonstrates the use of RecyclerView with a LinearLayoutManager.
29  */
30 public class RecyclerViewFragment extends Fragment {
31 
32     private static final String TAG = "RecyclerViewFragment";
33 
34     protected RecyclerView mRecyclerView;
35     protected RecyclerView.Adapter mAdapter;
36     protected RecyclerView.LayoutManager mLayoutManager;
37     protected String[] mDataset;
38 
39     @Override
onCreate(Bundle savedInstanceState)40     public void onCreate(Bundle savedInstanceState) {
41         super.onCreate(savedInstanceState);
42 
43         // Initialize dataset, this data would usually come from a local content provider or
44         // remote server.
45         initDataset();
46     }
47 
48     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)49     public View onCreateView(LayoutInflater inflater, ViewGroup container,
50             Bundle savedInstanceState) {
51         View rootView = inflater.inflate(R.layout.recycler_view_frag, container, false);
52         rootView.setTag(TAG);
53 
54         // BEGIN_INCLUDE(initializeRecyclerView)
55         mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
56 
57         // LinearLayoutManager is used here, this will layout the elements in a similar fashion
58         // to the way ListView would layout elements. The RecyclerView.LayoutManager defines how
59         // elements are laid out.
60         mLayoutManager = new LinearLayoutManager(getActivity());
61         mRecyclerView.setLayoutManager(mLayoutManager);
62 
63         mAdapter = new CustomAdapter(mDataset);
64         // Set CustomAdapter as the adapter for RecyclerView.
65         mRecyclerView.setAdapter(mAdapter);
66         // END_INCLUDE(initializeRecyclerView)
67 
68         return rootView;
69     }
70 
71     /**
72      * Generates Strings for RecyclerView's adapter. This data would usually come
73      * from a local content provider or remote server.
74      */
initDataset()75     private void initDataset() {
76         mDataset = new String[60];
77         for (int i=0; i < 60; i++) {
78             mDataset[i] = "This is element #" + i;
79         }
80     }
81 }
82