Home | History | Annotate | Download | only in viewpager2
      1 /*
      2  * Copyright (C) 2018 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.os.Bundle;
     22 import android.view.LayoutInflater;
     23 import android.view.View;
     24 import android.view.ViewGroup;
     25 
     26 import androidx.annotation.NonNull;
     27 import androidx.annotation.Nullable;
     28 import androidx.fragment.app.Fragment;
     29 import androidx.fragment.app.FragmentActivity;
     30 import androidx.fragment.app.FragmentManager;
     31 import androidx.viewpager2.widget.ViewPager2;
     32 import androidx.viewpager2.widget.ViewPager2.FragmentProvider;
     33 
     34 import com.example.androidx.viewpager2.cards.Card;
     35 import com.example.androidx.viewpager2.cards.CardView;
     36 
     37 import java.util.List;
     38 
     39 /**
     40  * Shows how to use {@link ViewPager2#setAdapter(FragmentManager, FragmentProvider, int)}
     41  *
     42  * @see CardActivity
     43  */
     44 public class CardFragmentActivity extends FragmentActivity {
     45     private static final List<Card> sCards = unmodifiableList(Card.createDeck52());
     46 
     47     @Override
     48     public void onCreate(Bundle bundle) {
     49         super.onCreate(bundle);
     50         setContentView(R.layout.activity_card_layout);
     51 
     52         this.<ViewPager2>findViewById(R.id.view_pager).setAdapter(getSupportFragmentManager(),
     53                 new FragmentProvider() {
     54                     @Override
     55                     public Fragment getItem(int position) {
     56                         return CardFragment.create(sCards.get(position));
     57                     }
     58 
     59                     @Override
     60                     public int getCount() {
     61                         return sCards.size();
     62                     }
     63                 },
     64                 ViewPager2.FragmentRetentionPolicy.SAVE_STATE);
     65     }
     66 
     67         /** {@inheritDoc} */
     68     public static class CardFragment extends Fragment {
     69         @Nullable
     70         @Override
     71         public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
     72                 @Nullable Bundle savedInstanceState) {
     73             CardView cardView = new CardView(getLayoutInflater(), container);
     74             cardView.bind(Card.fromBundle(getArguments()));
     75             return cardView.getView();
     76         }
     77 
     78         /** Creates a Fragment for a given {@link Card} */
     79         public static CardFragment create(Card card) {
     80             CardFragment fragment = new CardFragment();
     81             fragment.setArguments(card.toBundle());
     82             return fragment;
     83         }
     84     }
     85 }
     86