Skip to content

Commit 162104e

Browse files
committed
added possibility to update changed child in expandable RV
1 parent 90ca037 commit 162104e

File tree

14 files changed

+271
-72
lines changed

14 files changed

+271
-72
lines changed

.idea/codeStyles/Project.xml

Lines changed: 109 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ dependencies {
3333
# Usage
3434
Example of usage in xml layout
3535
```
36-
com.omega_r.libs.omegarecyclerview.OmegaRecyclerView
36+
<com.omega_r.libs.omegarecyclerview.OmegaRecyclerView
3737
android:id="@+id/custom_recycler_view"
3838
android:layout_width="match_parent"
3939
android:layout_height="match_parent"
@@ -306,7 +306,7 @@ Adding OmegaExpandableRecyclerView to layout:
306306

307307
Create adapter like following:
308308
```
309-
public class ExpandableAdapter extends OmegaExpandableRecyclerView.Adapter<QuoteGlobalInfo, String> {
309+
public class ExpandableAdapter extends OmegaExpandableRecyclerView.Adapter<QuoteGlobalInfo, Quote> {
310310
311311
@Override
312312
protected ExGroupViewHolder provideGroupViewHolder(@NonNull ViewGroup viewGroup) {
@@ -353,18 +353,19 @@ public class ExpandableAdapter extends OmegaExpandableRecyclerView.Adapter<Quote
353353
}
354354
355355
@Override
356-
protected void onBind(String item) {
357-
textView.setText(item);
356+
protected void onBind(Quote item) {
357+
textView.setText(item.getQuote());
358358
}
359359
}
360360
}
361361
```
362362

363363
Use one of the following methods to set items for adapter or use adapter constructors:
364364
```
365-
void setItems(@NonNull List<ExpandableViewData<G, CH>> expandableViewData)
366-
void setItems(ExpandableViewData<G, CH>... expandableViewData)
367-
void setItems(GroupProvider<G, CH>... groupProviders)
365+
public final void setItems(@NonNull List<ExpandableViewData<G, CH>> expandableViewData)
366+
public final void setItems(ExpandableViewData<G, CH>... expandableViewData)
367+
public final void setItemsAsGroupProviders(GroupProvider<G, CH>... groupProviders)
368+
public final void setItemsAsGroupProviders(@NonNull List<GroupProvider<G, CH>> groupProviders)
368369
```
369370

370371
Your view data (group and childs) should be wrapped with ```ExpandableViewData``` using one of the following ways:
@@ -387,6 +388,12 @@ public interface GroupProvider<G, CH> {
387388
}
388389
```
389390

391+
It is available to update only child item using
392+
```
393+
// Adapter
394+
public void notifyChildChanged(CH child)
395+
```
396+
390397
You can set expandMode and childAnimation both with xml attr and programmatically
391398

392399
*Available expandModes are single and multiple*

app/src/main/java/com/omega_r/omegarecyclerview/expandable_example/core/ExpandableActivity.java

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,22 @@
33
import android.os.Bundle;
44
import androidx.annotation.LayoutRes;
55
import androidx.appcompat.app.AppCompatActivity;
6+
7+
import android.view.View;
8+
import android.widget.Button;
69
import android.widget.CompoundButton;
710
import android.widget.RadioButton;
811

912
import com.omega_r.libs.omegarecyclerview.expandable_recycler_view.OmegaExpandableRecyclerView;
1013
import com.omega_r.libs.omegarecyclerview.expandable_recycler_view.animation.ExpandableItemAnimator;
1114
import com.omega_r.libs.omegarecyclerview.expandable_recycler_view.animation.standard_animations.DropDownItemAnimator;
1215
import com.omega_r.libs.omegarecyclerview.expandable_recycler_view.animation.standard_animations.FadeItemAnimator;
16+
import com.omega_r.libs.omegarecyclerview.expandable_recycler_view.data.GroupProvider;
1317
import com.omega_r.omegarecyclerview.R;
1418

19+
import java.util.Arrays;
20+
import java.util.List;
21+
1522
import omega.com.annotations.OmegaActivity;
1623

1724
@OmegaActivity
@@ -27,6 +34,8 @@ protected OmegaExpandableRecyclerView.Adapter provideAdapter() {
2734
return new ExpandableAdapter();
2835
}
2936

37+
private List<? extends GroupProvider<QuoteGlobalInfo, Quote>> items;
38+
3039
@LayoutRes
3140
protected int provideContentLayoutRes() {
3241
return R.layout.activity_expandable;
@@ -37,9 +46,46 @@ protected void onCreate(Bundle savedInstanceState) {
3746
super.onCreate(savedInstanceState);
3847
setContentView(provideContentLayoutRes());
3948

49+
createItems();
50+
4051
setupRecyclerView();
4152
setupRadioButtons();
4253
fillAdapter();
54+
55+
findViewById(R.id.button_test_update_child).setOnClickListener(new View.OnClickListener() {
56+
@Override
57+
public void onClick(View v) {
58+
Quote quote = items.get(3).provideChilds().get(1);
59+
quote.setQuote("UPDATED QUOTE");
60+
mAdapter.notifyChildChanged(quote);
61+
}
62+
});
63+
}
64+
65+
private void createItems() {
66+
items = Arrays.asList(
67+
SimpleData.from(
68+
new QuoteGlobalInfo(getString(R.string.group_text_1), 1500),
69+
new Quote(getString(R.string.child_text_1))
70+
),
71+
SimpleData.from(
72+
new QuoteGlobalInfo(getString(R.string.group_text_2), 1500),
73+
new Quote(getString(R.string.child_text_2))
74+
),
75+
SimpleData.from(
76+
new QuoteGlobalInfo(getString(R.string.group_text_3), 1914),
77+
new Quote(getString(R.string.child_text_3)),
78+
new Quote(getString(R.string.child_text_5))
79+
),
80+
SimpleData.from(
81+
new QuoteGlobalInfo(getString(R.string.group_text_5), 1914),
82+
new Quote(getString(R.string.child_text_1)),
83+
new Quote(getString(R.string.child_text_2)),
84+
new Quote(getString(R.string.child_text_3)),
85+
new Quote(getString(R.string.child_text_4)),
86+
new Quote(getString(R.string.child_text_5))
87+
)
88+
);
4389
}
4490

4591
protected void setupRecyclerView() {
@@ -48,19 +94,7 @@ protected void setupRecyclerView() {
4894
}
4995

5096
protected void fillAdapter() {
51-
mAdapter.setItems(
52-
SimpleData.from(new QuoteGlobalInfo(getString(R.string.group_text_1), 1500), getString(R.string.child_text_1)),
53-
SimpleData.from(new QuoteGlobalInfo(getString(R.string.group_text_2), 1500), getString(R.string.child_text_2)),
54-
SimpleData.from(new QuoteGlobalInfo(getString(R.string.group_text_3), 1914),
55-
getString(R.string.child_text_3),
56-
getString(R.string.child_text_5)),
57-
SimpleData.from(new QuoteGlobalInfo(getString(R.string.group_text_5), 1914),
58-
getString(R.string.child_text_1),
59-
getString(R.string.child_text_2),
60-
getString(R.string.child_text_3),
61-
getString(R.string.child_text_4),
62-
getString(R.string.child_text_5))
63-
);
97+
mAdapter.setItemsAsGroupProviders(items);
6498
}
6599

66100
protected void setupRadioButtons() {

app/src/main/java/com/omega_r/omegarecyclerview/expandable_example/core/ExpandableAdapter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import com.omega_r.libs.omegarecyclerview.expandable_recycler_view.OmegaExpandableRecyclerView;
88
import com.omega_r.omegarecyclerview.R;
99

10-
public class ExpandableAdapter extends OmegaExpandableRecyclerView.Adapter<QuoteGlobalInfo, String> {
10+
public class ExpandableAdapter extends OmegaExpandableRecyclerView.Adapter<QuoteGlobalInfo, Quote> {
1111

1212
@Override
1313
protected ExGroupViewHolder provideGroupViewHolder(@NonNull ViewGroup viewGroup) {
@@ -54,8 +54,8 @@ class ExChildViewHolder extends ChildViewHolder {
5454
}
5555

5656
@Override
57-
protected void onBind(String item) {
58-
textView.setText(item);
57+
protected void onBind(Quote item) {
58+
textView.setText(item.getQuote());
5959
}
6060
}
6161
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.omega_r.omegarecyclerview.expandable_example.core;
2+
3+
public class Quote {
4+
private String mQuote;
5+
6+
public Quote(String quote) {
7+
mQuote = quote;
8+
}
9+
10+
public String getQuote() {
11+
return mQuote;
12+
}
13+
14+
public void setQuote(String quote) {
15+
mQuote = quote;
16+
}
17+
18+
@Override
19+
public boolean equals(Object o) {
20+
if (this == o) return true;
21+
if (o == null || getClass() != o.getClass()) return false;
22+
Quote quote = (Quote) o;
23+
return mQuote.equals(quote.mQuote);
24+
}
25+
26+
@Override
27+
public int hashCode() {
28+
return mQuote.hashCode();
29+
}
30+
}

app/src/main/java/com/omega_r/omegarecyclerview/expandable_example/core/SimpleData.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
import java.util.Arrays;
88
import java.util.List;
99

10-
public class SimpleData implements GroupProvider<QuoteGlobalInfo, String> {
10+
public class SimpleData implements GroupProvider<QuoteGlobalInfo, Quote> {
1111

1212
private QuoteGlobalInfo mQuoteGlobalInfo;
13-
private List<String> mQuotes;
13+
private List<Quote> mQuotes;
1414

15-
public static SimpleData from(QuoteGlobalInfo title, String... quotes) {
15+
public static SimpleData from(QuoteGlobalInfo title, Quote... quotes) {
1616
return new SimpleData(title, Arrays.asList(quotes));
1717
}
1818

19-
public SimpleData(QuoteGlobalInfo quoteGlobalInfo, List<String> quotes) {
19+
public SimpleData(QuoteGlobalInfo quoteGlobalInfo, List<Quote> quotes) {
2020
mQuoteGlobalInfo = quoteGlobalInfo;
2121
mQuotes = quotes;
2222
}
@@ -29,11 +29,11 @@ public void setQuoteGlobalInfo(QuoteGlobalInfo quoteGlobalInfo) {
2929
mQuoteGlobalInfo = quoteGlobalInfo;
3030
}
3131

32-
public List<String> getQuotes() {
32+
public List<Quote> getQuotes() {
3333
return mQuotes;
3434
}
3535

36-
public void setQuotes(List<String> quotes) {
36+
public void setQuotes(List<Quote> quotes) {
3737
mQuotes = quotes;
3838
}
3939

@@ -43,7 +43,7 @@ public QuoteGlobalInfo provideGroup() {
4343
}
4444

4545
@Override
46-
public List<String> provideChilds() {
46+
public List<Quote> provideChilds() {
4747
return mQuotes;
4848
}
4949

0 commit comments

Comments
 (0)