Skip to content

Instantly share code, notes, and snippets.

@jayjaykim
Last active August 26, 2016 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jayjaykim/5426043f7cdff438a06cf92457232a75 to your computer and use it in GitHub Desktop.
Save jayjaykim/5426043f7cdff438a06cf92457232a75 to your computer and use it in GitHub Desktop.
RecyclerView Fold/Unfold implementations
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