1 /* 2 * Copyright (C) 2020 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 17 package com.android.systemui.screenshot; 18 19 import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; 20 21 import android.app.Activity; 22 import android.graphics.Color; 23 import android.os.Bundle; 24 import android.view.View; 25 import android.view.ViewGroup; 26 import android.widget.TextView; 27 28 import androidx.annotation.Nullable; 29 30 import com.android.internal.widget.LinearLayoutManager; 31 import com.android.internal.widget.RecyclerView; 32 import com.android.internal.widget.RecyclerView.LayoutParams; 33 34 import java.util.Random; 35 36 public class RecyclerViewActivity extends Activity { 37 public static final int CHILD_VIEW_HEIGHT = 300; 38 private static final int CHILD_VIEWS = 12; 39 40 @Override onCreate(@ullable Bundle savedInstanceState)41 protected void onCreate(@Nullable Bundle savedInstanceState) { 42 super.onCreate(savedInstanceState); 43 RecyclerView recyclerView = new RecyclerView(this); 44 recyclerView.setLayoutManager(new LinearLayoutManager(this)); 45 recyclerView.setAdapter(new TestAdapter()); 46 recyclerView.setLayoutParams(new LayoutParams(MATCH_PARENT, MATCH_PARENT)); 47 setContentView(recyclerView); 48 } 49 50 static final class TestViewHolder extends RecyclerView.ViewHolder { TestViewHolder(View itemView)51 TestViewHolder(View itemView) { 52 super(itemView); 53 } 54 } 55 56 static final class TestAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { 57 private final Random mRandom = new Random(); 58 59 @Override onCreateViewHolder(ViewGroup parent, int viewType)60 public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 61 return new TestViewHolder(new TextView(parent.getContext())); 62 } 63 64 @Override onBindViewHolder(RecyclerView.ViewHolder holder, int position)65 public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 66 TextView view = (TextView) holder.itemView; 67 view.setText("Child #" + position); 68 view.setTextColor(Color.WHITE); 69 view.setTextSize(30f); 70 view.setBackgroundColor( 71 Color.rgb(mRandom.nextFloat(), mRandom.nextFloat(), mRandom.nextFloat())); 72 view.setMinHeight(CHILD_VIEW_HEIGHT); 73 } 74 75 @Override getItemCount()76 public int getItemCount() { 77 return CHILD_VIEWS; 78 } 79 } 80 } 81