Last active
August 26, 2016 11:40
-
-
Save jayjaykim/5426043f7cdff438a06cf92457232a75 to your computer and use it in GitHub Desktop.
RecyclerView Fold/Unfold implementations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FileWrapper { | |
int type; // 0 : File, 1 : Directory | |
int depth; // 0~n | |
File file; | |
// contructor for injecting depedency, e.g. File object, type, depth | |
// setters and getters | |
} | |
public class Adapter extends RecyclerView.Adapter<RecyclerView.Holder> { | |
List<FileWrapper> dataset; | |
SparseArray<Range> filePositonRangeInDirectory; | |
// ... overriden methods | |
// TODO need synchronized? | |
public void fold(int position, List<FileWrapper> files) { | |
final Range range = filePositonRangeInDirectory.get(position); | |
filePositonRangeInDirectory.remove(position); | |
final int start = range.getStart(); | |
for(int i = range.getEnd() i >= start; i--) { // FIXME ArrayList.removeRange(). | |
dataset.remove(i); | |
} | |
notifyItemRangeRemoved(range.getStart(), range.getEnd()); | |
} | |
// TODO need synchronized? | |
public void unfold(int position) { | |
final int end = position + files.size() - 1; | |
dataset.addAll(position, files); | |
filePositonRangeInDirectory.append(position, | |
Range.getRange(position, end); | |
notifyItemRangeInserted(position, end); | |
} | |
public static class Range { | |
int start; | |
int end; | |
private Range(int start, int end) { | |
this.start = start; | |
this.end = end; | |
} | |
public static final Range getRange(int start, int end) { | |
return new Range(start, end); | |
} | |
// setters and getters | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment