在多个TextView上滑动 [英] Swiping Over Multiple TextViews

查看:84
本文介绍了在多个TextView上滑动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在多个textview上实现滑动功能,其中每个视图都实现一个onclick侦听器.我的问题是,如果我将onTouch添加到每个textview中,并且由于它们很小且很多,则它们将无法识别两者之间的差异.我试图将ontouch添加到父级布局中,但是似乎由于它具有textviews,因此无法检测到它们上的滑动.

I wanted to imlement the swipe function over multiple textviews, where each view implements a onclick Listener. My problem is if i add onTouch to each textview and since they are small and many, they wont be able to recognize teh diference in both. I've tried to add to the parent layout the ontouch but it seems that since it has textviews it doesnt detect swipe over them.

任何人都知道一种实现在文本视图上左右滑动并仍然保留这些onClick的好方法吗?

Anyone know a good way to implement swipe left and right over the textviews and still preserve the onClick of these?

谢谢.

推荐答案

为了给您一个主意,我有一个gridview(可以是任何 Layout ),其中有很多单元格,用于检测滑过所有这些单元格,还要检测每个单元格的点击,我有以下代码,当然,您需要根据需要自定义:

To give you an idea, I have a gridview (could be any Layout) where there are many cells, to detect swipe over all of these cells and also to detect click on each cell I have following code, Ofcourse you need to customize based on your need:

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;
    }
}

这篇关于在多个TextView上滑动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆