Android 如何在包含多个视图类型的ScrollView中检测不可见视图

Android 如何在包含多个视图类型的ScrollView中检测不可见视图,android,Android,目前,我在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.car

目前,我在
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);
        }
    }
}
根据上面的代码和布局,系统将知道我第一次向下滚动时,从第4个位置开始的卡不可见

但是,如果我在卡片顶部添加一些其他视图

<ScrollView>
    <LinearLayout>
        <LinearLayout button_container>
            <Button />
            <Button />
            <Button />
        </Linear>
        <LinearLayout card_container>
            <Card />
            <Card />
            <Card />
            <Card />
            <Card />
        </LinearLayout>
    </LinearLayout>
</ScrollView>

添加按钮容器后,我希望系统告诉我,从第二个位置开始的卡不可见(因为按钮容器占用了以前的一些空间)

但是,系统仍然让我知道,从第4位开始的卡不可见。(这是不正确的)


有什么办法可以解决这个问题吗?

我使用
getGlobalVisibleRect
。我不确定这是一个好的解决方案

// 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);

        // If tag == 'false' and View is visible we know that
        // View became visible during this scroll event.
        if ((Boolean) card.getTag() == false
                && card.getGlobalVisibleRect(mRect)) {
            card.startAnimation(AnimationUtils.loadAnimation(getContext(),
                    R.anim.slide_up));                
            card.setTag(true);
        }
    }
}
//这是从ScrollView扩展的类的代码
@凌驾
CrollChanged上的公共无效(int l、int t、int oldl、int oldt){
super.onScrollChanged(l,t,oldl,oldt);
LinearLayout list=(LinearLayout)this.findViewById(R.id.card\u容器);
对于(int i=0;i
// 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);

        // If tag == 'false' and View is visible we know that
        // View became visible during this scroll event.
        if ((Boolean) card.getTag() == false
                && card.getGlobalVisibleRect(mRect)) {
            card.startAnimation(AnimationUtils.loadAnimation(getContext(),
                    R.anim.slide_up));                
            card.setTag(true);
        }
    }
}