Skip to content

Commit ef068ba

Browse files
committed
- Add position to renderer methods.
1 parent 42280f4 commit ef068ba

File tree

2 files changed

+17
-25
lines changed

2 files changed

+17
-25
lines changed

app/src/main/java/iammert/com/expandablelayout/MainActivity.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,15 @@ protected void onCreate(Bundle savedInstanceState) {
1919

2020
ExpandableLayout sectionLinearLayout = (ExpandableLayout) findViewById(R.id.el);
2121

22-
sectionLinearLayout.setParentRenderer(new ExpandableLayout.Renderer<FruitCategory>() {
22+
sectionLinearLayout.setRenderer(new ExpandableLayout.Renderer<FruitCategory, Fruit>() {
2323
@Override
24-
public void render(View view, FruitCategory model, boolean isExpanded) {
24+
public void renderParent(View view, FruitCategory model, boolean isExpanded, int parentPosition) {
2525
((TextView) view.findViewById(R.id.tvParent)).setText(model.name);
2626
view.findViewById(R.id.arrow).setBackgroundResource(isExpanded ? R.drawable.arrow_up : R.drawable.arrow_down);
2727
}
28-
});
2928

30-
sectionLinearLayout.setChildRenderer(new ExpandableLayout.Renderer<Fruit>() {
3129
@Override
32-
public void render(View view, Fruit model, boolean isExpanded) {
30+
public void renderChild(View view, Fruit model, int parentPosition, int childPosition) {
3331
((TextView) view.findViewById(R.id.tvChild)).setText(model.name);
3432
}
3533
});

expandablelib/src/main/java/iammert/com/expandablelib/ExpandableLayout.java

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222

2323
public class ExpandableLayout extends LinearLayout {
2424

25-
public interface Renderer<T> {
26-
void render(View view, T model, boolean isExpanded, int position);
25+
public interface Renderer<P, C> {
26+
void renderParent(View view, P model, boolean isExpanded, int parentPosition);
27+
28+
void renderChild(View view, C model, int parentPosition, int childPosition);
2729
}
2830

2931
private static final int NO_RES = 0;
@@ -38,9 +40,7 @@ public interface Renderer<T> {
3840
@LayoutRes
3941
private int childLayout;
4042

41-
private Renderer parentRenderer;
42-
43-
private Renderer childRenderer;
43+
private Renderer renderer;
4444

4545
private List<Section> sections;
4646

@@ -89,12 +89,8 @@ public <P> void setCollapseListener(ExpandCollapseListener.CollapseListener<P> c
8989
this.collapseListener = collapseListener;
9090
}
9191

92-
public void setParentRenderer(@NonNull Renderer renderer) {
93-
this.parentRenderer = renderer;
94-
}
95-
96-
public void setChildRenderer(@NonNull Renderer renderer) {
97-
this.childRenderer = renderer;
92+
public void setRenderer(@NonNull Renderer renderer) {
93+
this.renderer = renderer;
9894
}
9995

10096
public void addSection(@NonNull Section section) {
@@ -129,31 +125,29 @@ public <P, C> void addChildren(P parent, List<C> children) {
129125
}
130126

131127
private <C> void notifyItemAdded(int parentIndex, C child) {
132-
if (childRenderer == null) {
128+
if (renderer == null) {
133129
return;
134130
}
135131
ViewGroup parentView = (ViewGroup) getChildAt(parentIndex);
136132
View childView = layoutInflater.inflate(childLayout, null);
137-
childRenderer.render(childView, child, sections.get(parentIndex).expanded, sections.get(parentIndex).children.size() - 1);
133+
renderer.renderChild(childView, child, parentIndex, sections.get(parentIndex).children.size() - 1);
138134
parentView.addView(childView);
139135
}
140136

141137
private <C> void notifyItemAdded(int parentIndex, List<C> children) {
142-
if (childRenderer == null) {
138+
if (renderer == null) {
143139
return;
144140
}
145141
ViewGroup parentView = (ViewGroup) getChildAt(parentIndex);
146-
boolean isExpanded = sections.get(parentIndex).expanded;
147142
for (int i = 0; i < children.size(); i++) {
148143
View childView = layoutInflater.inflate(childLayout, null);
149-
childRenderer.render(childView, children.get(i), isExpanded, i);
144+
renderer.renderChild(childView, children.get(i), parentIndex, i);
150145
parentView.addView(childView);
151146
}
152147
}
153148

154149
private void notifySectionAdded(final Section section) {
155-
156-
if (parentRenderer == null || childRenderer == null)
150+
if (renderer == null)
157151
return;
158152

159153
LinearLayout sectionLayout = new LinearLayout(getContext());
@@ -171,14 +165,14 @@ public void onClick(View view) {
171165
}
172166
}
173167
});
174-
parentRenderer.render(parentView, section.parent, section.expanded, sections.size() - 1);
168+
renderer.renderParent(parentView, section.parent, section.expanded, sections.size() - 1);
175169
sectionLayout.addView(parentView);
176170

177171
if (section.expanded) {
178172
for (int i = 0; i < section.children.size(); i++) {
179173
Object child = section.children.get(i);
180174
View childView = layoutInflater.inflate(childLayout, null);
181-
childRenderer.render(childView, child, section.expanded, i);
175+
renderer.renderChild(childView, child, sections.size() - 1, i);
182176
sectionLayout.addView(childView);
183177
}
184178
}

0 commit comments

Comments
 (0)