Skip to content

Commit

Permalink
Merge pull request #65 from SlaVcE14/ListDialog_selected-item
Browse files Browse the repository at this point in the history
ListDialog - select item
  • Loading branch information
SlaVcE14 authored Jul 22, 2023
2 parents 6494da5 + fbf9f4d commit b7288ba
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 4 deletions.
12 changes: 12 additions & 0 deletions SJDialog/ListDialogDoc.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions SJDialog/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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}\""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ public ListDialog hideEmptyListText() {
hideEmptyListTxt = true;
return this;
}

/**
* Set text to display when list is empty
* @param text Text
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -922,6 +972,8 @@ public <T> ArrayList<T> getSelectedItems() {
return (ArrayList<T>) selectedItems;
}



void checkListsSize(int size) {

if (hideEmptyListTxt) {
Expand All @@ -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");
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class DefaultImageListAdapter extends RecyclerView.Adapter<DefaultImageLi
int listItemBgColor;
int listItemBgColorSelected;
int textColor;
int selected = -1;

ListItemClickObj<ImageListItem> itemClick;
ArrayList<ImageListItem> selectedItems;
Expand Down Expand Up @@ -101,13 +102,21 @@ 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)));

}

private void setBackground(@NonNull ViewHolder holder, int position) {
if (!isSelectable) {
setItemResource(holder, itemBgRes);
setItemColor(holder,listItemBgColor);
return;
}

Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class DefaultListAdapter extends RecyclerView.Adapter<DefaultListAdapterG
int listItemBgColor;
int listItemBgColorSelected;
int textColor;

int selected = -1;

public DefaultListAdapter(String[] items,
boolean isSelectable,
Expand Down Expand Up @@ -77,6 +77,13 @@ public void onBindViewHolder(@NonNull DefaultListAdapterGeneric.ViewHolder holde
return;
}

if (selected != -1){
if (selected == position)
selectItem(holder, position);
else
deselectItem(holder, position);
}

holder.getView().setOnClickListener(v -> itemClick.onClick(position,items[position]));

}
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class DefaultListAdapterGeneric<T> extends RecyclerView.Adapter<DefaultLi
int listItemBgColor;
int listItemBgColorSelected;
int textColor;
int selected = -1;

ListItemClickObj<T> itemClick;
ArrayList<T> selectedItems;
Expand Down Expand Up @@ -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)));

Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b7288ba

Please sign in to comment.