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