• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 package com.android.test.uibench.recyclerview;
17 
18 import android.content.Context;
19 import android.os.Bundle;
20 import android.support.annotation.Nullable;
21 import android.support.v4.app.Fragment;
22 import android.support.v4.app.FragmentManager;
23 import android.support.v7.app.AppCompatActivity;
24 import android.support.v7.widget.LinearLayoutManager;
25 import android.support.v7.widget.RecyclerView;
26 import android.view.LayoutInflater;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.widget.ArrayAdapter;
30 
31 import com.android.test.uibench.R;
32 
33 public abstract class RvCompatListActivity extends AppCompatActivity {
34     public static class RecyclerViewFragment extends Fragment {
35         RecyclerView.LayoutManager layoutManager;
36         RecyclerView.Adapter adapter;
37 
38         @Nullable
39         @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)40         public View onCreateView(LayoutInflater inflater, ViewGroup container,
41                 Bundle savedInstanceState) {
42             RecyclerView recyclerView = (RecyclerView) inflater.inflate(
43                     R.layout.recycler_view, container, false);
44             recyclerView.setLayoutManager(layoutManager);
45             recyclerView.setAdapter(adapter);
46             return recyclerView;
47         }
48     }
49 
50     @Override
onCreate(Bundle savedInstanceState)51     protected void onCreate(Bundle savedInstanceState) {
52         super.onCreate(savedInstanceState);
53 
54         FragmentManager fm = getSupportFragmentManager();
55         if (fm.findFragmentById(android.R.id.content) == null) {
56             RecyclerViewFragment fragment = new RecyclerViewFragment();
57             fragment.layoutManager = createLayoutManager(this);
58             fragment.adapter = createAdapter();
59             fm.beginTransaction().add(android.R.id.content, fragment).commit();
60         }
61     }
62 
createLayoutManager(Context context)63     protected RecyclerView.LayoutManager createLayoutManager(Context context) {
64         return new LinearLayoutManager(context);
65     }
66 
createAdapter()67     protected abstract RecyclerView.Adapter createAdapter();
68 }
69