How To Detect When A View Inside Horizontalscrollview Touches Another View?
Solution 1:
This solution works for me and also this is the only solution I was able to make out after a long research on options we have with HorizontalScrollView.
ImageView lineImage=(ImageView)findViewById(R.id.yourImageViewIdOfWhiteLineImage);
Then in onTouchListener of your horizontalScrollView
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stubswitch (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[] = newint[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
}
}
returnfalse;
case MotionEvent.ACTION_UP:
//It returns true to avoid fling effect, otherwise this method won't work.returntrue;
}
returnfalse;
}
This is strictly according to your requirement. But I'd suggest you use this code in ACTION_UP case, because if you do all these calculations in ACTION_MOVE then this code has tendency to make your app slow while scrolling and also, more the calculations more the app slows down. so, i suggest to use this in ACTION_UP.
You can test your app in both cases and decide in which case you want this code.
Solution 2:
Set OnTouchListener for imageview, then u ve to implement below callback. where event reference will give everything what u need
publicbooleanonTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub// event reference will give every co-ordinate that u needreturnfalse;
}
Solution 3:
You are probably going to want to extend the parent that scrolls that reports its location on screen as it is being touched. I'm not sure from your screenshot if the while line, the image reel, or both scroll, so I am going to assume the white line is fixed and the images scroll (although your solution could be adapted accordingly).
In a custom view that scrolls the ImageViews:
/* @return True if the event was handled, false otherwise. */public boolean onTouchEvent (MotionEvent event) {
switch(event.getActionMasked()) {
case MotionEvent.ACTION_MOVE: {
x = event.getRawX();
// This is the x-position on screen, compare to that of while line.
As the finger moves, you will intercept it's change in position. I assume all ImageView
s are in a parent container that scrolls them horizontally. If you need to know which child the finger is hovering above, take a look at at the previous question:
how to get a view from an event coordinates in android?. Using these method you can know as you scroll which ImageView
is overlapping with the white line, and update the large image accordingly.
Hope this helps!
Post a Comment for "How To Detect When A View Inside Horizontalscrollview Touches Another View?"