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 30 import com.android.test.uibench.R; 31 32 public abstract class RvCompatListActivity extends AppCompatActivity { 33 public static class RecyclerViewFragment extends Fragment { 34 RecyclerView.LayoutManager layoutManager; 35 RecyclerView.Adapter adapter; 36 37 @Nullable 38 @Override 39 public View onCreateView(LayoutInflater inflater, ViewGroup container, 40 Bundle savedInstanceState) { 41 RecyclerView recyclerView = (RecyclerView) inflater.inflate( 42 R.layout.recycler_view, container, false); 43 recyclerView.setLayoutManager(layoutManager); 44 recyclerView.setAdapter(adapter); 45 return recyclerView; 46 } 47 } 48 49 @Override 50 protected void onCreate(Bundle savedInstanceState) { 51 super.onCreate(savedInstanceState); 52 53 FragmentManager fm = getSupportFragmentManager(); 54 if (fm.findFragmentById(android.R.id.content) == null) { 55 RecyclerViewFragment fragment = new RecyclerViewFragment(); 56 fragment.layoutManager = createLayoutManager(this); 57 fragment.adapter = createAdapter(); 58 fm.beginTransaction().add(android.R.id.content, fragment).commit(); 59 } 60 } 61 62 protected RecyclerView.LayoutManager createLayoutManager(Context context) { 63 return new LinearLayoutManager(context); 64 } 65 66 protected abstract RecyclerView.Adapter createAdapter(); 67 } 68