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.GlifPreferenceLayout}. 51 * 52 * <p>Example: 53 * 54 * <pre>{@code 55 * <com.android.setupwizardlib.GlifPreferenceLayout 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(android.view.LayoutInflater, 64 * android.view.ViewGroup, android.os.Bundle)} 65 */ 66 public class GlifPreferenceLayout extends GlifRecyclerLayout { 67 GlifPreferenceLayout(Context context)68 public GlifPreferenceLayout(Context context) { 69 super(context); 70 } 71 GlifPreferenceLayout(Context context, int template, int containerId)72 public GlifPreferenceLayout(Context context, int template, int containerId) { 73 super(context, template, containerId); 74 } 75 GlifPreferenceLayout(Context context, AttributeSet attrs)76 public GlifPreferenceLayout(Context context, AttributeSet attrs) { 77 super(context, attrs); 78 } 79 GlifPreferenceLayout(Context context, AttributeSet attrs, int defStyleAttr)80 public GlifPreferenceLayout(Context context, AttributeSet attrs, int defStyleAttr) { 81 super(context, attrs, defStyleAttr); 82 } 83 84 @Override findContainer(int containerId)85 protected ViewGroup findContainer(int containerId) { 86 if (containerId == 0) { 87 containerId = R.id.suw_layout_content; 88 } 89 return super.findContainer(containerId); 90 } 91 92 /** This method must be called in {@code PreferenceFragment#onCreateRecyclerView}. */ onCreateRecyclerView( LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)93 public RecyclerView onCreateRecyclerView( 94 LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { 95 return mRecyclerMixin.getRecyclerView(); 96 } 97 98 @Override onInflateTemplate(LayoutInflater inflater, int template)99 protected View onInflateTemplate(LayoutInflater inflater, int template) { 100 if (template == 0) { 101 template = R.layout.suw_glif_preference_template; 102 } 103 return super.onInflateTemplate(inflater, template); 104 } 105 106 @Override onTemplateInflated()107 protected void onTemplateInflated() { 108 // Inflate the recycler view here, so attributes on the decoration views can be applied 109 // immediately. 110 final LayoutInflater inflater = LayoutInflater.from(getContext()); 111 RecyclerView recyclerView = 112 (RecyclerView) inflater.inflate(R.layout.suw_glif_preference_recycler_view, this, false); 113 mRecyclerMixin = new RecyclerMixin(this, recyclerView); 114 } 115 } 116