Skip to content

Commit ec90a99

Browse files
committed
Ability to select & deselect a file
1 parent 173d867 commit ec90a99

File tree

6 files changed

+53
-15
lines changed

6 files changed

+53
-15
lines changed

simplefileexplorer/src/main/java/com/example/simplefileexplorer/ActivityListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55
interface ActivityListener extends Serializable {
66
void onDirectoryChanged(String absolutePath);
7-
void onFileSelect(String absolutePath);
7+
void onFileSelect(FileModel fileModel);
88
void onBackButtonPressed(String absolutePath);
99
}

simplefileexplorer/src/main/java/com/example/simplefileexplorer/AdapterListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55
interface AdapterListener extends Serializable {
66
void onDirectoryClick(String selectedAbsolutePath);
7-
void onFileClick(String selectedAbsolutePath);
7+
void onFileClick(FileModel fileModel);
88
}

simplefileexplorer/src/main/java/com/example/simplefileexplorer/FileModel.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,21 @@
22

33
class FileModel {
44
private String absolutePath;
5+
private String directoryPath;
56
private FileModelType fileModelType;
7+
private boolean isSelected;
68

79
public FileModel(String absolutePath, FileModelType fileModelType) {
810
this.absolutePath = absolutePath;
11+
this.directoryPath = this.absolutePath;
912
this.fileModelType = fileModelType;
13+
this.isSelected = false;
1014
}
1115

16+
public FileModel(String absolutePath, String directoryPath, FileModelType fileModelType) {
17+
this(absolutePath, fileModelType);
18+
this.directoryPath = directoryPath;
19+
}
1220

1321
public String getAbsolutePath() {
1422
return absolutePath;
@@ -22,6 +30,22 @@ public FileModelType getFileModelType() {
2230
return fileModelType;
2331
}
2432

33+
public boolean isSelected() {
34+
return isSelected;
35+
}
36+
37+
public void setSelected(boolean selected) {
38+
isSelected = selected;
39+
}
40+
41+
public String getDirectoryPath() {
42+
return directoryPath;
43+
}
44+
45+
public void setDirectoryPath(String directoryPath) {
46+
this.directoryPath = directoryPath;
47+
}
48+
2549
public void setFileModelType(FileModelType fileModelType) {
2650
this.fileModelType = fileModelType;
2751
}

simplefileexplorer/src/main/java/com/example/simplefileexplorer/SimpleFileExplorerActivity.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,16 @@ public void onDirectoryChanged(String absolutePath) {
7575
}
7676

7777
@Override
78-
public void onFileSelect(String absolutePath) {
79-
this.selectedAbsolutePath = absolutePath;
80-
this.fileTypeImageView.setImageResource(R.drawable.ic_file);
78+
public void onFileSelect(FileModel fileModel) {
79+
if(fileModel.isSelected()){
80+
this.selectedAbsolutePath = fileModel.getAbsolutePath();
81+
this.fileTypeImageView.setImageResource(R.drawable.ic_file);
82+
}
83+
else{
84+
this.selectedAbsolutePath = fileModel.getDirectoryPath();
85+
this.fileTypeImageView.setImageResource(R.drawable.ic_folder);
86+
}
87+
8188
}
8289

8390
@Override

simplefileexplorer/src/main/java/com/example/simplefileexplorer/SimpleFileExplorerAdapter.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void onBindViewHolder(@NonNull SimpleFileExplorerViewHolder simpleFileExp
5151
simpleFileExplorerViewHolder.backgroundConstraintLayout.setBackgroundColor(Color.rgb(255, 255, 255));
5252
this.setTextByFileAbsolutePath(simpleFileExplorerViewHolder.fileAbsolutePathTextView, fileModel.getAbsolutePath());
5353
this.setImagesByFileType(simpleFileExplorerViewHolder.fileImageView, fileModel.getFileModelType());
54-
this.setLayoutOnClickListenerByFileType(simpleFileExplorerViewHolder.backgroundConstraintLayout, fileModel.getFileModelType(), fileModel.getAbsolutePath(), i);
54+
this.setLayoutOnClickListenerByFileType(simpleFileExplorerViewHolder.backgroundConstraintLayout, fileModel.getFileModelType(), fileModel, i);
5555
this.updateSelectedItemColor(simpleFileExplorerViewHolder.backgroundConstraintLayout, i);
5656

5757

@@ -68,7 +68,7 @@ public void loadDirectory(List<FileModel> filesList) {
6868
@Override
6969
public int compare(FileModel o1, FileModel o2) {
7070
int directorySortResult = o2.getFileModelType().compareTo(o1.getFileModelType());
71-
if(directorySortResult == 0){
71+
if (directorySortResult == 0) {
7272
return o1.getAbsolutePath().compareTo(o2.getAbsolutePath());
7373
}
7474
return directorySortResult;
@@ -84,10 +84,10 @@ private void setTextByFileAbsolutePath(TextView textView, String absolutePath) {
8484
private void setImagesByFileType(ImageView imageView, FileModelType fileModelType) {
8585
int fileImageId = 0;
8686
int directoryImageId = 0;
87-
if(SimpleFileResources.imageFileId == null){
87+
if (SimpleFileResources.imageFileId == null) {
8888
fileImageId = SimpleFileResources.defaultImageFileId;
8989
}
90-
if(SimpleFileResources.imageDirectoryId == null){
90+
if (SimpleFileResources.imageDirectoryId == null) {
9191
directoryImageId = SimpleFileResources.defaultImageDirectoryId;
9292
}
9393
switch (fileModelType) {
@@ -102,18 +102,25 @@ private void setImagesByFileType(ImageView imageView, FileModelType fileModelTyp
102102
}
103103
}
104104

105-
private void setLayoutOnClickListenerByFileType(final ConstraintLayout layout, final FileModelType fileModelType, final String absolutePath, final int index) {
105+
private void setLayoutOnClickListenerByFileType(final ConstraintLayout layout, final FileModelType fileModelType, final FileModel fileModel, final int index) {
106106
layout.setOnClickListener(new View.OnClickListener() {
107107
@Override
108108
public void onClick(View v) {
109109
switch (fileModelType) {
110110
case FILE:
111-
adapterListener.onFileClick(absolutePath);
111+
if (fileModel.isSelected()) {
112+
fileModel.setSelected(false);
113+
adapterListener.onFileClick(fileModel);
114+
notifyDataSetChanged();
115+
break;
116+
}
117+
fileModel.setSelected(true);
118+
adapterListener.onFileClick(fileModel);
112119
previousItemSelectedIndex = index;
113120
notifyDataSetChanged();
114121
break;
115122
case DIRECTORY:
116-
adapterListener.onDirectoryClick(absolutePath);
123+
adapterListener.onDirectoryClick(fileModel.getAbsolutePath());
117124
break;
118125
default:
119126
break;

simplefileexplorer/src/main/java/com/example/simplefileexplorer/SimpleFileExplorerFragment.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ private void loadDirectory(){
6969
fileModelList.add(new FileModel(file.getAbsolutePath(), FileModelType.DIRECTORY));
7070
}
7171
else{
72-
fileModelList.add(new FileModel(file.getAbsolutePath(), FileModelType.FILE));
72+
fileModelList.add(new FileModel(file.getAbsolutePath(), file.getParentFile().getAbsolutePath(), FileModelType.FILE));
7373
}
7474
}
7575
}
@@ -95,8 +95,8 @@ public void onDirectoryClick(String selectedAbsolutePath) {
9595
}
9696

9797
@Override
98-
public void onFileClick(String selectedAbsolutePath) {
99-
this.activityListener.onFileSelect(selectedAbsolutePath);
98+
public void onFileClick(FileModel fileModel) {
99+
this.activityListener.onFileSelect(fileModel);
100100
}
101101

102102
void setListeners(ActivityListener activityListener){

0 commit comments

Comments
 (0)