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;
29
30import com.android.test.uibench.R;
31
32public 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