Skip to content

Commit 00fabdf

Browse files
committed
Update
1 parent bdd39d6 commit 00fabdf

3 files changed

Lines changed: 107 additions & 1 deletion

File tree

app/src/main/java/com/tool/tree/ActivityFileSelector.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,32 @@ class ActivityFileSelector : AppCompatActivity() {
152152

153153
binding.fileSelectorList.adapter = adapterFileSelector
154154

155+
// Hàng "Chọn tất cả" chỉ hiện khi đang ở chế độ chọn nhiều (multiple)
156+
if (multiple) {
157+
binding.selectAllBlock.visibility = View.VISIBLE
158+
159+
fun syncSelectAllCheckbox() {
160+
binding.selectAll.isChecked = adapterFileSelector?.isAllCurrentDirSelected() == true
161+
}
162+
syncSelectAllCheckbox()
163+
164+
val toggleSelectAll = View.OnClickListener {
165+
val nextState = adapterFileSelector?.isAllCurrentDirSelected() != true
166+
adapterFileSelector?.setSelectAllState(nextState)
167+
binding.selectAll.isChecked = nextState
168+
}
169+
binding.selectAllBlock.setOnClickListener(toggleSelectAll)
170+
binding.selectAll.setOnClickListener(toggleSelectAll)
171+
172+
adapterFileSelector?.setSelectionChangedListener(object : AdapterFileSelector.SelectionChangedListener {
173+
override fun onSelectionChanged(selectedCount: Int) {
174+
syncSelectAllCheckbox()
175+
}
176+
})
177+
} else {
178+
binding.selectAllBlock.visibility = View.GONE
179+
}
180+
155181
// ✅ Chụp ảnh mờ sau khi gán Adapter và Render dữ liệu xong
156182
binding.root.post {
157183
BlurEngine.controller.captureAndBlur(this)

app/src/main/java/com/tool/tree/ui/AdapterFileSelector.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ public boolean accept(File fileItem) {
156156
public void run() {
157157
notifyDataSetChanged();
158158
progressBarDialog.hideDialog();
159+
if (selectionChangedListener != null) {
160+
selectionChangedListener.onSelectionChanged(selectedFiles.size());
161+
}
159162
}
160163
});
161164
}).start();
@@ -220,6 +223,51 @@ private void toggleSelection(File file) {
220223
}
221224
}
222225

226+
// Một tệp/thư mục có phải là đối tượng "có thể chọn" (hiện checkbox) trong danh sách hiện tại không.
227+
// Ở chế độ chọn thư mục: mọi mục trong fileArray đều là thư mục -> có thể chọn.
228+
// Ở chế độ chọn tệp: chỉ tệp mới có thể chọn, thư mục chỉ dùng để điều hướng.
229+
private boolean isSelectable(File file) {
230+
return folderChooserMode || !file.isDirectory();
231+
}
232+
233+
// Thư mục hiện tại đã được chọn hết (mọi mục có thể chọn) hay chưa - dùng để đồng bộ checkbox "Chọn tất cả".
234+
public boolean isAllCurrentDirSelected() {
235+
if (fileArray == null || fileArray.length == 0) {
236+
return false;
237+
}
238+
boolean hasSelectable = false;
239+
for (File file : fileArray) {
240+
if (isSelectable(file)) {
241+
hasSelectable = true;
242+
if (!selectedFiles.contains(file)) {
243+
return false;
244+
}
245+
}
246+
}
247+
return hasSelectable;
248+
}
249+
250+
// Chọn/bỏ chọn tất cả các mục có thể chọn trong thư mục đang hiển thị.
251+
public void setSelectAllState(boolean selectAll) {
252+
if (fileArray == null) {
253+
return;
254+
}
255+
for (File file : fileArray) {
256+
if (!isSelectable(file)) {
257+
continue;
258+
}
259+
if (selectAll) {
260+
selectedFiles.add(file);
261+
} else {
262+
selectedFiles.remove(file);
263+
}
264+
}
265+
notifyDataSetChanged();
266+
if (selectionChangedListener != null) {
267+
selectionChangedListener.onSelectionChanged(selectedFiles.size());
268+
}
269+
}
270+
223271
@Override
224272
public View getView(int position, View convertView, ViewGroup parent) {
225273
final View view;

app/src/main/res/layout/activity_file_selector.xml

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,38 @@
1919
android:layout_height="match_parent"
2020
android:orientation="vertical">
2121

22+
<!-- Hàng "Chọn tất cả" cố định ở đầu trang, chỉ hiện khi đang ở chế độ chọn nhiều (multiple) -->
23+
<LinearLayout
24+
android:id="@+id/select_all_block"
25+
android:orientation="horizontal"
26+
android:gravity="center_vertical"
27+
android:visibility="gone"
28+
android:clickable="true"
29+
android:focusable="true"
30+
android:paddingStart="16dp"
31+
android:paddingEnd="16dp"
32+
android:paddingTop="10dp"
33+
android:paddingBottom="10dp"
34+
android:layout_width="match_parent"
35+
android:layout_height="wrap_content">
36+
37+
<TextView
38+
android:id="@+id/select_all_label"
39+
android:text="@string/select_all"
40+
android:textColor="?android:attr/textColorPrimary"
41+
android:textSize="14dp"
42+
android:layout_width="0dp"
43+
android:layout_weight="1"
44+
android:layout_height="wrap_content" />
45+
46+
<CheckBox
47+
android:id="@+id/select_all"
48+
android:clickable="true"
49+
android:focusable="true"
50+
android:layout_width="wrap_content"
51+
android:layout_height="wrap_content" />
52+
</LinearLayout>
53+
2254
<ListView
2355
android:id="@+id/file_selector_list"
2456
android:layout_width="match_parent"
@@ -31,4 +63,4 @@
3163

3264
</com.omarea.common.ui.BlurViewLinearLayout>
3365

34-
</LinearLayout>
66+
</LinearLayout>

0 commit comments

Comments
 (0)