Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/InnerNode.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/InnerNode.java b/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/InnerNode.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8c0078ae9007e8e6ce91dd69738dc4b2dede56a6 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/InnerNode.java |
| @@ -0,0 +1,90 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.ntp.cards; |
| + |
| +import org.chromium.chrome.browser.ntp.snippets.SnippetArticle; |
| + |
| +import java.util.List; |
| + |
| +/** |
| + * An inner node in the tree: the root of a subtree, with a list of child nodes. |
| + */ |
| +public abstract class InnerNode extends ChildNode implements NodeParent { |
| + public InnerNode(NodeParent parent) { |
| + super(parent); |
| + } |
| + |
| + protected abstract List<TreeNode> getChildren(); |
| + |
| + int getChildIndexForPosition(int position) { |
|
PEConn
2016/10/13 13:48:56
Is this meant to be package private?
Bernhard Bauer
2016/10/13 13:54:37
Yes, I'm calling it from NewTabPageAdapter to expo
|
| + List<TreeNode> children = getChildren(); |
| + int numItems = 0; |
| + int numChildren = children.size(); |
| + for (int i = 0; i < numChildren; i++) { |
| + numItems += children.get(i).getItemCount(); |
| + if (position < numItems) return i; |
| + } |
| + return -1; |
| + } |
| + |
| + private int getStartingOffsetForChildIndex(int childIndex) { |
| + List<TreeNode> children = getChildren(); |
| + int offset = 0; |
| + for (int i = 0; i < childIndex; i++) { |
| + offset += children.get(i).getItemCount(); |
| + } |
| + return offset; |
| + } |
| + |
| + int getStartingOffsetForChild(TreeNode child) { |
| + return getStartingOffsetForChildIndex(getChildren().indexOf(child)); |
| + } |
| + |
| + @Override |
| + public int getItemCount() { |
| + int numItems = 0; |
| + for (TreeNode child : getChildren()) { |
| + numItems += child.getItemCount(); |
|
PEConn
2016/10/13 13:48:56
Can we cache this?
Bernhard Bauer
2016/10/13 13:54:37
I'm planning to do that in one of the next CLs, it
|
| + } |
| + return numItems; |
| + } |
| + |
| + @Override |
| + @ItemViewType |
| + public int getItemViewType(int position) { |
| + int index = getChildIndexForPosition(position); |
| + return getChildren().get(index).getItemViewType( |
| + position - getStartingOffsetForChildIndex(index)); |
| + } |
| + |
| + @Override |
| + public void onBindViewHolder(NewTabPageViewHolder holder, int position) { |
| + int index = getChildIndexForPosition(position); |
| + getChildren().get(index).onBindViewHolder( |
| + holder, position - getStartingOffsetForChildIndex(index)); |
| + } |
| + |
| + @Override |
| + public SnippetArticle getSuggestionAt(int position) { |
| + int index = getChildIndexForPosition(position); |
| + return getChildren().get(index).getSuggestionAt( |
| + position - getStartingOffsetForChildIndex(index)); |
| + } |
| + |
| + @Override |
| + public void onItemRangeChanged(TreeNode child, int index, int count) { |
| + notifyItemRangeChanged(getStartingOffsetForChild(child) + index, count); |
| + } |
| + |
| + @Override |
| + public void onItemRangeInserted(TreeNode child, int index, int count) { |
| + notifyItemRangeInserted(getStartingOffsetForChild(child) + index, count); |
| + } |
| + |
| + @Override |
| + public void onItemRangeRemoved(TreeNode child, int index, int count) { |
| + notifyItemRangeRemoved(getStartingOffsetForChild(child) + index, count); |
| + } |
| +} |