Android onTouch活动

时间:2015-02-25 12:23:15

标签: android

我已设法创建此代码以在列表视图中滑入或滑出按钮。如果我在手机屏幕上的手势是水平的,它可以正常工作。但如果我的手势也是对角线......它不会触发显示/隐藏按钮。手势是水平的(从左到右或从右到左),但它可以是一个小对角线。该怎么办 ?

类show实现了OnTouchListener      {

    @Override
    public boolean onTouch(View v, MotionEvent event) 
    {
        int action = event.getAction();
        switch (action) 
        {
            case MotionEvent.ACTION_DOWN:
                action_down_x = (int) event.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                action_up_x = (int) event.getX();
                difference = action_down_x - action_up_x;
                break;
            case MotionEvent.ACTION_UP:
                if (difference < -30)
                {
                    Animation slide_up = AnimationUtils.loadAnimation(mContext, R.anim.slide_stanga);
                    LinearLayout right = (LinearLayout) v.findViewById(R.id.dreapta);
                    if (right.isShown())
                    {
                    right.startAnimation(slide_up);
                    right.setVisibility(View.GONE);
                    final View x = v;
                    Handler handler = new Handler();
                    handler.postDelayed(new Runnable() 
                    {
                        public void run() 
                        {
                            LinearLayout left = (LinearLayout) x.findViewById(R.id.stanga);
                            left.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,(float) 4.0));
                        }
                    }, 500);

                    }

                }
                if (difference > 30)
                {
                     Animation slide_up = AnimationUtils.loadAnimation(mContext, R.anim.slide_dreapta);
                     LinearLayout right = (LinearLayout) v.findViewById(R.id.dreapta);
                     if (!right.isShown())
                        { 
                         LinearLayout left = (LinearLayout) v.findViewById(R.id.stanga);
                         left.setLayoutParams(new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT,(float) 3.0));
                         right.setLayoutParams(new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT,(float) 1.0));
                         right.startAnimation(slide_up);
                         right.setVisibility(View.VISIBLE);
                        }
                }
                action_down_x = 0;
                action_up_x = 0;
                difference = 0;
                break;
            }
            return true;
    }

}

谢谢!

1 个答案:

答案 0 :(得分:1)

没有内置选项可以执行此操作。我认为你应该使用Gesture Detector。获取屏幕的初始和最终坐标,然后按照您的方式编码。