获取项目的ListView控件只能用OnTouchListener只能用、控件、项目、OnTouchListener

2023-09-05 09:47:43 作者:百毒不侵°

在我的应用程序有一个Touchlistener为我的ListView。随着TouchListener我能够得到X和Y的触摸事件的坐标。现在,我想从ListView中获取点击的项目。我怎样才能获得点击的项目的位置在ListView只能用onTouchListener?我不希望使用onItemClickedListener。

解决方案

 开关(motionEvent.getActionMasked()){
            案例MotionEvent.ACTION_DOWN:{
                如果(mPaused){
                    返回false;
                }

                // TODO:确保这是一个手指,并设置一个标志

                //查找被感动了子视图(执行命中测试)
                矩形RECT =新的矩形();
                INT childCount = mListView.getChildCount();
                INT [] listViewCoords =新INT [2];
                mListView.getLocationOnScreen(listViewCoords);
                INT X =(int)的motionEvent.getRawX() -  listViewCoords [0];
                INT Y =(int)的motionEvent.getRawY() -  listViewCoords [1];
                查看子女;
                的for(int i = 0; I< childCount;我++){
                    孩子= mListView.getChildAt(我);
                    child.getHitRect(RECT);
                    如果(rect.contains(X,Y)){
                        mDownView =子女; //这是你往下看
                        打破;
                    }
                }

                如果(mDownView!= NULL){
                    mDownX = motionEvent.getRawX();
                    mDownPosition = mListView.getPositionForView(mDownView);

                    mVelocityTracker = VelocityTracker.obtain();
                    mVelocityTracker.addMovement(motionEvent);
                }
                view.onTouchEvent(motionEvent);
                返回true;
            }
 

我是怎么得来的SwipeListView杰克沃顿

Vb中的ListView控件怎样使用

In my application I have a Touchlistener for my ListView. With the TouchListener I'm able to get the X and Y coordinates from the touch event. Now I want to get the clicked item from the ListView. How can I get the position of the click item in the listView only with an onTouchListener? I do not want to use a onItemClickedListener.

解决方案

switch (motionEvent.getActionMasked()) {
            case MotionEvent.ACTION_DOWN: {
                if (mPaused) {
                    return false;
                }

                // TODO: ensure this is a finger, and set a flag

                // Find the child view that was touched (perform a hit test)
                Rect rect = new Rect();
                int childCount = mListView.getChildCount();
                int[] listViewCoords = new int[2];
                mListView.getLocationOnScreen(listViewCoords);
                int x = (int) motionEvent.getRawX() - listViewCoords[0];
                int y = (int) motionEvent.getRawY() - listViewCoords[1];
                View child;
                for (int i = 0; i < childCount; i++) {
                    child = mListView.getChildAt(i);
                    child.getHitRect(rect);
                    if (rect.contains(x, y)) {
                        mDownView = child; // This is your down view
                        break;
                    }
                }

                if (mDownView != null) {
                    mDownX = motionEvent.getRawX();
                    mDownPosition = mListView.getPositionForView(mDownView);

                    mVelocityTracker = VelocityTracker.obtain();
                    mVelocityTracker.addMovement(motionEvent);
                }
                view.onTouchEvent(motionEvent);
                return true;
            }

i am get it from SwipeListView Jake Wharton