OnTouchListener用于整个屏幕

时间:2011-12-10 02:25:17

标签: java android android-layout motion-detection

我想在整个屏幕上安装一个OnTouchListener。我已尝试将所有视图附加到onTouchListener但生成错误的touchEvents。我知道这可以通过重写方法来实现,我正在寻找的是一个倾听者解决方案。谢谢!

可以使用手势监听器完成吗?

1 个答案:

答案 0 :(得分:1)

您可以使用侦听滑动操作的方法插入onSwipeListener类。然后,您可以为活动布局(LinearLayout / RelativeLayout)设置view.OnTouchListener,然后覆盖onSwipeListener的各种方法并插入各种任务。

下面是您可以创建的onSwipeListener类。

public class ListAdapter extends BaseAdapter {

    private Context con;
    private List<String> dataLt;
    private static LayoutInflater inflater = null;

    public ListAdapter(Context context, List<String> dataList){
        con         = context;
        dataLt      = dataList;
        inflater    = (LayoutInflater)con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return dataLt.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        ViewHolder holder;

        if(convertView==null){

            /****** Inflate tabitem.xml file for each row ( Defined below ) *******/
            vi = inflater.inflate(R.layout.list_item_search, null);
            /****** View Holder Object to contain tabitem.xml file elements ******/
            holder = new ViewHolder();
            holder.textView           = (TextView)    vi.findViewById(R.id.textView);
            /************  Set holder with LayoutInflater ************/
            vi.setTag( holder );
        }
        else
            holder=(ViewHolder)vi.getTag();

        return vi;
    }

    public static class ViewHolder{
        TextView textView;
    }
}

创建此类后,您可以按如下方式调用它:

public class OnSwipeTouchListener implements OnTouchListener {

    private final GestureDetector gestureDetector;

    public OnSwipeTouchListener (Context ctx){
        gestureDetector = new GestureDetector(ctx, new GestureListener());
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }

    private final class GestureListener extends SimpleOnGestureListener {

        private static final int SWIPE_THRESHOLD = 100;
        private static final int SWIPE_VELOCITY_THRESHOLD = 100;

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            boolean result = false;
            try {
                float diffY = e2.getY() - e1.getY();
                float diffX = e2.getX() - e1.getX();
                if (Math.abs(diffX) > Math.abs(diffY)) {
                    if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffX > 0) {
                            onSwipeRight();
                        } else {
                            onSwipeLeft();
                        }
                    }
                    result = true;
                }
                else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        onSwipeBottom();
                    } else {
                        onSwipeTop();
                    }
                }
                result = true;

            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        }
    }

    public void onSwipeRight() {
    }

    public void onSwipeLeft() {
    }

    public void onSwipeTop() {
    }

    public void onSwipeBottom() {
    }
}

希望这有帮助!