1 /* 2 * Copyright (C) 2016 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.android.setupwizardlib; 18 19 import android.content.Context; 20 import android.os.Bundle; 21 import androidx.recyclerview.widget.RecyclerView; 22 import android.util.AttributeSet; 23 import android.view.LayoutInflater; 24 import android.view.View; 25 import android.view.ViewGroup; 26 import com.android.setupwizardlib.template.RecyclerMixin; 27 28 /** 29 * A layout to be used with {@code PreferenceFragment} in v14 support library. This can be specified 30 * as the {@code android:layout} in the {@code app:preferenceFragmentStyle} in {@code 31 * app:preferenceTheme}. 32 * 33 * <p>Example: 34 * 35 * <pre>{@code 36 * <style android:name="MyActivityTheme"> 37 * <item android:name="preferenceTheme">@style/MyPreferenceTheme</item> 38 * </style> 39 * 40 * <style android:name="MyPreferenceTheme"> 41 * <item android:name="preferenceFragmentStyle">@style/MyPreferenceFragmentStyle</item> 42 * </style> 43 * 44 * <style android:name="MyPreferenceFragmentStyle"> 45 * <item android:name="android:layout">@layout/my_preference_layout</item> 46 * </style> 47 * }</pre> 48 * 49 * where {@code my_preference_layout} is a layout that contains {@link 50 * com.android.setupwizardlib.SetupWizardPreferenceLayout}. 51 * 52 * <p>Example: 53 * 54 * <pre>{@code 55 * <com.android.setupwizardlib.SetupWizardPreferenceLayout 56 * xmlns:android="http://schemas.android.com/apk/res/android" 57 * android:id="@id/list_container" 58 * android:layout_width="match_parent" 59 * android:layout_height="match_parent" /> 60 * }</pre> 61 * 62 * <p>Fragments using this layout <em>must</em> delegate {@code onCreateRecyclerView} to the 63 * implementation in this class: {@link #onCreateRecyclerView} 64 */ 65 public class SetupWizardPreferenceLayout extends SetupWizardRecyclerLayout { 66 SetupWizardPreferenceLayout(Context context)67 public SetupWizardPreferenceLayout(Context context) { 68 super(context); 69 } 70 SetupWizardPreferenceLayout(Context context, int template, int containerId)71 public SetupWizardPreferenceLayout(Context context, int template, int containerId) { 72 super(context, template, containerId); 73 } 74 SetupWizardPreferenceLayout(Context context, AttributeSet attrs)75 public SetupWizardPreferenceLayout(Context context, AttributeSet attrs) { 76 super(context, attrs); 77 } 78 SetupWizardPreferenceLayout(Context context, AttributeSet attrs, int defStyleAttr)79 public SetupWizardPreferenceLayout(Context context, AttributeSet attrs, int defStyleAttr) { 80 super(context, attrs, defStyleAttr); 81 } 82 83 @Override findContainer(int containerId)84 protected ViewGroup findContainer(int containerId) { 85 if (containerId == 0) { 86 containerId = R.id.suw_layout_content; 87 } 88 return super.findContainer(containerId); 89 } 90 91 /** This method must be called in {@code PreferenceFragment#onCreateRecyclerView}. */ onCreateRecyclerView( LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)92 public RecyclerView onCreateRecyclerView( 93 LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { 94 return mRecyclerMixin.getRecyclerView(); 95 } 96 97 @Override onInflateTemplate(LayoutInflater inflater, int template)98 protected View onInflateTemplate(LayoutInflater inflater, int template) { 99 if (template == 0) { 100 template = R.layout.suw_preference_template; 101 } 102 return super.onInflateTemplate(inflater, template); 103 } 104 105 @Override onTemplateInflated()106 protected void onTemplateInflated() { 107 // Inflate the recycler view here, so attributes on the decoration views can be applied 108 // immediately. 109 final LayoutInflater inflater = LayoutInflater.from(getContext()); 110 RecyclerView recyclerView = 111 (RecyclerView) inflater.inflate(R.layout.suw_preference_recycler_view, this, false); 112 mRecyclerMixin = new RecyclerMixin(this, recyclerView); 113 } 114 } 115