From 010224461b669d00af6df2867e3eaba76a0f0d74 Mon Sep 17 00:00:00 2001 From: SlaVcE <102253539+SlaVcE14@users.noreply.github.com> Date: Thu, 13 Jul 2023 23:56:04 +0200 Subject: [PATCH 1/6] select item --- SJDialog/ListDialogDoc.md | 8 +++ SJDialog/build.gradle | 2 +- .../library/customdialog/ListDialog.java | 52 ++++++++++++++++++- .../adapter/DefaultListAdapter.java | 16 +++++- .../adapter/DefaultListAdapterGeneric.java | 15 ++++++ 5 files changed, 90 insertions(+), 3 deletions(-) diff --git a/SJDialog/ListDialogDoc.md b/SJDialog/ListDialogDoc.md index 233a1c7..59d37fb 100644 --- a/SJDialog/ListDialogDoc.md +++ b/SJDialog/ListDialogDoc.md @@ -393,6 +393,14 @@ listDialog.setItems(arrayList, listItemValues, listItemClickObj); listDialog.setImageItems(listItems); listDialog.setImageItems(listItems, listItemClickObj); +//Select item in a list +listDialog.selectItem(id); +//Get select item in a list +int id = listDialog.getSelectedItem(); + +//Update list +listDialog.updateList(); + //Hide 'List is empty' text listDialog.hideEmptyListText(); //Change empty list text diff --git a/SJDialog/build.gradle b/SJDialog/build.gradle index 36447bf..19beeb0 100644 --- a/SJDialog/build.gradle +++ b/SJDialog/build.gradle @@ -8,7 +8,7 @@ android { defaultConfig { minSdk 23 versionCode 21 - versionName "1.6.1 (dev 1)" + versionName "1.6.1 (dev 2)" buildConfigField 'int', 'VERSION_CODE', "${versionCode}" buildConfigField 'String', 'VERSION_NAME', "\"${versionName}\"" diff --git a/SJDialog/src/main/java/com/sjapps/library/customdialog/ListDialog.java b/SJDialog/src/main/java/com/sjapps/library/customdialog/ListDialog.java index b151539..7f211f7 100644 --- a/SJDialog/src/main/java/com/sjapps/library/customdialog/ListDialog.java +++ b/SJDialog/src/main/java/com/sjapps/library/customdialog/ListDialog.java @@ -471,7 +471,7 @@ public ListDialog hideEmptyListText() { hideEmptyListTxt = true; return this; } - + /** * Set text to display when list is empty * @param text Text @@ -852,6 +852,10 @@ public RecyclerView.Adapter getListAdapter() { return adapter; } + public void updateList(){ + adapter.notifyItemRangeChanged(0,adapter.getItemCount()); + } + /** * @return Background resource for views in RecycleView @@ -880,6 +884,48 @@ public ListDialog setSelectableList() { return this; } + /** + * Select an item in a list + * + * @param id id of a item + * @return current class + * @since 1.6.1 + */ + public ListDialog selectItem(int id){ + + if (adapter == null) + throw nullAdapterException; + + if (adapter instanceof DefaultListAdapterGeneric){ + ((DefaultListAdapterGeneric) adapter).selectItem(id); + return this; + } + if (adapter instanceof DefaultListAdapter){ + ((DefaultListAdapter) adapter).selectItem(id); + return this; + } + + throw AdapterNotSupportedException; + } + + /** + * Get a index of a selected item in a list + * + * @return index of a selected item in a list + * @since 1.6.1 + */ + public int getSelectedItem(){ + if (adapter == null) + throw nullAdapterException; + + if (adapter instanceof DefaultListAdapterGeneric) + return ((DefaultListAdapterGeneric) adapter).getSelectedItem(); + if (adapter instanceof DefaultListAdapter) + return ((DefaultListAdapter) adapter).getSelectedItem(); + + throw AdapterNotSupportedException; + } + public boolean isSelectableList() { return isSelectableList; } @@ -922,6 +968,8 @@ public ArrayList getSelectedItems() { return (ArrayList) selectedItems; } + + void checkListsSize(int size) { if (hideEmptyListTxt) { @@ -944,4 +992,6 @@ private NullPointerException nullListItemClick(Class className) { } private final UnsupportedOperationException tooManyAdapters = new UnsupportedOperationException("Too many Adapters for RecyclerView"); + private final UnsupportedOperationException AdapterNotSupportedException = new UnsupportedOperationException("Current adapter for the ListDialog is not supported for this method"); + private final NullPointerException nullAdapterException = new NullPointerException("The adapter for the ListDialog is null"); } diff --git a/SJDialog/src/main/java/com/sjapps/library/customdialog/adapter/DefaultListAdapter.java b/SJDialog/src/main/java/com/sjapps/library/customdialog/adapter/DefaultListAdapter.java index c0526b6..ae18461 100644 --- a/SJDialog/src/main/java/com/sjapps/library/customdialog/adapter/DefaultListAdapter.java +++ b/SJDialog/src/main/java/com/sjapps/library/customdialog/adapter/DefaultListAdapter.java @@ -23,7 +23,7 @@ public class DefaultListAdapter extends RecyclerView.Adapter itemClick.onClick(position,items[position])); } @@ -88,6 +95,13 @@ private void selectItem(@NonNull DefaultListAdapterGeneric.ViewHolder holder, in } + public void selectItem(int id) { + selected = id; + } + public int getSelectedItem(){ + return selected; + } + private void deselectItem(@NonNull DefaultListAdapterGeneric.ViewHolder holder, int position){ selectedItems.remove(items[position]); setItemResource(holder,itemBgRes); diff --git a/SJDialog/src/main/java/com/sjapps/library/customdialog/adapter/DefaultListAdapterGeneric.java b/SJDialog/src/main/java/com/sjapps/library/customdialog/adapter/DefaultListAdapterGeneric.java index fd9c9da..151124c 100644 --- a/SJDialog/src/main/java/com/sjapps/library/customdialog/adapter/DefaultListAdapterGeneric.java +++ b/SJDialog/src/main/java/com/sjapps/library/customdialog/adapter/DefaultListAdapterGeneric.java @@ -34,6 +34,7 @@ public class DefaultListAdapterGeneric extends RecyclerView.Adapter itemClick; ArrayList selectedItems; @@ -192,6 +193,13 @@ else if (isObjArr) return; } + if (selected != -1){ + if (selected == position) + selectItem(holder, null); + else + deselectItem(holder, null); + } + if (isArrList) holder.getView().setOnClickListener(v -> itemClick.onClick(position,arrayListItems.get(position))); @@ -262,6 +270,13 @@ private void selectItem(@NonNull DefaultListAdapterGeneric.ViewHolder holder, T } + public void selectItem(int id){ + selected = id; + } + public int getSelectedItem(){ + return selected; + } + private void deselectItem(@NonNull DefaultListAdapterGeneric.ViewHolder holder, T obj){ selectedItems.remove(obj); setItemResource(holder,itemBgRes); From 79509bef6f0078d8c1bec64348ec851760f81fc1 Mon Sep 17 00:00:00 2001 From: SlaVcE <102253539+SlaVcE14@users.noreply.github.com> Date: Sun, 16 Jul 2023 20:03:07 +0200 Subject: [PATCH 2/6] update build.gradle --- SJDialog/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SJDialog/build.gradle b/SJDialog/build.gradle index 19beeb0..cc24e2f 100644 --- a/SJDialog/build.gradle +++ b/SJDialog/build.gradle @@ -7,7 +7,7 @@ android { defaultConfig { minSdk 23 - versionCode 21 + versionCode 22 versionName "1.6.1 (dev 2)" buildConfigField 'int', 'VERSION_CODE', "${versionCode}" From c658baa3e83f21aa8f4e177d8344d4a677566a89 Mon Sep 17 00:00:00 2001 From: SlaVcE <102253539+SlaVcE14@users.noreply.github.com> Date: Sat, 22 Jul 2023 23:02:27 +0200 Subject: [PATCH 3/6] select item - image list adapter --- .../com/sjapps/library/customdialog/ListDialog.java | 6 +++++- .../adapter/DefaultImageListAdapter.java | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/SJDialog/src/main/java/com/sjapps/library/customdialog/ListDialog.java b/SJDialog/src/main/java/com/sjapps/library/customdialog/ListDialog.java index 7f211f7..5ab538a 100644 --- a/SJDialog/src/main/java/com/sjapps/library/customdialog/ListDialog.java +++ b/SJDialog/src/main/java/com/sjapps/library/customdialog/ListDialog.java @@ -904,8 +904,12 @@ public ListDialog selectItem(int id){ ((DefaultListAdapter) adapter).selectItem(id); return this; } + if (adapter instanceof DefaultImageListAdapter){ + ((DefaultImageListAdapter) adapter).selectItem(id); + return this; + } - throw AdapterNotSupportedException; + throw AdapterNotSupportedException; } /** diff --git a/SJDialog/src/main/java/com/sjapps/library/customdialog/adapter/DefaultImageListAdapter.java b/SJDialog/src/main/java/com/sjapps/library/customdialog/adapter/DefaultImageListAdapter.java index 0456f1a..b5fd459 100644 --- a/SJDialog/src/main/java/com/sjapps/library/customdialog/adapter/DefaultImageListAdapter.java +++ b/SJDialog/src/main/java/com/sjapps/library/customdialog/adapter/DefaultImageListAdapter.java @@ -27,6 +27,7 @@ public class DefaultImageListAdapter extends RecyclerView.Adapter itemClick; ArrayList selectedItems; @@ -101,6 +102,13 @@ public void onBindViewHolder(@NonNull ViewHolder holder, int position) { return; } + if (selected != -1){ + if (selected == position) + selectItem(holder, arrayListItems.get(position)); + else + deselectItem(holder, arrayListItems.get(position)); + } + holder.getView().setOnClickListener(v -> itemClick.onClick(position, arrayListItems.get(position))); } @@ -145,6 +153,10 @@ private void deselectItem(@NonNull ViewHolder holder,ImageListItem obj) { setItemColor(holder,listItemBgColor); } + public void selectItem(int id) { + selected = id; + } + private void setItemResource(@NonNull ViewHolder holder, int drawable) { holder.getView().setBackgroundResource(drawable); } From 5d0bf642a3619bceb14956978fbc5e461f2703e3 Mon Sep 17 00:00:00 2001 From: SlaVcE <102253539+SlaVcE14@users.noreply.github.com> Date: Sat, 22 Jul 2023 23:05:23 +0200 Subject: [PATCH 4/6] fix list item background color fix for DefaultListAdapterGeneric --- .../library/customdialog/adapter/DefaultListAdapterGeneric.java | 1 + 1 file changed, 1 insertion(+) diff --git a/SJDialog/src/main/java/com/sjapps/library/customdialog/adapter/DefaultListAdapterGeneric.java b/SJDialog/src/main/java/com/sjapps/library/customdialog/adapter/DefaultListAdapterGeneric.java index 151124c..92e0bf5 100644 --- a/SJDialog/src/main/java/com/sjapps/library/customdialog/adapter/DefaultListAdapterGeneric.java +++ b/SJDialog/src/main/java/com/sjapps/library/customdialog/adapter/DefaultListAdapterGeneric.java @@ -211,6 +211,7 @@ else if (isObjArr) private void setBackground(@NonNull DefaultListAdapterGeneric.ViewHolder holder, int position) { if (!isSelectable) { setItemResource(holder,itemBgRes); + setItemColor(holder,listItemBgColor); return; } if (isArrList) { From e7dbf303cbbf388e883385049bb8ae468f0cb273 Mon Sep 17 00:00:00 2001 From: SlaVcE <102253539+SlaVcE14@users.noreply.github.com> Date: Sat, 22 Jul 2023 23:15:13 +0200 Subject: [PATCH 5/6] fix list item background color - image fix for DefaultImageListAdapter --- .../library/customdialog/adapter/DefaultImageListAdapter.java | 1 + 1 file changed, 1 insertion(+) diff --git a/SJDialog/src/main/java/com/sjapps/library/customdialog/adapter/DefaultImageListAdapter.java b/SJDialog/src/main/java/com/sjapps/library/customdialog/adapter/DefaultImageListAdapter.java index b5fd459..4846a9a 100644 --- a/SJDialog/src/main/java/com/sjapps/library/customdialog/adapter/DefaultImageListAdapter.java +++ b/SJDialog/src/main/java/com/sjapps/library/customdialog/adapter/DefaultImageListAdapter.java @@ -116,6 +116,7 @@ public void onBindViewHolder(@NonNull ViewHolder holder, int position) { private void setBackground(@NonNull ViewHolder holder, int position) { if (!isSelectable) { setItemResource(holder, itemBgRes); + setItemColor(holder,listItemBgColor); return; } From fbf9f4d07f2cfcebc4409b3dfac1cedf8ab3765d Mon Sep 17 00:00:00 2001 From: SlaVcE <102253539+SlaVcE14@users.noreply.github.com> Date: Sat, 22 Jul 2023 23:50:32 +0200 Subject: [PATCH 6/6] Update ListDialogDoc.md --- SJDialog/ListDialogDoc.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/SJDialog/ListDialogDoc.md b/SJDialog/ListDialogDoc.md index 59d37fb..a6dd6ed 100644 --- a/SJDialog/ListDialogDoc.md +++ b/SJDialog/ListDialogDoc.md @@ -362,6 +362,10 @@ listDialog.setSelectableList(); //Set text color of an items in a list listDialog.setListItemTextColor(color); +//Set a background color for items in a list +listDialog.setListItemBackgroundColor(color); +//Set a background color for selected items in a list +listDialog.setListItemSelectedBackgroundColor(color); //Set a background resource for items in a list listDialog.setListItemBackgroundResource(drawable); //Set a background resource for selected items in a list