diff --git a/SJDialog/ListDialogDoc.md b/SJDialog/ListDialogDoc.md index 233a1c7..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 @@ -393,6 +397,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..cc24e2f 100644 --- a/SJDialog/build.gradle +++ b/SJDialog/build.gradle @@ -7,8 +7,8 @@ android { defaultConfig { minSdk 23 - versionCode 21 - versionName "1.6.1 (dev 1)" + versionCode 22 + 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..5ab538a 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,52 @@ 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; + } + if (adapter instanceof DefaultImageListAdapter){ + ((DefaultImageListAdapter) 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 +972,8 @@ public ArrayList getSelectedItems() { return (ArrayList) selectedItems; } + + void checkListsSize(int size) { if (hideEmptyListTxt) { @@ -944,4 +996,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/DefaultImageListAdapter.java b/SJDialog/src/main/java/com/sjapps/library/customdialog/adapter/DefaultImageListAdapter.java index 0456f1a..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 @@ -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))); } @@ -108,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; } @@ -145,6 +154,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); } 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..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 @@ -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))); @@ -203,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) { @@ -262,6 +271,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);