Home | History | Annotate | Download | only in swipe
      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 android.content.Context;
     20 import android.os.Bundle;
     21 import android.view.LayoutInflater;
     22 import android.view.View;
     23 import android.view.ViewGroup;
     24 import android.widget.TextView;
     25 
     26 import androidx.annotation.NonNull;
     27 import androidx.annotation.Nullable;
     28 import androidx.fragment.app.Fragment;
     29 import androidx.viewpager2.test.R;
     30 
     31 public class PageFragment extends Fragment {
     32     private static final String ARG_VALUE = "value";
     33 
     34     private int mValue;
     35     EventListener mOnAttachListener = EventListener.NO_OP;
     36     EventListener mOnDestroyListener = EventListener.NO_OP;
     37 
     38     static PageFragment create(int value) {
     39         PageFragment result = new PageFragment();
     40         Bundle args = new Bundle(1);
     41         args.putInt(ARG_VALUE, value);
     42         result.setArguments(args);
     43         return result;
     44     }
     45 
     46     @NonNull
     47     @Override
     48     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
     49             @Nullable Bundle savedInstanceState) {
     50         return inflater.inflate(R.layout.item_test_layout, container, false);
     51     }
     52 
     53     @Override
     54     public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
     55         Bundle data = savedInstanceState != null ? savedInstanceState : getArguments();
     56         updateValue(data.getInt(ARG_VALUE));
     57     }
     58 
     59     @Override
     60     public void onAttach(Context context) {
     61         super.onAttach(context);
     62         mOnAttachListener.onEvent(this);
     63     }
     64 
     65     @Override
     66     public void onSaveInstanceState(@NonNull Bundle outState) {
     67         outState.putInt(ARG_VALUE, mValue);
     68     }
     69 
     70     @Override
     71     public void onDestroy() {
     72         super.onDestroy();
     73         mOnDestroyListener.onEvent(this);
     74     }
     75 
     76     void updateValue(int newValue) {
     77         mValue = newValue;
     78         ((TextView) getView()).setText(String.valueOf(newValue));
     79     }
     80 
     81     public interface EventListener {
     82         void onEvent(PageFragment fragment);
     83 
     84         EventListener NO_OP = new EventListener() {
     85             @Override
     86             public void onEvent(PageFragment fragment) {
     87                 // do nothing
     88             }
     89         };
     90     }
     91 }
     92