PagerRecyclerViewActivity.java revision 396f55a04df1b3fdfa3e7192ce14f050aed9a6d9
1/* 2 * Copyright (C) 2016 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 17package com.example.android.supportv7.widget; 18 19import android.os.Bundle; 20import android.support.v7.app.AppCompatActivity; 21import android.support.v7.widget.LinearLayoutManager; 22import android.support.v7.widget.PagerSnapHelper; 23import android.support.v7.widget.RecyclerView; 24import android.view.ViewGroup; 25 26import com.example.android.supportv7.Cheeses; 27import com.example.android.supportv7.widget.adapter.SimpleStringAdapter; 28 29/** 30 * Example activity that uses LinearLayoutManager, RecyclerView, and PagerSnapHelper. 31 */ 32public class PagerRecyclerViewActivity extends AppCompatActivity { 33 @Override 34 protected void onCreate(Bundle savedInstanceState) { 35 super.onCreate(savedInstanceState); 36 37 final RecyclerView rv = new RecyclerView(this); 38 final LinearLayoutManager manager = 39 new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false); 40 rv.setLayoutManager(manager); 41 rv.setHasFixedSize(true); 42 rv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 43 ViewGroup.LayoutParams.MATCH_PARENT)); 44 rv.setAdapter(new SimpleStringAdapter(this, Cheeses.sCheeseStrings) { 45 @Override 46 public RecyclerView.LayoutParams getLayoutParams() { 47 return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 48 ViewGroup.LayoutParams.MATCH_PARENT); 49 } 50 }); 51 PagerSnapHelper snapHelper = new PagerSnapHelper(); 52 snapHelper.attachToRecyclerView(rv); 53 setContentView(rv); 54 } 55} 56