如何检测Horizo​​ntalScrollView中的视图何时触及另一个视图?

Jim*_*mmy 2 android scrollview

所以我试图像滚动视图一样创建iMovie,我想在滚动视图中的图像触摸白线时得到一个回调(所以我可以改变大图像).我怎样才能做到这一点?

在此输入图像描述

Dro*_*Dev 7

这个解决方案对我有用,这也是我在对Horizo​​ntalScrollView的选项进行长期研究后能够得出的唯一解决方案.

ImageView lineImage =(ImageView)findViewById(R.id.yourImageViewIdOfWhiteLineImage);

然后在horizo​​ntalScrollView的onTouchListener中

public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()){
    case MotionEvent.ACTION_MOVE:
     //get horizontal scroll view for images
    HorizontalScrollView parent=(HorizontalScrollView)v;
    //get linear layout for list of images
    LinearLayout wholeList=(LinearLayout)parent.getChildAt(0);
    int centerCord[] = new int[2];
    for(int idx=0;idx<wholeList.getChildCount();idx++){
        ImageView discoveredIv=(ImageView)wholeList.getChildAt(idx);
        discoveredTv.getLocationInWindow(centerCord);
        int discoveredX=centerCord[0];
        //set these (lineImage.getLeft()-20, lineImage.getLeft()+20) according to your 
        //requirement, the way it best suits you. You'll have to test in runtime, how your 
        //views come under the line and then set these.
        if(discoveredX >= lineImage.getLeft()-20 && discoveredX <= lineImage.getLeft()+20)
        {
            //you have a winner, take that image from imageView and set it wherever you want.
            //Your image is in discoveredIv
        }
    }
        return false;
    case MotionEvent.ACTION_UP:
        //It returns true to avoid fling effect, otherwise this method won't work.
        return true;
    }
return false;
}
Run Code Online (Sandbox Code Playgroud)

这完全符合您的要求.但是我建议你在ACTION_UP的情况下使用这个代码,因为如果你在ACTION_MOVE中进行所有这些计算,那么这个代码有可能使你的应用程序在滚动时变慢,而且,更多的计算使应用程序变慢. 所以,我建议在ACTION_UP中使用它.

您可以在两种情况下测试您的应用,并决定您希望在哪种情况下使用此代码.