Currently, I have code to detect invisible view in ScrollView
// This is code for class extends from ScrollView
@Override
public void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
LinearLayout list = (LinearLayout)this.findViewById(R.id.card_container);
for (int i = 0; i < list.getChildCount(); ++i) {
View card = list.getChildAt(i);
list.getHitRect(mRect);
// If tag == 'false' and View is visible we know that
// View became visible during this scroll event.
if ((Boolean) card.getTag() == false
&& card.getLocalVisibleRect(mRect)) {
card.startAnimation(AnimationUtils.loadAnimation(getContext(),
R.anim.slide_up));
card.setTag(true);
}
}
}
To emulate Google+ card animation behavior. The first-time visible card will slide up, while users scrolls down.
In order to check whether a card is visible within scroll view, I'm using card.getLocalVisibleRect(mRect)
by following technique from Android: how to check if a View inside of ScrollView is visible?
This works well if my layout is as follow
<ScrollView>
<LinearLayout>
<LinearLayout card_container>
<Card />
<Card />
<Card />
<Card />
<Card />
</LinearLayout>
</LinearLayout>
</ScrollView>
According to the above code and layout, the system will know that the first time I scroll down, card starting from 4th position is not visible.
However, if I add some other views on the top of card
<ScrollView>
<LinearLayout>
<LinearLayout button_container>
<Button />
<Button />
<Button />
</Linear>
<LinearLayout card_container>
<Card />
<Card />
<Card />
<Card />
<Card />
</LinearLayout>
</LinearLayout>
</ScrollView>
After adding button containers, I expect the system let me know, the card starting from 2nd position is not visible (As some previous space is occupied by button container).
However, the system still let me know, card starting from 4th position is not visible. (which is not correct)
Is there any way I can fix this?