1 /* 2 * Copyright (C) 2017 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.viewpager2; 18 19 import static java.util.Collections.unmodifiableList; 20 21 import android.app.Activity; 22 import android.os.Bundle; 23 import android.view.ViewGroup; 24 25 import androidx.annotation.NonNull; 26 import androidx.recyclerview.widget.RecyclerView; 27 import androidx.viewpager2.widget.ViewPager2; 28 29 import com.example.androidx.viewpager2.cards.Card; 30 import com.example.androidx.viewpager2.cards.CardView; 31 32 import java.util.List; 33 34 /** 35 * Shows how to use {@link ViewPager2#setAdapter(RecyclerView.Adapter)} 36 * 37 * @see CardFragmentActivity 38 */ 39 public class CardActivity extends Activity { 40 private static final List<Card> sCards = unmodifiableList(Card.createDeck52()); 41 42 @Override 43 public void onCreate(Bundle bundle) { 44 super.onCreate(bundle); 45 setContentView(R.layout.activity_card_layout); 46 47 this.<ViewPager2>findViewById(R.id.view_pager).setAdapter( 48 new RecyclerView.Adapter<CardViewHolder>() { 49 @NonNull 50 public CardViewHolder onCreateViewHolder(@NonNull ViewGroup parent, 51 int viewType) { 52 return new CardViewHolder(new CardView(getLayoutInflater(), parent)); 53 } 54 55 @Override 56 public void onBindViewHolder(@NonNull CardViewHolder holder, int position) { 57 holder.bind(sCards.get(position)); 58 } 59 60 @Override 61 public int getItemCount() { 62 return sCards.size(); 63 } 64 }); 65 } 66 67 /** @inheritDoc */ 68 public static class CardViewHolder extends RecyclerView.ViewHolder { 69 private final CardView mCardView; 70 71 /** {@inheritDoc} */ 72 public CardViewHolder(CardView cardView) { 73 super(cardView.getView()); 74 mCardView = cardView; 75 } 76 77 /** @see CardView#bind(Card) */ 78 public void bind(Card card) { 79 mCardView.bind(card); 80 } 81 } 82 } 83