• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 androidx.viewpager2.widget.swipe;
18 
19 import static org.hamcrest.Matchers.allOf;
20 import static org.hamcrest.Matchers.greaterThanOrEqualTo;
21 import static org.hamcrest.Matchers.lessThanOrEqualTo;
22 import static org.junit.Assert.assertThat;
23 
24 import androidx.fragment.app.Fragment;
25 import androidx.viewpager2.widget.ViewPager2;
26 
27 import java.util.Random;
28 import java.util.concurrent.atomic.AtomicInteger;
29 
30 public class FragmentAdapterActivity extends BaseActivity {
31     private static final Random RANDOM = new Random();
32 
33     private AtomicInteger mAttachCount = new AtomicInteger(0);
34     private AtomicInteger mDestroyCount = new AtomicInteger(0);
35     private PageFragment[] mFragments;
36 
37     @Override
setAdapter()38     protected void setAdapter() {
39         mFragments = new PageFragment[mTotalPages];
40 
41         ViewPager2.FragmentProvider fragmentProvider = new ViewPager2.FragmentProvider() {
42             final boolean[] mWasEverAttached = new boolean[mTotalPages];
43 
44             @Override
45             public Fragment getItem(final int position) {
46                 PageFragment fragment = PageFragment.create(valueForPosition(position));
47 
48                 fragment.mOnAttachListener = new PageFragment.EventListener() {
49                     @Override
50                     public void onEvent(PageFragment fragment) {
51                         mAttachCount.incrementAndGet();
52                         mWasEverAttached[position] = true;
53                     }
54                 };
55 
56                 fragment.mOnDestroyListener = new PageFragment.EventListener() {
57                     @Override
58                     public void onEvent(PageFragment fragment) {
59                         mDestroyCount.incrementAndGet();
60                     }
61                 };
62 
63                 return mFragments[position] = fragment;
64             }
65 
66             private int valueForPosition(int position) {
67                 // only supply correct value ones; then rely on it being kept by Fragment state
68                 return mWasEverAttached[position]
69                         ? RANDOM.nextInt() // junk value to be overridden by state saved value
70                         : position;
71             }
72 
73             @Override
74             public int getCount() {
75                 return mTotalPages;
76             }
77         };
78 
79         mViewPager.setAdapter(getSupportFragmentManager(), fragmentProvider,
80                 ViewPager2.FragmentRetentionPolicy.SAVE_STATE);
81     }
82 
83     @Override
updatePage(int pageIx, int newValue)84     public void updatePage(int pageIx, int newValue) {
85         mFragments[pageIx].updateValue(newValue);
86     }
87 
88     @Override
validateState()89     public void validateState() {
90         assertThat(mAttachCount.get() - mDestroyCount.get(),
91                 allOf(greaterThanOrEqualTo(1), lessThanOrEqualTo(4)));
92     }
93 }
94