GuidedActionsStylist: support different viewTypes and LayoutIds
Bug 25387934
Change-Id: I1ebc652b2de4d162284e111fe2a095ab3d37158d
diff --git a/v17/leanback/api/current.txt b/v17/leanback/api/current.txt
index 4897af6..37f1ed4 100644
--- a/v17/leanback/api/current.txt
+++ b/v17/leanback/api/current.txt
@@ -1062,18 +1062,22 @@
public class GuidedActionsStylist implements android.support.v17.leanback.widget.FragmentAnimationProvider {
ctor public GuidedActionsStylist();
method public android.support.v17.leanback.widget.VerticalGridView getActionsGridView();
+ method public int getItemViewType(android.support.v17.leanback.widget.GuidedAction);
method public void onAnimateItemChecked(android.support.v17.leanback.widget.GuidedActionsStylist.ViewHolder, boolean);
method public void onAnimateItemFocused(android.support.v17.leanback.widget.GuidedActionsStylist.ViewHolder, boolean);
method public void onAnimateItemPressed(android.support.v17.leanback.widget.GuidedActionsStylist.ViewHolder, boolean);
method public void onBindViewHolder(android.support.v17.leanback.widget.GuidedActionsStylist.ViewHolder, android.support.v17.leanback.widget.GuidedAction);
method public android.view.View onCreateView(android.view.LayoutInflater, android.view.ViewGroup);
method public android.support.v17.leanback.widget.GuidedActionsStylist.ViewHolder onCreateViewHolder(android.view.ViewGroup);
+ method public android.support.v17.leanback.widget.GuidedActionsStylist.ViewHolder onCreateViewHolder(android.view.ViewGroup, int);
method protected void onEditingModeChange(android.support.v17.leanback.widget.GuidedActionsStylist.ViewHolder, android.support.v17.leanback.widget.GuidedAction, boolean);
method public void onImeAppearing(java.util.List<android.animation.Animator>);
method public void onImeDisappearing(java.util.List<android.animation.Animator>);
method public int onProvideItemLayoutId();
+ method public int onProvideItemLayoutId(int);
method public int onProvideLayoutId();
method public void setEditingMode(android.support.v17.leanback.widget.GuidedActionsStylist.ViewHolder, android.support.v17.leanback.widget.GuidedAction, boolean);
+ field public static final int VIEW_TYPE_DEFAULT = 0; // 0x0
field protected android.support.v17.leanback.widget.VerticalGridView mActionsGridView;
field protected android.view.View mMainView;
field protected android.view.View mSelectorView;
diff --git a/v17/leanback/src/android/support/v17/leanback/app/GuidedActionAdapter.java b/v17/leanback/src/android/support/v17/leanback/app/GuidedActionAdapter.java
index 52323a5..719ff9f 100644
--- a/v17/leanback/src/android/support/v17/leanback/app/GuidedActionAdapter.java
+++ b/v17/leanback/src/android/support/v17/leanback/app/GuidedActionAdapter.java
@@ -233,6 +233,14 @@
* {@inheritDoc}
*/
@Override
+ public int getItemViewType(int position) {
+ return mStylist.getItemViewType(mActions.get(position));
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
mRecyclerView = null;
}
@@ -242,7 +250,7 @@
*/
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
- GuidedActionsStylist.ViewHolder vh = mStylist.onCreateViewHolder(parent);
+ GuidedActionsStylist.ViewHolder vh = mStylist.onCreateViewHolder(parent, viewType);
View v = vh.view;
v.setOnKeyListener(mActionOnKeyListener);
v.setOnClickListener(mOnClickListener);
diff --git a/v17/leanback/src/android/support/v17/leanback/widget/GuidedAction.java b/v17/leanback/src/android/support/v17/leanback/widget/GuidedAction.java
index b3043a1..4031677 100644
--- a/v17/leanback/src/android/support/v17/leanback/widget/GuidedAction.java
+++ b/v17/leanback/src/android/support/v17/leanback/widget/GuidedAction.java
@@ -292,7 +292,7 @@
}
/**
- * Indicates whether this action is editable. Note: Editable actions cannot also be
+ * Indicates whether this action title is editable. Note: Editable actions cannot also be
* checked, or belong to a check set.
* @param editable Whether this action is editable.
*/
@@ -539,8 +539,8 @@
}
/**
- * Returns whether this action is editable.
- * @return true if the action is editable, false otherwise.
+ * Returns whether this action title is editable.
+ * @return true if the action title is editable, false otherwise.
*/
public boolean isEditable() {
return mEditable;
diff --git a/v17/leanback/src/android/support/v17/leanback/widget/GuidedActionsStylist.java b/v17/leanback/src/android/support/v17/leanback/widget/GuidedActionsStylist.java
index 69aa1b0..fc43d53 100644
--- a/v17/leanback/src/android/support/v17/leanback/widget/GuidedActionsStylist.java
+++ b/v17/leanback/src/android/support/v17/leanback/widget/GuidedActionsStylist.java
@@ -119,6 +119,14 @@
public class GuidedActionsStylist implements FragmentAnimationProvider {
/**
+ * Default viewType that associated with default layout Id for the action item.
+ * @see #getItemViewType(GuidedAction)
+ * @see #onProvideItemLayoutId(int)
+ * @see #onCreateViewHolder(ViewGroup, int)
+ */
+ public static final int VIEW_TYPE_DEFAULT = 0;
+
+ /**
* ViewHolder caches information about the action item layouts' subviews. Subclasses of {@link
* GuidedActionsStylist} may also wish to subclass this in order to add fields.
* @see GuidedAction
@@ -353,6 +361,16 @@
}
/**
+ * Return view type of action, each different type can have differently associated layout Id.
+ * Default implementation returns {@link #VIEW_TYPE_DEFAULT}.
+ * @param action The action object.
+ * @return View type that used in {@link #onProvideItemLayoutId(int)}.
+ */
+ public int getItemViewType(GuidedAction action) {
+ return VIEW_TYPE_DEFAULT;
+ }
+
+ /**
* Provides the resource ID of the layout defining the view for an individual guided actions.
* Subclasses may override to provide their own customized layouts. The base implementation
* returns {@link android.support.v17.leanback.R.layout#lb_guidedactions_item}. If overridden,
@@ -360,7 +378,8 @@
* the base class; this can be achieved by starting with a copy of the base layout file. Note
* that in order for the item to support editing, the title view should both subclass {@link
* android.widget.EditText} and implement {@link ImeKeyMonitor}; see {@link
- * GuidedActionEditText}.
+ * GuidedActionEditText}. To support different types of Layouts, override {@link
+ * #onProvideItemLayoutId(int)}.
* @return The resource ID of the layout to be inflated to define the view to display an
* individual GuidedAction.
*/
@@ -369,8 +388,31 @@
}
/**
+ * Provides the resource ID of the layout defining the view for an individual guided actions.
+ * Subclasses may override to provide their own customized layouts. The base implementation
+ * returns {@link android.support.v17.leanback.R.layout#lb_guidedactions_item}. If overridden,
+ * the substituted layout should contain matching IDs for any views that should be managed by
+ * the base class; this can be achieved by starting with a copy of the base layout file. Note
+ * that in order for the item to support editing, the title view should both subclass {@link
+ * android.widget.EditText} and implement {@link ImeKeyMonitor}; see {@link
+ * GuidedActionEditText}.
+ * @param viewType View type returned by {@link #getItemViewType(GuidedAction)}
+ * @return The resource ID of the layout to be inflated to define the view to display an
+ * individual GuidedAction.
+ */
+ public int onProvideItemLayoutId(int viewType) {
+ if (viewType == VIEW_TYPE_DEFAULT) {
+ return onProvideItemLayoutId();
+ } else {
+ throw new RuntimeException("ViewType " + viewType +
+ " not supported in GuidedActionsStylist");
+ }
+ }
+
+ /**
* Constructs a {@link ViewHolder} capable of representing {@link GuidedAction}s. Subclasses
- * may choose to return a subclass of ViewHolder.
+ * may choose to return a subclass of ViewHolder. To support different view types, override
+ * {@link #onCreateViewHolder(ViewGroup, int)}
* <p>
* <i>Note: Should not actually add the created view to the parent; the caller will do
* this.</i>
@@ -384,6 +426,25 @@
}
/**
+ * Constructs a {@link ViewHolder} capable of representing {@link GuidedAction}s. Subclasses
+ * may choose to return a subclass of ViewHolder.
+ * <p>
+ * <i>Note: Should not actually add the created view to the parent; the caller will do
+ * this.</i>
+ * @param parent The view group to be used as the parent of the new view.
+ * @param viewType The viewType returned by {@link #getItemViewType(GuidedAction)}
+ * @return The view to be added to the caller's view hierarchy.
+ */
+ public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
+ if (viewType == VIEW_TYPE_DEFAULT) {
+ return onCreateViewHolder(parent);
+ }
+ LayoutInflater inflater = LayoutInflater.from(parent.getContext());
+ View v = inflater.inflate(onProvideItemLayoutId(viewType), parent, false);
+ return new ViewHolder(v);
+ }
+
+ /**
* Binds a {@link ViewHolder} to a particular {@link GuidedAction}.
* @param vh The view holder to be associated with the given action.
* @param action The guided action to be displayed by the view holder's view.