Skip to content

Commit

Permalink
Show & hide tab indicator feature in Carousel View
Browse files Browse the repository at this point in the history
Implemented show & hide tab indicator feature in Crousel View
Clean-up some codes
  • Loading branch information
sung2063 committed Mar 21, 2021
1 parent cc72bad commit da8a357
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .idea/gradle.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 @@ -23,6 +23,7 @@ public class CarouselHandler {
private Context context;
private List<ViewGroup> slideList;
private int scrollDirection;
private boolean isShowingIndicator;
private boolean isShowingSlideNumber;

// =============================================================================================
Expand All @@ -45,9 +46,10 @@ public CarouselHandler() {
// Default Constructor
}

public CarouselHandler(Context context, int scrollDirection, boolean isShowingSlideNumber) {
public CarouselHandler(Context context, int scrollDirection, boolean isShowingIndicator, boolean isShowingSlideNumber) {
this.context = context;
this.scrollDirection = scrollDirection;
this.isShowingIndicator = isShowingIndicator;
this.isShowingSlideNumber = isShowingSlideNumber;
}

Expand All @@ -69,8 +71,9 @@ public List<ViewGroup> getSlideList() {
* @throws SlideOutOfBoundException on list size is greater than 10
*/
public void setSlideList(List<ViewGroup> slideList) throws SlideOutOfBoundException {
if (slideList.size() > 10)
if (slideList.size() > 10) {
throw new SlideOutOfBoundException(context.getString(R.string.exceed_slide_boundary_error));
}
this.slideList = slideList;
}

Expand All @@ -90,6 +93,22 @@ public void setScrollDirection(int scrollDirection) {
this.scrollDirection = scrollDirection;
}

/**
* Returns the value of showing tab indicator
* @return true if showing the tab indicator, otherwise false
*/
protected boolean isShowingIndicator() {
return isShowingIndicator;
}

/**
* Set the value of tab indicator
* @param isShowingIndicator boolean value for showing the tab indicator
*/
protected void showIndicator(boolean isShowingIndicator) {
this.isShowingIndicator = isShowingIndicator;
}

/**
* Returns the value of showing slide number
* @return true if showing the slide number, otherwise false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ public CarouselView(Context context, @Nullable AttributeSet attrs) {
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CarouselView, 0, 0);
try {
int scrollDirection = typedArray.getInt(R.styleable.CarouselView_scrollDirection, CarouselHandler.CAROUSEL_HORIZONTAL_DIRECTION);
boolean isShowingIndicator = typedArray.getBoolean(R.styleable.CarouselView_showIndicator, false);
boolean isShowingSlideNumber = typedArray.getBoolean(R.styleable.CarouselView_showSlideNumber, false);
carouselHandler = new CarouselHandler(context, scrollDirection, isShowingSlideNumber);
carouselHandler = new CarouselHandler(context, scrollDirection, isShowingIndicator, isShowingSlideNumber);
} finally {
typedArray.recycle();
init();
Expand All @@ -88,6 +89,7 @@ protected void init() {

// Setup Layout
setupLayoutDirection(carouselHandler.getScrollDirection());
setupIndicator(carouselHandler.isShowingIndicator());
setupSlideNumber(carouselHandler.isShowingSlideNumber());

}
Expand Down Expand Up @@ -118,8 +120,9 @@ private void repositioningVerticalTab() {
*/
private void setupLayoutDirection(int layoutDirection) {

if (rootLayout != null)
if (rootLayout != null) {
removeView(rootLayout); // Remove layout first
}

LayoutInflater inflater = LayoutInflater.from(context);
if (layoutDirection == CarouselHandler.CAROUSEL_HORIZONTAL_DIRECTION) {
Expand All @@ -141,6 +144,18 @@ private void setupLayoutDirection(int layoutDirection) {

}

/**
* Setup the tab indicator
* @param isShowingIndicator boolean value for showing the tab indicator
*/
private void setupIndicator(boolean isShowingIndicator) {
if (isShowingIndicator) {
tabIndicator.setVisibility(VISIBLE); // Show the indicator
} else {
tabIndicator.setVisibility(GONE); // Does not use the indicator
}
}

/**
* Setup the slide number view
* @param isShowingSlideNumber boolean value for showing the slide number
Expand All @@ -161,8 +176,7 @@ public void onPageSelected(int index) {
});

} else {
// Does not use the slide number
tvPageNum.setVisibility(GONE);
tvPageNum.setVisibility(GONE); // Does not use the slide number
}

}
Expand Down Expand Up @@ -230,6 +244,23 @@ public void setScrollDirection(int scrollDirection) {
carouselHandler.setScrollDirection(scrollDirection);
}

/**
* Returns the value of showing tab indicator
* @return true if showing the tab indicator, otherwise false
*/
public boolean isShowingIndicator() {
return carouselHandler.isShowingIndicator();
}

/**
* Set the value of tab indicator
* @param isShowingIndicator boolean value for showing the tab indicator
*/
public void showIndicator(boolean isShowingIndicator) {
setupIndicator(isShowingIndicator);
carouselHandler.showIndicator(isShowingIndicator);
}

/**
* Returns the value of showing slide number
* @return true if showing the slide number, otherwise false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ protected long getDelayTimePeriod() {
* @exception IllegalArgumentException on delay time is set less than 0 or greater than 10
*/
protected void setDelayTimePeriod(int delayTimePeriod) throws IllegalArgumentException {
if ((delayTimePeriod < 0) || (delayTimePeriod > 10))
if (delayTimePeriod < 0 || delayTimePeriod > 10) {
throw new IllegalArgumentException(context.getString(R.string.slide_delay_time_illegal_error));
}
this.delayTimePeriod = delayTimePeriod;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ public SlideshowView(Context context, @Nullable AttributeSet attrs) throws Illeg
int delayTimePeriod = typedArray.getInt(R.styleable.SlideshowView_delayTimePeriod, 5);

// Check illegal exception
if ((delayTimePeriod < 0) || (delayTimePeriod > 10))
if (delayTimePeriod < 0 || delayTimePeriod > 10) {
throw new IllegalArgumentException(context.getString(R.string.slide_delay_time_illegal_error));
}

slideshowHandler = new SlideshowHandler(context, isShowingIndicator, isShowingSlideNumber, delayTimePeriod);

Expand Down Expand Up @@ -114,12 +115,11 @@ private void setPageNumber(int position) {
* @param isShowingIndicator boolean value for showing the tab indicator
*/
private void setupIndicator(boolean isShowingIndicator) {

if (isShowingIndicator)
if (isShowingIndicator) {
tabIndicator.setVisibility(VISIBLE); // Show the indicator
else
} else {
tabIndicator.setVisibility(GONE); // Does not use the indicator

}
}

/**
Expand All @@ -140,8 +140,9 @@ public void onPageSelected(int index) {
tvPageNum.setText(position + " / " + slideshowHandler.getSlideList().size());
}
});
} else
} else {
tvPageNum.setVisibility(GONE); // Does not use the slide number
}

}

Expand All @@ -162,14 +163,16 @@ public void run() {
public void run() {

int slideIndex;
if (vpSlider.getCurrentItem() < slideshowHandler.getSlideList().size() - 1)
if (vpSlider.getCurrentItem() < slideshowHandler.getSlideList().size() - 1) {
slideIndex = vpSlider.getCurrentItem() + 1;
else
} else {
slideIndex = 0;
}
vpSlider.setCurrentItem(slideIndex);

if (slideshowHandler.showSlideNumber())
if (slideshowHandler.showSlideNumber()) {
setPageNumber(slideIndex + 1); // Update only when slide is enabled
}

}
});
Expand All @@ -187,18 +190,21 @@ public void launch() throws SlideNullPointerException {
SlideAdapter slideAdapter = new SlideAdapter(slideList);
vpSlider.setAdapter(slideAdapter);

if (slideshowHandler.isShowingIndicator())
if (slideshowHandler.isShowingIndicator()) {
tabIndicator.setupWithViewPager(vpSlider, true); // Setup indicator only when it is on
}

if (slideshowHandler.showSlideNumber())
if (slideshowHandler.showSlideNumber()) {
setPageNumber(1); // Update only when slide is enabled
}

long periodTime = slideshowHandler.getDelayTimePeriod() * 1000;
Timer sliderTimer = new Timer();
sliderTimer.scheduleAtFixedRate(new SlideshowTimer(), 4000, periodTime);

} else
} else {
throw new SlideNullPointerException(context.getString(R.string.list_null_error)); // Null Exception
}

}

Expand Down
4 changes: 3 additions & 1 deletion Sliders/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<resources>

<!-- Commonly Used -->
<attr name="showIndicator" format="boolean" />
<attr name="showSlideNumber" format="boolean"/>

<!-- XML attributes for Carousel View -->
Expand All @@ -12,14 +13,15 @@
<enum name="vertical" value="1" />
</attr>

<attr name="showIndicator" />
<attr name="showSlideNumber" />

</declare-styleable>

<!-- XML attributes for Slideshow View -->
<declare-styleable name="SlideshowView">

<attr name="showIndicator" format="boolean"/>
<attr name="showIndicator" />
<attr name="showSlideNumber" />
<attr name="delayTimePeriod" format="integer" />

Expand Down

0 comments on commit da8a357

Please sign in to comment.