blob: c7e247c0ca49e0c33f7f7063ef0b47ed29e3b405 [file] [log] [blame]
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package android.support.v7.preference;
import android.support.annotation.IdRes;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
/**
* A {@link android.support.v7.widget.RecyclerView.ViewHolder} class which caches views associated
* with the default {@link Preference} layouts. Cached views can be retrieved by calling
* {@link #findViewById(int)}.
*/
public class PreferenceViewHolder extends RecyclerView.ViewHolder {
private final SparseArray<View> mCachedViews = new SparseArray<>(4);
/* package */ PreferenceViewHolder(View itemView) {
super(itemView);
// Pre-cache the views that we know in advance we'll want to find
mCachedViews.put(android.R.id.title, itemView.findViewById(android.R.id.title));
mCachedViews.put(android.R.id.summary, itemView.findViewById(android.R.id.summary));
mCachedViews.put(android.R.id.icon, itemView.findViewById(android.R.id.icon));
mCachedViews.put(R.id.icon_frame, itemView.findViewById(R.id.icon_frame));
}
/**
* Returns a cached reference to a subview managed by this object. If the view reference is not
* yet cached, it falls back to calling {@link View#findViewById(int)} and caches the result.
*
* @param id Resource ID of the view to find
* @return The view, or null if no view with the requested ID is found.
*/
public View findViewById(@IdRes int id) {
final View cachedView = mCachedViews.get(id);
if (cachedView != null) {
return cachedView;
} else {
final View v = itemView.findViewById(id);
if (v != null) {
mCachedViews.put(id, v);
}
return v;
}
}
}