OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.ntp.cards; |
| 6 |
| 7 import org.chromium.chrome.browser.ntp.snippets.SnippetArticle; |
| 8 |
| 9 import java.util.List; |
| 10 |
| 11 /** |
| 12 * An inner node in the tree: the root of a subtree, with a list of child nodes. |
| 13 */ |
| 14 public abstract class InnerNode extends ChildNode implements NodeParent { |
| 15 public InnerNode(NodeParent parent) { |
| 16 super(parent); |
| 17 } |
| 18 |
| 19 protected abstract List<TreeNode> getChildren(); |
| 20 |
| 21 int getChildIndexForPosition(int position) { |
| 22 List<TreeNode> children = getChildren(); |
| 23 int numItems = 0; |
| 24 int numChildren = children.size(); |
| 25 for (int i = 0; i < numChildren; i++) { |
| 26 numItems += children.get(i).getItemCount(); |
| 27 if (position < numItems) return i; |
| 28 } |
| 29 return -1; |
| 30 } |
| 31 |
| 32 private int getStartingOffsetForChildIndex(int childIndex) { |
| 33 List<TreeNode> children = getChildren(); |
| 34 int offset = 0; |
| 35 for (int i = 0; i < childIndex; i++) { |
| 36 offset += children.get(i).getItemCount(); |
| 37 } |
| 38 return offset; |
| 39 } |
| 40 |
| 41 int getStartingOffsetForChild(TreeNode child) { |
| 42 return getStartingOffsetForChildIndex(getChildren().indexOf(child)); |
| 43 } |
| 44 |
| 45 @Override |
| 46 public int getItemCount() { |
| 47 int numItems = 0; |
| 48 for (TreeNode child : getChildren()) { |
| 49 numItems += child.getItemCount(); |
| 50 } |
| 51 return numItems; |
| 52 } |
| 53 |
| 54 @Override |
| 55 @ItemViewType |
| 56 public int getItemViewType(int position) { |
| 57 int index = getChildIndexForPosition(position); |
| 58 return getChildren().get(index).getItemViewType( |
| 59 position - getStartingOffsetForChildIndex(index)); |
| 60 } |
| 61 |
| 62 @Override |
| 63 public void onBindViewHolder(NewTabPageViewHolder holder, int position) { |
| 64 int index = getChildIndexForPosition(position); |
| 65 getChildren().get(index).onBindViewHolder( |
| 66 holder, position - getStartingOffsetForChildIndex(index)); |
| 67 } |
| 68 |
| 69 @Override |
| 70 public SnippetArticle getSuggestionAt(int position) { |
| 71 int index = getChildIndexForPosition(position); |
| 72 return getChildren().get(index).getSuggestionAt( |
| 73 position - getStartingOffsetForChildIndex(index)); |
| 74 } |
| 75 |
| 76 @Override |
| 77 public void onItemRangeChanged(TreeNode child, int index, int count) { |
| 78 notifyItemRangeChanged(getStartingOffsetForChild(child) + index, count); |
| 79 } |
| 80 |
| 81 @Override |
| 82 public void onItemRangeInserted(TreeNode child, int index, int count) { |
| 83 notifyItemRangeInserted(getStartingOffsetForChild(child) + index, count)
; |
| 84 } |
| 85 |
| 86 @Override |
| 87 public void onItemRangeRemoved(TreeNode child, int index, int count) { |
| 88 notifyItemRangeRemoved(getStartingOffsetForChild(child) + index, count); |
| 89 } |
| 90 } |
OLD | NEW |