Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for managing number replays & removing childViews #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ for (int index = 0; index < 50; index++) {
tickerView.addChildView(tv);
}

// Call setReplays(replays) to set numper of times to loop/scroll entire messages

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

numper -> number

tickerView.setReplays(replays)

// Call removeChildViews to remove all child views
tickerView.removeChildViews()

// Call the showTickers() to show them on the screen
tickerView.showTickers();

Expand Down
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ org.gradle.jvmargs=-Xmx1536m
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true

version=1.0
Empty file modified gradlew
100644 → 100755
Empty file.
69 changes: 49 additions & 20 deletions tickerview/src/main/java/com/vinay/ticker/lib/TickerView.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public class TickerView extends HorizontalScrollView {
private TextView lastTicker;
private List<View> childViews = null;
private LinearLayout linearLayout;

private Integer replays;
private Integer loop = 1;
Comment on lines +40 to +41

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use primitive type for better memory usage?

Suggested change
private Integer replays;
private Integer loop = 1;
private int replays;
private int loop = 1;

private boolean replaysCompleted = false;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can remove explicit assignment to false since it's already the default value

Suggested change
private boolean replaysCompleted = false;
private boolean replaysCompleted;


public TickerView(Context context) {
super(context);
Expand All @@ -60,6 +62,13 @@ public TickerView(Context context, AttributeSet attrs, int defStyleAttr, int def
init(context, null);
}

/**
* Use this method to set the number of times the tickerView/scolling all messages should happen
*/
public void setReplays(Integer replays) {
this.replays = replays;
}

/**
* If the views are added to the container view, the tickers start showing up. This method calls {@code showTickers()}
*/
Expand All @@ -78,7 +87,6 @@ protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
}


/**
* This method initialized the View container by adding a horizontal {@code LinearLayout} onside the root view.
*
Expand Down Expand Up @@ -117,7 +125,6 @@ public void setDisplacement(int displacement) {
this.displacement = (int) Math.ceil((displacement) * 5.0 / 100.0);
}


/**
* Saves the views collection to be plotted in the ticker view.
*
Expand All @@ -139,6 +146,13 @@ public void addChildView(View childView) {
this.childViews.add(childView);
}

/**
*
*/
Comment on lines +149 to +151

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove empty comment

public void removeChildViews() {
this.childViews = null;
}

/**
* This method to show the child views added by {@code addChildView()} or {@code setChildViews(List<View> childViews)} methods.
* Method Details:
Expand All @@ -149,6 +163,7 @@ public void addChildView(View childView) {
* 5. We add {@code ViewTreeObserver.OnGlobalLayoutListener} to intercept changes in global layout and start auto scrolling by calling {@code startAutoScrolling()} method.
*/
public void showTickers() {
replaysCompleted = false;
if (linearLayout != null) {
linearLayout.removeAllViews();
}
Expand Down Expand Up @@ -237,31 +252,45 @@ public void run() {
* <p>
* The logic in place is to get a {@code Rect} and set it for custom last marker ticker(invisible). An additional {@code Rect} object is created whose horizontal bounds are
* for how much the view has scrolled added to its width. Once we have both of these objects, we simply check if the {@code Rect}object for last custom ticker view (invisible)
* intersects with the current screen {@code Rect} object, as soon as the condition is met, which means the last hidden view has scrolled on to the screen, which in tern means all the ticker items have been shown, we set the scroll position to 0 again,, and the scrolling starts from the first Tivker view again.
* intersects with the current screen {@code Rect} object, as soon as the condition is met, which means the last hidden view has scrolled on to the screen, which in tern means all the ticker items have been shown, we set the scroll position to 0 again,, and the scrolling starts from the first Ticker view again.
*/
public void moveScrollView() {

try {
scrollPos = (int) (getScrollX() + displacement);

final Rect bounds = new Rect();
lastTicker.getHitRect(bounds);

final Rect scrollBounds = new Rect(getScrollX(), getScrollY(), getScrollX()
+ getWidth(), getScrollY() + getHeight());

if (Rect.intersects(scrollBounds, bounds)) {
// is visible
scrollPos = 0;
scrollTo(scrollPos, 0);
} else {
smoothScrollTo(scrollPos, 0);
if(!replaysCompleted) {
boolean continuing = scrollPos == -1;
scrollPos = continuing ? 0 : (int) (getScrollX() + displacement);
final Rect bounds = new Rect();
lastTicker.getHitRect(bounds);

final Rect scrollBounds = new Rect(getScrollX(), getScrollY(), getScrollX()
+ getWidth(), getScrollY() + getHeight());

if (Rect.intersects(scrollBounds, bounds)) {
if(continuing) {
loop = 1;
restartScroll();
} else if(replays >= loop) {
loop++;
restartScroll();
} else {
replaysCompleted = true;
scrollPos = -1;// sets a tracker the picking up a next call after a former replays end
}
} else {
smoothScrollTo(scrollPos, 0);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

private void restartScroll() {
// is visible
scrollPos = 0;
scrollTo(scrollPos, 0);
}

/**
* This method cancels {@code Timer} and {@code TimerTask} and there callback.
*/
Expand Down Expand Up @@ -291,4 +320,4 @@ private void clearTimerTaks(TimerTask timerTask) {
timerTask.cancel();
}
}
}
}