OnTouchEvent how to get view under your finger

this is my code

public class Main extends Activity {  

    @Override  
    public void onCreate(Bundle savedInstanceState)  
    {  
        super.onCreate(savedInstanceState);   
        LinearLayout rLinear=new LinearLayout(this);  
        rLinear.setId(200);  

        for (int c=0;c<5;c++)  
        {  
            ImageView imEdit = new ImageView(this);  
            imEdit.setId(300+c);  
            imEdit.setImageResource(R.drawable.a);  
            imEdit.setLayoutParams(new LayoutParams(36, 36));  

            rLinear.addView(imEdit);
        }
    }  
}  

Ok, I have this code, I add 5 ImageView to the linear layout at runtime, how good it is, but what I would like to do is when the user slides a finger on one of them, I would like to change the image, so what my question will be, how can I find out what the current image is under the user's finger. ??

Thank.

+3
source share
4 answers

This is what I used to find the first view without looking at the group under my finger. It is recursive and uses getGlobalVisibleRect

private View findViewAtPosition(View parent, int x, int y) {
    if (parent instanceof ViewGroup) {
        ViewGroup viewGroup = (ViewGroup)parent;
        for (int i=0; i<viewGroup.getChildCount(); i++) {
            View child = viewGroup.getChildAt(i);
            View viewAtPosition = findViewAtPosition(child, x, y);
            if (viewAtPosition != null) {
                return viewAtPosition;
            }
        }
        return null;
    } else {
        Rect rect = new Rect();
        parent.getGlobalVisibleRect(rect);
        if (rect.contains(x, y)) {
            return parent;
        } else {
            return null;
        }
    }
}

Usage example:

public boolean onTouchEvent(MotionEvent ev) {
    Log.i(TAG, "View under finger: " + findViewAtPosition(getRootView(), (int)ev.getRawX(), (int)ev.getRawY()));
}
+9
source

To apply

    rLinear.setOnTouchListener(new OnTouchListener() {@Override
    public boolean onTouch(View v, MotionEvent event) {
        String eventName = "";

        LinearLayout layout = (LinearLayout)v;

        for(int i =0; i< layout.getChildCount(); i++)
        {
            View view = layout.getChildAt(i);


            Rect outRect = new Rect(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());

            if(outRect.contains((int)event.getX(), (int)event.getY()))
            {
                                  Log.d("", view.getId());
            }
        }
    }});

ps: this code button is a mess

+3
source

, StateListDrawables ImageView XML , ,

http://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html

.
< android = "http://schemas.android.com/apk/res/android" >
< item state_pressed = "true" drawable = "@drawable/redbutton" >
< item state_focused = "true" drawable = "@drawable/redbutton" >
< item drawable = "@drawable/bluebutton" >
</ >

0

I think you should use the Layout mentor .. look at the codes below ...

LinearLayout lin_layt = (LinearLayout) findViewById(R.id.list_layout);

final FrameLayout fl[]=new FrameLayout[size];
View ResultList=new View(Result.this);
             ResultList=layoutInflater.inflate(getResources().getLayout(R.layout.result_list), null);
             fl[i] = new FrameLayout(this);
             fl[i] = (FrameLayout) ResultList.findViewById(R.id.FrameLayout_middle);

//                              
                fl[i].setOnTouchListener(new OnTouchListener(){

                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        switch(event.getAction()){
                        case MotionEvent.ACTION_DOWN:
                            fl[x].setBackgroundResource(R.drawable.middlestripselect);
                            return true;
                        case MotionEvent.ACTION_UP:
                            fl[x].setBackgroundResource(R.drawable.middlestrip);




                            return true;
                        }
                        return false;
                    }

                });



             lin_layt.addView(ResultList);
0
source

Source: https://habr.com/ru/post/1775792/


All Articles