Skip to content

Commit

Permalink
Fixed
Browse files Browse the repository at this point in the history
java.util.NoSuchElementException
at java.util.LinkedList.getLast(LinkedList.java:257)
W/System.err: at segmented_control.widget.custom.android.com.segmentedcontrol.SegmentedControlControllerComponent.getLastSelectedViewHolder(SegmentedControlControllerComponent.java:103)
W/System.err: at segmented_control.widget.custom.android.com.segmentedcontrol.SegmentedControlControllerComponent.recreate(SegmentedControlControllerComponent.java:201)
W/System.err: at segmented_control.widget.custom.android.com.segmentedcontrol.SegmentedControlControllerComponent.notifyConfigIsChanged(SegmentedControlControllerComponent.java:156)
W/System.err: at segmented_control.widget.custom.android.com.segmentedcontrol.SegmentedControl.notifyConfigIsChanged(SegmentedControl.java:620)
  • Loading branch information
apikyanrobert committed Jun 20, 2019
1 parent d4f0acf commit 870a257
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 32 deletions.
2 changes: 0 additions & 2 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);

sc = findViewById(R.id.sc);
sc.setSupportedSelectionsCount(6);

sc.addSegments(getResources().getStringArray(R.array.segments));

sc.setColumnCount(3);
sc.notifyConfigIsChanged();

sc.addOnSegmentSelectListener(new OnSegmentSelectedListener<String>() {
@Override
public void onSegmentSelected(SegmentViewHolder<String> segmentViewHolder, boolean isSelected, boolean isReselected) {
Expand All @@ -32,5 +37,6 @@ public void onSegmentClick(SegmentViewHolder<String> segmentViewHolder) {
Log.d("Segment Click",segmentViewHolder.getSegmentData());
}
});

}
}
4 changes: 1 addition & 3 deletions preveiwapp/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
android:layout_height="wrap_content"
app:supportedSelectionsCount="3"
app:distributeEvenly="true"
app:selectionAnimationDuration="5000"
android:layout_margin="16dp"
app:radius="16dp"
app:textVerticalPadding="4dp"
app:segments="@array/segments"/>
app:textVerticalPadding="4dp"/>

</FrameLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

class SegmentedControlControllerComponent<D> extends ControllerComponent<SegmentedControlViewComponent<D>> {
private final Configs configs = Configs.getDefault();
// private SegmentViewHolder<D> lastClickedSegmentViewHolder;
// private SegmentViewHolder<D> lastClickedSegmentViewHolder;
private LinkedList<SegmentViewHolder<D>> selectedSegments = new LinkedList<>();
private final Notifier<D> notifier = new Notifier<>();
private final List<D> dataList = new ArrayList<>();
Expand Down Expand Up @@ -64,43 +64,44 @@ public void onSegmentClick(SegmentViewHolder<D> segmentViewHolder) {

int index = selectedSegments.indexOf(segmentViewHolder);
boolean contains = index != -1;
if (contains){
if (contains) {
// on section reselected
SegmentViewHolder<D> viewHolder = selectedSegments.get(index);
viewHolder.setSelected(true);
notifier.onSegmentSelected(segmentViewHolder,true,true);
} else if (notifier.onSegmentSelectRequest(segmentViewHolder)){
notifier.onSegmentSelected(segmentViewHolder, true, true);
} else if (notifier.onSegmentSelectRequest(segmentViewHolder)) {
// on section selected
// unSelect the last one
SegmentViewHolder<D> lastSelected = addSelectedSegmentViewHolder(segmentViewHolder);
if (lastSelected!=null){
if (lastSelected != null) {
lastSelected.setSelected(false);
notifier.onSegmentSelected(lastSelected,false,false);
notifier.onSegmentSelected(lastSelected, false, false);
}
// select the current
segmentViewHolder.setSelected(true);
notifier.onSegmentSelected(segmentViewHolder,true,false);
notifier.onSegmentSelected(segmentViewHolder, true, false);
}
}
};

/**
* Watch the supported selections count, if limit is reached the oldest segment will be removed from
* selections list and returned back to method caller
*
* @param segmentViewHolder new selected segment;
* @return oldest selection
*/
@Nullable
private SegmentViewHolder<D> addSelectedSegmentViewHolder(SegmentViewHolder<D> segmentViewHolder){
private SegmentViewHolder<D> addSelectedSegmentViewHolder(SegmentViewHolder<D> segmentViewHolder) {
selectedSegments.add(segmentViewHolder);
if (selectedSegments.size() > configs.supportedSelectionsCount){
if (selectedSegments.size() > configs.supportedSelectionsCount) {
return selectedSegments.remove(0);
}
return null;
}

private SegmentViewHolder<D> getLastSelectedViewHolder(){
return selectedSegments.getLast();
private SegmentViewHolder<D> getLastSelectedViewHolder() {
return selectedSegments.size() > 0 ? selectedSegments.getLast() : null;
}

private void addSegment(D segmentData) {
Expand Down Expand Up @@ -169,12 +170,12 @@ void notifyConfigIsChanged() {
// }

public void clearSelection(boolean notifySegmentSelectedListener) {
if (!selectedSegments.isEmpty()){
if (!selectedSegments.isEmpty()) {
for (SegmentViewHolder<D> selectedSegment : selectedSegments) {
selectedSegment.setSelected(false);

if (notifySegmentSelectedListener){
notifier.onSegmentSelected(selectedSegment,false,false);
if (notifySegmentSelectedListener) {
notifier.onSegmentSelected(selectedSegment, false, false);
}
}

Expand Down Expand Up @@ -274,11 +275,11 @@ void setUnSelectedBackgroundColor(int color) {
configs.segmentDecoration.unSelectedBackgroundColor = color;
}

void setFocusedBackgroundColor(int color){
void setFocusedBackgroundColor(int color) {
configs.segmentDecoration.focusedBackgroundColor = color;
}

void setSelectionAnimationDuration(int duration){
void setSelectionAnimationDuration(int duration) {
configs.segmentDecoration.selectionAnimationDuration = duration;
}

Expand Down Expand Up @@ -369,7 +370,7 @@ void setDistributeEvenly(boolean willDistributeEvenly) {
configs.willDistributeEvenly = willDistributeEvenly;
}

void setSupportedSelectionsCount(int supportedSelectionsCount){
void setSupportedSelectionsCount(int supportedSelectionsCount) {
configs.supportedSelectionsCount = supportedSelectionsCount;
}

Expand Down Expand Up @@ -410,24 +411,25 @@ void setSelectedSegment(int column, int row) {
// return lastClickedSegmentViewHolder;
// }

List<SegmentViewHolder<D>> getSelectedViewHolders(){
List<SegmentViewHolder<D>> getSelectedViewHolders() {
return selectedSegments;
}

int[] getLastSelectedColumnAndRow() {
SegmentViewHolder<D> lastSelection = getLastSelectedViewHolder();
if (lastSelection!=null){
return new int[]{lastSelection.getColumn(),lastSelection.getRow()};
}else{
return new int[]{-1,-1};
if (lastSelection != null) {
return new int[]{lastSelection.getColumn(), lastSelection.getRow()};
} else {
return new int[]{-1, -1};
}
}
//

//
int getLastSelectedAbsolutePosition() {
SegmentViewHolder<D> lastSelection = getLastSelectedViewHolder();
if (lastSelection!=null){
if (lastSelection != null) {
return lastSelection.getAbsolutePosition();
}else{
} else {
return -1;
}
}
Expand All @@ -436,7 +438,7 @@ int getLastSelectedAbsolutePosition() {
// return lastClickedSegmentViewHolder != null;
// }

boolean isSelected(){
boolean isSelected() {
return !selectedSegments.isEmpty();
}

Expand Down

0 comments on commit 870a257

Please sign in to comment.