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 */ 16package com.android.test.uibench.recyclerview; 17 18import android.content.Context; 19import android.os.Bundle; 20import android.support.annotation.Nullable; 21import android.support.v4.app.Fragment; 22import android.support.v4.app.FragmentManager; 23import android.support.v7.app.AppCompatActivity; 24import android.support.v7.widget.LinearLayoutManager; 25import android.support.v7.widget.RecyclerView; 26import android.view.LayoutInflater; 27import android.view.View; 28import android.view.ViewGroup; 29import android.widget.ArrayAdapter; 30 31import com.android.test.uibench.R; 32 33public 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 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 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 63 protected RecyclerView.LayoutManager createLayoutManager(Context context) { 64 return new LinearLayoutManager(context); 65 } 66 67 protected abstract RecyclerView.Adapter createAdapter(); 68} 69