Home | History | Annotate | Download | only in intentplayground
      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 package com.example.android.intentplayground;
     17 
     18 import android.content.Context;
     19 import android.view.LayoutInflater;
     20 import android.view.View;
     21 import android.view.ViewGroup;
     22 import android.widget.Button;
     23 import android.widget.TextView;
     24 
     25 import androidx.viewpager.widget.PagerAdapter;
     26 
     27 import java.util.List;
     28 
     29 /**
     30  * A {@link PagerAdapter} for {@link ShowcaseFragment} that handles the creation of Views based
     31  * on a list of {@link com.example.android.intentplayground.ShowcaseFragment.Step}.
     32  */
     33 class StepAdapter extends PagerAdapter {
     34     private final Context mContext;
     35     private View.OnClickListener mNextCallback;
     36     private View.OnClickListener mCancelCallback;
     37     private View.OnClickListener mFinishCallback;
     38     private List<ShowcaseFragment.Step> mSteps;
     39     private LayoutInflater mInflater;
     40 
     41     /**
     42      * Constructs a new StepAdapter.
     43      * @param context The context that holds this adapter.
     44      * @param steps A list of {@link com.example.android.intentplayground.ShowcaseFragment.Step}s
     45      */
     46     public StepAdapter(Context context, List<ShowcaseFragment.Step> steps) {
     47         mContext = context;
     48         mSteps = steps;
     49         mInflater = LayoutInflater.from(mContext);
     50     }
     51 
     52     /**
     53      * Set the callbacks to be run when pager buttons are clicked.
     54      * @param finish The method to run when the finish action is requested.
     55      * @param cancel The method to run when the cancel action is requested.
     56      * @param next The method to run when the next action is requested.
     57      */
     58     public void setButtonCallbacks(View.OnClickListener finish, View.OnClickListener cancel,
     59                                    View.OnClickListener next) {
     60         mFinishCallback = finish;
     61         mCancelCallback = cancel;
     62         mNextCallback = next;
     63     }
     64 
     65     @Override
     66     public Object instantiateItem(ViewGroup container, int position) {
     67         ShowcaseFragment.Step currentStep = getStep(position);
     68         ViewGroup skeletonLayout = (ViewGroup) mInflater
     69                 .inflate(R.layout.showcase_skeleton, container, false /* attachToRoot */);
     70         TextView tutorialText = skeletonLayout.findViewById(R.id.tutorial_text);
     71         tutorialText.setText(mContext.getString(currentStep.tutorialText));
     72         Button cancelButton = skeletonLayout.findViewById(R.id.cancel_pager);
     73         Button nextButton = skeletonLayout.findViewById(R.id.next_pager);
     74         if (position == getCount() - 1) {
     75             // last item, adjust button bar
     76             cancelButton.setVisibility(View.GONE);
     77             nextButton.setText(R.string.help_step_finish);
     78             nextButton.setOnClickListener(mFinishCallback);
     79         } else {
     80             cancelButton.setOnClickListener(mCancelCallback);
     81             nextButton.setOnClickListener(mNextCallback);
     82         }
     83         container.addView(skeletonLayout);
     84         return skeletonLayout;
     85     }
     86 
     87     @Override
     88     public void destroyItem(ViewGroup container, int position, Object view) {
     89         container.removeView((View) view);
     90     }
     91 
     92     public ShowcaseFragment.Step getStep(int i) {
     93         return mSteps.get(i);
     94     }
     95 
     96     @Override
     97     public int getCount() {
     98         return mSteps.size();
     99     }
    100 
    101     @Override
    102     public boolean isViewFromObject(View view, Object o) {
    103         return view.equals(o);
    104     }
    105 }
    106