1 /* 2 * Copyright (C) 2019 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.android.customization.picker; 17 18 import static androidx.core.view.ViewCompat.LAYOUT_DIRECTION_RTL; 19 20 import android.content.Context; 21 import android.view.LayoutInflater; 22 import android.view.View; 23 import android.view.ViewGroup; 24 25 import androidx.annotation.LayoutRes; 26 import androidx.annotation.NonNull; 27 import androidx.cardview.widget.CardView; 28 import androidx.core.view.ViewCompat; 29 import androidx.viewpager.widget.PagerAdapter; 30 31 import com.android.customization.picker.BasePreviewAdapter.PreviewPage; 32 import com.android.customization.widget.PreviewPager; 33 34 import java.util.ArrayList; 35 import java.util.List; 36 37 /** 38 * Base adapter for {@link PreviewPager}. 39 * It can be used as-is by creating an extension of {@link PreviewPage} and adding pages via 40 * {@link #addPage(PreviewPage)} 41 * @param <T> the type of {@link PreviewPage} that this adapter will handle. 42 */ 43 public class BasePreviewAdapter<T extends PreviewPage> extends PagerAdapter { 44 45 private final int mPreviewCardResId; 46 private final Context mContext; 47 private final LayoutInflater mInflater; 48 protected final List<T> mPages = new ArrayList<>(); 49 BasePreviewAdapter(Context context, @LayoutRes int previewCardResId)50 protected BasePreviewAdapter(Context context, @LayoutRes int previewCardResId) { 51 mContext = context; 52 mPreviewCardResId = previewCardResId; 53 mInflater = LayoutInflater.from(mContext); 54 } 55 addPage(T p)56 protected void addPage(T p) { 57 mPages.add(p); 58 } 59 60 @Override getCount()61 public int getCount() { 62 return mPages.size(); 63 } 64 65 @Override isViewFromObject(@onNull View view, @NonNull Object object)66 public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { 67 return view == ((PreviewPage) object).card; 68 } 69 70 @NonNull 71 @Override instantiateItem(@onNull ViewGroup container, int position)72 public Object instantiateItem(@NonNull ViewGroup container, int position) { 73 if (ViewCompat.getLayoutDirection(container) == LAYOUT_DIRECTION_RTL) { 74 position = mPages.size() - 1 - position; 75 } 76 CardView card = (CardView) mInflater.inflate(mPreviewCardResId, container, false); 77 T page = mPages.get(position); 78 79 page.setCard(card); 80 page.bindPreviewContent(); 81 if (card.getParent() != null) { 82 container.removeView(card); 83 } 84 container.addView(card); 85 return page; 86 } 87 88 @Override destroyItem(@onNull ViewGroup container, int position, @NonNull Object object)89 public void destroyItem(@NonNull ViewGroup container, int position, 90 @NonNull Object object) { 91 View card = ((PreviewPage) object).card; 92 ((PreviewPage) object).card = null; 93 if (card.getParent() == container) { 94 container.removeView(card); 95 } 96 } 97 98 /** 99 * Represents a possible page in a {@link PreviewPager}, based on a CardView container. 100 * Override {@link #bindPreviewContent()} to bind the contents of the page when instantiated. 101 */ 102 public static abstract class PreviewPage { 103 protected final String title; 104 protected CardView card; 105 PreviewPage(String title)106 protected PreviewPage(String title) { 107 this.title = title; 108 } 109 setCard(CardView card)110 public void setCard(CardView card) { 111 this.card = card; 112 } 113 bindPreviewContent()114 public abstract void bindPreviewContent(); 115 } 116 } 117