From 3955de100dfeb40caee3b39c8ef7889c0aa57d4b Mon Sep 17 00:00:00 2001 From: ZhanGSKen Date: Sun, 26 Apr 2026 11:06:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=9B=B8=E5=86=8C=E9=9B=86?= =?UTF-8?q?=E6=B5=8F=E8=A7=88=E7=AA=97=E5=8F=A3=E6=8E=92=E5=BA=8F=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 工具栏添加排序按钮组,支持时间倒序/正序、名称倒序/正序 - 排序设置保存到本地,应用重启后恢复默认排序方式 - 排序菜单显示当前选中的排序方法(带勾选标记) - 修复置顶图片排序,保留原有排序顺序排在前面 --- gallery/build.properties | 4 +- .../winboll/studio/gallery/AlbumActivity.java | 85 +++++++++++++++++-- .../winboll/studio/gallery/ImageAdapter.java | 38 +++------ .../winboll/studio/gallery/Preferences.java | 16 ++++ gallery/src/main/res/drawable/ic_sort.xml | 10 +++ gallery/src/main/res/menu/menu_album.xml | 26 ++++++ 6 files changed, 143 insertions(+), 36 deletions(-) create mode 100644 gallery/src/main/res/drawable/ic_sort.xml create mode 100644 gallery/src/main/res/menu/menu_album.xml diff --git a/gallery/build.properties b/gallery/build.properties index 9d5ea53..5563358 100644 --- a/gallery/build.properties +++ b/gallery/build.properties @@ -1,8 +1,8 @@ #Created by .winboll/winboll_app_build.gradle -#Sun Apr 26 10:16:07 HKT 2026 +#Sun Apr 26 11:00:35 CST 2026 stageCount=5 libraryProject= baseVersion=15.0 publishVersion=15.0.4 -buildCount=0 +buildCount=5 baseBetaVersion=15.0.5 diff --git a/gallery/src/main/java/cc/winboll/studio/gallery/AlbumActivity.java b/gallery/src/main/java/cc/winboll/studio/gallery/AlbumActivity.java index e0e6795..034d95d 100644 --- a/gallery/src/main/java/cc/winboll/studio/gallery/AlbumActivity.java +++ b/gallery/src/main/java/cc/winboll/studio/gallery/AlbumActivity.java @@ -33,7 +33,7 @@ public class AlbumActivity extends AppCompatActivity { private String albumPath; private String albumName; private FloatingActionButton fabScrollTop; - private RecyclerView.OnScrollListener scrollListener; + private Preferences prefs; @Override protected void onCreate(Bundle savedInstanceState) { @@ -49,6 +49,8 @@ public class AlbumActivity extends AppCompatActivity { getSupportActionBar().setTitle(albumName); + prefs = new Preferences(this); + recyclerView = findViewById(R.id.recycler_view); recyclerView.setLayoutManager(new GridLayoutManager(this, 3)); adapter = new ImageAdapter(); @@ -63,7 +65,7 @@ public class AlbumActivity extends AppCompatActivity { } }); - scrollListener = new RecyclerView.OnScrollListener() { + recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); @@ -77,8 +79,7 @@ public class AlbumActivity extends AppCompatActivity { } } } - }; - recyclerView.addOnScrollListener(scrollListener); + }); adapter.setOnImageClickListener(new OnImageClickListener() { @Override @@ -122,6 +123,20 @@ public class AlbumActivity extends AppCompatActivity { } } +private String getSortOrder(int sortMode) { + switch (sortMode) { + case Preferences.SORT_TIME_ASC: + return android.provider.MediaStore.Images.Media.DATE_ADDED + " ASC"; + case Preferences.SORT_NAME_DESC: + return android.provider.MediaStore.Images.Media.DISPLAY_NAME + " DESC"; + case Preferences.SORT_NAME_ASC: + return android.provider.MediaStore.Images.Media.DISPLAY_NAME + " ASC"; + case Preferences.SORT_TIME_DESC: + default: + return android.provider.MediaStore.Images.Media.DATE_ADDED + " DESC"; + } + } + private void loadImages() { LogUtils.d(TAG, "loadImages"); ArrayList imageUrls = new ArrayList<>(); @@ -131,7 +146,8 @@ public class AlbumActivity extends AppCompatActivity { String selection = android.provider.MediaStore.Images.Media.DATA + " LIKE ?"; String[] selectionArgs = new String[]{albumPath + "%"}; - String sortOrder = android.provider.MediaStore.Images.Media.DATE_ADDED + " DESC"; + int sortMode = prefs.getAlbumSortMode(); + String sortOrder = getSortOrder(sortMode); try (Cursor cursor = contentResolver.query(collection, null, selection, selectionArgs, sortOrder)) { if (cursor != null) { @@ -155,7 +171,64 @@ public class AlbumActivity extends AppCompatActivity { adapter.setData(imageUrls, imagePaths); LogUtils.d(TAG, "Loaded " + imageUrls.size() + " images"); } - + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.menu_album, menu); + int sortMode = prefs.getAlbumSortMode(); + int menuId = getSortMenuId(sortMode); + MenuItem item = menu.findItem(menuId); + if (item != null) { + item.setChecked(true); + } + MenuItem sortItem = menu.findItem(R.id.action_sort); + if (sortItem != null && sortItem.getSubMenu() != null) { + sortItem.getSubMenu().setGroupCheckable(0, true, true); + } + return true; + } + + private int getSortMenuId(int sortMode) { + switch (sortMode) { + case Preferences.SORT_TIME_ASC: + return R.id.sort_time_asc; + case Preferences.SORT_NAME_DESC: + return R.id.sort_name_desc; + case Preferences.SORT_NAME_ASC: + return R.id.sort_name_asc; + case Preferences.SORT_TIME_DESC: + default: + return R.id.sort_time_desc; + } + } + + @Override + public boolean onOptionsItemSelected(@NonNull MenuItem item) { + int itemId = item.getItemId(); + if (itemId == R.id.sort_time_desc) { + prefs.setAlbumSortMode(Preferences.SORT_TIME_DESC); + item.setChecked(true); + loadImages(); + return true; + } else if (itemId == R.id.sort_time_asc) { + prefs.setAlbumSortMode(Preferences.SORT_TIME_ASC); + item.setChecked(true); + loadImages(); + return true; + } else if (itemId == R.id.sort_name_desc) { + prefs.setAlbumSortMode(Preferences.SORT_NAME_DESC); + item.setChecked(true); + loadImages(); + return true; + } else if (itemId == R.id.sort_name_asc) { + prefs.setAlbumSortMode(Preferences.SORT_NAME_ASC); + item.setChecked(true); + loadImages(); + return true; + } + return super.onOptionsItemSelected(item); + } + @Override protected void onResume() { super.onResume(); diff --git a/gallery/src/main/java/cc/winboll/studio/gallery/ImageAdapter.java b/gallery/src/main/java/cc/winboll/studio/gallery/ImageAdapter.java index cb1badd..16a0b89 100644 --- a/gallery/src/main/java/cc/winboll/studio/gallery/ImageAdapter.java +++ b/gallery/src/main/java/cc/winboll/studio/gallery/ImageAdapter.java @@ -58,45 +58,27 @@ public class ImageAdapter extends RecyclerView.Adapter if (pinnedDbHelper == null || imagePaths.isEmpty()) { return; } + ArrayList pinnedUrls = new ArrayList<>(); ArrayList pinnedPaths = new ArrayList<>(); + ArrayList unpinnedUrls = new ArrayList<>(); ArrayList unpinnedPaths = new ArrayList<>(); - for (String path : imagePaths) { + for (int i = 0; i < imagePaths.size(); i++) { + String path = imagePaths.get(i); if (pinnedDbHelper.isPinned(path)) { + pinnedUrls.add(imageUrls.get(i)); pinnedPaths.add(path); } else { + unpinnedUrls.add(imageUrls.get(i)); unpinnedPaths.add(path); } } - Comparator pathComparator = new Comparator() { - @Override - public int compare(String p1, String p2) { - return p1.compareToIgnoreCase(p2); - } - }; - Collections.sort(pinnedPaths, pathComparator); - Collections.sort(unpinnedPaths, pathComparator); - - ArrayList newUrls = new ArrayList<>(); - ArrayList newPaths = new ArrayList<>(); - for (String path : pinnedPaths) { - int index = imagePaths.indexOf(path); - if (index >= 0) { - newUrls.add(imageUrls.get(index)); - newPaths.add(path); - } - } - for (String path : unpinnedPaths) { - int index = imagePaths.indexOf(path); - if (index >= 0) { - newUrls.add(imageUrls.get(index)); - newPaths.add(path); - } - } imageUrls.clear(); imagePaths.clear(); - imageUrls.addAll(newUrls); - imagePaths.addAll(newPaths); + imageUrls.addAll(pinnedUrls); + imageUrls.addAll(unpinnedUrls); + imagePaths.addAll(pinnedPaths); + imagePaths.addAll(unpinnedPaths); } public void setContext(android.content.Context context) { diff --git a/gallery/src/main/java/cc/winboll/studio/gallery/Preferences.java b/gallery/src/main/java/cc/winboll/studio/gallery/Preferences.java index 500cb25..d5e70e3 100644 --- a/gallery/src/main/java/cc/winboll/studio/gallery/Preferences.java +++ b/gallery/src/main/java/cc/winboll/studio/gallery/Preferences.java @@ -9,9 +9,16 @@ public class Preferences { private static final String PREF_NAME = "gallery_prefs"; private static final String KEY_FOLDER_PATH = "folder_path"; private static final String KEY_BG_TYPE = "bg_type"; + private static final String KEY_ALBUM_SORT_MODE = "album_sort_mode"; private static final int DEFAULT_BG_TYPE = 0; + private static final int DEFAULT_SORT_MODE = 0; private static final String DEFAULT_PATH = "/storage/emulated/0/Pictures/Gallery/owner"; + public static final int SORT_TIME_DESC = 0; + public static final int SORT_TIME_ASC = 1; + public static final int SORT_NAME_DESC = 2; + public static final int SORT_NAME_ASC = 3; + public static String getDefaultPath() { return DEFAULT_PATH; } @@ -41,4 +48,13 @@ public class Preferences { LogUtils.d(TAG, "setBgType: " + type); prefs.edit().putInt(KEY_BG_TYPE, type).apply(); } + + public int getAlbumSortMode() { + return prefs.getInt(KEY_ALBUM_SORT_MODE, DEFAULT_SORT_MODE); + } + + public void setAlbumSortMode(int mode) { + LogUtils.d(TAG, "setAlbumSortMode: " + mode); + prefs.edit().putInt(KEY_ALBUM_SORT_MODE, mode).apply(); + } } \ No newline at end of file diff --git a/gallery/src/main/res/drawable/ic_sort.xml b/gallery/src/main/res/drawable/ic_sort.xml new file mode 100644 index 0000000..7d37ecc --- /dev/null +++ b/gallery/src/main/res/drawable/ic_sort.xml @@ -0,0 +1,10 @@ + + + + \ No newline at end of file diff --git a/gallery/src/main/res/menu/menu_album.xml b/gallery/src/main/res/menu/menu_album.xml new file mode 100644 index 0000000..9fbf668 --- /dev/null +++ b/gallery/src/main/res/menu/menu_album.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + \ No newline at end of file