代码之家  ›  专栏  ›  技术社区  ›  Radaeld

在多个文本视图上滑动

  •  0
  • Radaeld  · 技术社区  · 6 年前

    我想在多个TextView上实现滑动功能,其中每个视图实现一个onclick侦听器。我的问题是,如果我将onTouch添加到每个文本视图中,由于它们又小又多,它们将无法识别两者之间的差异。我试图将ontouch添加到父布局中,但似乎因为它有文本视图,所以无法检测到对它们的滑动。

    有谁知道一个好方法来实现在文本视图上左右滑动,并且仍然保留对这些文本视图的一次单击?

    提前谢谢。

    1 回复  |  直到 6 年前
        1
  •  0
  •   ruben    6 年前

    给你一个主意,我有一个gridview(可以是任何 Layout )在有许多单元格的情况下,要检测对所有这些单元格的滑动,还要检测对每个单元格的单击,我有以下代码,当然您需要根据需要进行自定义:

    private GestureDetectorCompat gestureDetector;                                                               
    protected void onAttachments() {
        gestureDetector = new GestureDetectorCompat(this.context, new SingleTapConfirm());
        //get the width and height of the grid view
        final int cellWidth = calendarGridView.getWidth() / 7;
        final int cellHeight = calendarGridView.getHeight() / 5;
    
    
        calendarGridView.setOnTouchListener(new View.OnTouchListener() {
    
            @Override
            public boolean onTouch(View v, MotionEvent touchEvent) {
    
                int touchX = (int) touchEvent.getX();
                int touchY = (int) touchEvent.getY();
    
                if(touchEvent.getAction() == MotionEvent.ACTION_MOVE || gestureDetector.onTouchEvent(touchEvent)){
    
                    if (touchX <= calendarGridView.getWidth() && touchY <= calendarGridView.getHeight()) {
    
                        int cellX = (touchX / cellWidth);
                        int cellY = (touchY / cellHeight);
    
                        touchPosition = cellX + (7 * (cellY));
    
                        positionPrinter.setText("child: " + cellX + "," + cellY);
                        cellIDprinter.setText(Integer.toString(touchPosition));
    
                        // Do you stuff
                        ... ... ... 
                    }
    
                }
    
                return false;
            }
        });
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
    
        if (gestureDetector != null)
            gestureDetector.onTouchEvent(event);
    
        return super.onTouchEvent(event);
    }private class SingleTapConfirm extends GestureDetector.SimpleOnGestureListener {
    
        @Override
        public boolean onSingleTapUp(MotionEvent event) {
            return true;
        }
    }