Skip to content

Commit 2bf2c21

Browse files
author
최인탁
committed
added library files
1 parent cf6dc45 commit 2bf2c21

9 files changed

Lines changed: 137 additions & 45 deletions

File tree

.idea/misc.xml

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

library/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@ dependencies {
2929
exclude group: 'com.android.support', module: 'support-annotations'
3030
})
3131
compile 'com.android.support:appcompat-v7:25.1.0'
32+
compile 'com.android.support:recyclerview-v7:25.1.0'
3233
testCompile 'junit:junit:4.12'
3334
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.choiintack.recursiverecyclerview;
2+
3+
import java.util.List;
4+
5+
/**
6+
* Created by choiintack on 2017. 1. 31..
7+
*/
8+
9+
public interface RecursiveItem {
10+
List getChildren();
11+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.choiintack.recursiverecyclerview;
2+
3+
/**
4+
* Created by choiintack on 2017. 1. 31..
5+
*/
6+
7+
public class RecursiveItemSet {
8+
int depth;
9+
boolean expanded;
10+
Object item;
11+
12+
public RecursiveItemSet(Object item, boolean expanded, int depth) {
13+
this.item = item;
14+
this.expanded = expanded;
15+
this.depth = depth;
16+
}
17+
18+
public int getDepth() {
19+
return depth;
20+
}
21+
public boolean isExpanded() {
22+
return expanded;
23+
}
24+
public Object getItem() {
25+
return item;
26+
}
27+
28+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.choiintack.recursiverecyclerview;
2+
3+
import android.support.v7.widget.RecyclerView;
4+
import android.view.View;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
/**
10+
* Created by choiintack on 2017. 1. 31..
11+
*/
12+
13+
public abstract class RecursiveRecyclerAdapter<VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> {
14+
15+
List<RecursiveItemSet> mItems = new ArrayList<>();
16+
17+
public void setItems(List items) {
18+
for (Object item : items) {
19+
mItems.add(new RecursiveItemSet(
20+
item, false, 0
21+
));
22+
}
23+
}
24+
25+
public Object getItem(int position) {
26+
return mItems.get(position).getItem();
27+
}
28+
29+
@Override
30+
public void onBindViewHolder(VH holder, final int position) {
31+
if (getItem(position) instanceof RecursiveItem) {
32+
final RecursiveItem item = (RecursiveItem) getItem(position);
33+
final Object _item = mItems.get(position);
34+
holder.itemView.setOnClickListener(new View.OnClickListener() {
35+
@Override
36+
public void onClick(View v) {
37+
if (expanded(_item)) {
38+
removeItems(_item, item.getChildren());
39+
} else {
40+
addItems(_item, item.getChildren());
41+
}
42+
}
43+
});
44+
}
45+
}
46+
47+
@Override
48+
public int getItemCount() {
49+
return mItems.size();
50+
}
51+
52+
@Override
53+
public int getItemViewType(int position) {
54+
return mItems.get(position).getDepth();
55+
}
56+
57+
public boolean expanded(Object item) {
58+
int position = mItems.indexOf(item);
59+
return mItems.get(position).isExpanded();
60+
}
61+
62+
public void addItems(Object parent, List items) {
63+
int position = mItems.indexOf(parent);
64+
for (int i = 0; i < items.size(); i ++) {
65+
Object item = items.get(i);
66+
mItems.add(position + 1 + i, new RecursiveItemSet(
67+
item, false, mItems.get(position).getDepth() + 1
68+
));
69+
}
70+
mItems.get(position).expanded = true;
71+
notifyItemRangeInserted(position + 1, items.size());
72+
notifyItemChanged(position);
73+
}
74+
75+
public void removeItems(Object parent, List items) {
76+
int position = mItems.indexOf(parent);
77+
for (int i = 0; i < items.size(); i ++) {
78+
if (getItem(position + 1) instanceof RecursiveItem) {
79+
if (expanded(mItems.get(position + 1))) {
80+
RecursiveItem item = (RecursiveItem) getItem(position + 1);
81+
removeItems(mItems.get(position + 1), item.getChildren());
82+
}
83+
}
84+
mItems.remove(position + 1);
85+
}
86+
mItems.get(position).expanded = false;
87+
notifyItemRangeRemoved(position + 1, items.size());
88+
notifyItemChanged(position);
89+
}
90+
91+
}

sampleapp/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ android {
44
compileSdkVersion 25
55
buildToolsVersion "25.0.2"
66
defaultConfig {
7-
applicationId "com.choiintack.recursiverecyclerview"
7+
applicationId "com.choiintack.recursiverecyclerviewdemo"
88
minSdkVersion 9
99
targetSdkVersion 25
1010
versionCode 1
@@ -25,5 +25,6 @@ dependencies {
2525
exclude group: 'com.android.support', module: 'support-annotations'
2626
})
2727
compile 'com.android.support:appcompat-v7:25.1.0'
28+
compile project(':library')
2829
testCompile 'junit:junit:4.12'
2930
}

sampleapp/src/androidTest/java/com/choiintack/recursiverecyclerview/ExampleInstrumentedTest.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

sampleapp/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2-
package="com.choiintack.recursiverecyclerview">
2+
package="com.choiintack.recursiverecyclerviewdemo">
33

44
<application
55
android:allowBackup="true"

sampleapp/src/test/java/com/choiintack/recursiverecyclerview/ExampleUnitTest.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)