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.view.LayoutInflater; 21 import android.view.View; 22 import android.view.ViewGroup; 23 24 import androidx.annotation.Nullable; 25 import androidx.appcompat.app.AppCompatActivity; 26 import androidx.fragment.app.Fragment; 27 import androidx.fragment.app.FragmentManager; 28 import androidx.recyclerview.widget.LinearLayoutManager; 29 import androidx.recyclerview.widget.RecyclerView; 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 Fragment existingFragment = fm.findFragmentById(android.R.id.content); 56 if (existingFragment == null) { 57 RecyclerViewFragment fragment = new RecyclerViewFragment(); 58 initializeRecyclerViewFragment(fragment); 59 fm.beginTransaction().add(android.R.id.content, fragment).commit(); 60 } else if (existingFragment instanceof RecyclerViewFragment) { 61 initializeRecyclerViewFragment((RecyclerViewFragment) existingFragment); 62 } 63 } 64 initializeRecyclerViewFragment(RecyclerViewFragment fragment)65 private void initializeRecyclerViewFragment(RecyclerViewFragment fragment) { 66 fragment.layoutManager = createLayoutManager(this); 67 fragment.adapter = createAdapter(); 68 } 69 createLayoutManager(Context context)70 protected RecyclerView.LayoutManager createLayoutManager(Context context) { 71 return new LinearLayoutManager(context); 72 } 73 createAdapter()74 protected abstract RecyclerView.Adapter createAdapter(); 75 } 76