| /* |
| * Copyright (C) 2014 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package com.example.android.recyclerview; |
| |
| import android.os.Bundle; |
| import android.support.v4.app.Fragment; |
| import android.support.v7.widget.LinearLayoutManager; |
| import android.support.v7.widget.RecyclerView; |
| import android.view.LayoutInflater; |
| import android.view.View; |
| import android.view.ViewGroup; |
| |
| /** |
| * Demonstrates the use of RecyclerView with a LinearLayoutManager. |
| */ |
| public class RecyclerViewFragment extends Fragment { |
| |
| private static final String TAG = "RecyclerViewFragment"; |
| |
| protected RecyclerView mRecyclerView; |
| protected RecyclerView.Adapter mAdapter; |
| protected RecyclerView.LayoutManager mLayoutManager; |
| protected String[] mDataset; |
| |
| @Override |
| public void onCreate(Bundle savedInstanceState) { |
| super.onCreate(savedInstanceState); |
| |
| // Initialize dataset, this data would usually come from a local content provider or |
| // remote server. |
| initDataset(); |
| } |
| |
| @Override |
| public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| Bundle savedInstanceState) { |
| View rootView = inflater.inflate(R.layout.recycler_view_frag, container, false); |
| rootView.setTag(TAG); |
| |
| // BEGIN_INCLUDE(initializeRecyclerView) |
| mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView); |
| |
| // LinearLayoutManager is used here, this will layout the elements in a similar fashion |
| // to the way ListView would layout elements. The RecyclerView.LayoutManager defines how |
| // elements are laid out. |
| mLayoutManager = new LinearLayoutManager(getActivity()); |
| mRecyclerView.setLayoutManager(mLayoutManager); |
| |
| mAdapter = new CustomAdapter(mDataset); |
| // Set CustomAdapter as the adapter for RecyclerView. |
| mRecyclerView.setAdapter(mAdapter); |
| // END_INCLUDE(initializeRecyclerView) |
| |
| return rootView; |
| } |
| |
| /** |
| * Generates Strings for RecyclerView's adapter. This data would usually come |
| * from a local content provider or remote server. |
| */ |
| private void initDataset() { |
| mDataset = new String[60]; |
| for (int i=0; i < 60; i++) { |
| mDataset[i] = "This is element #" + i; |
| } |
| } |
| } |