向上滑动显示视图

时间:2014-11-19 08:32:28

标签: android drag motionevent

我是android开发的新手。我想知道当用户向上滑动时我将如何显示视图。 看我的布局。! Image of Layout

现在,当用户向上滑动蓝色视图时,我希望显示一个视图,当用户向下滑动时,视图应该消失。 由于我知道并尝试在谷歌上找到我没有取得任何成功所以请帮助我

我尝试过这样做,但没有发生任何事情

public class MainActivity extends ActionBarActivity {

    private LinearLayout slideUp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        slideUp = (LinearLayout) findViewById(R.id.ll_slideUp);

        slideUp.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent touchevent) {
                float x1 = 0, y1 = 0, x2, y2;
                switch (touchevent.getAction()) {
                    // when user first touches the screen we get x and y coordinate

                    case MotionEvent.ACTION_DOWN: {
                        x1 = touchevent.getX();
                        y1 = touchevent.getY();
                        break;
                    }
                    case MotionEvent.ACTION_UP: {
                        x2 = touchevent.getX();
                        y2 = touchevent.getY();

                        //if left to right sweep event on screen
                        if (x1 < x2) {
                            Toast.makeText(MainActivity.this, "Left to Right Swap Performed", Toast.LENGTH_LONG).show();
                        }

                        // if right to left sweep event on screen
                        if (x1 > x2) {
                            Toast.makeText(MainActivity.this, "Right to Left Swap Performed", Toast.LENGTH_LONG).show();
                        }

                        // if UP to Down sweep event on screen
                        if (y1 < y2) {
                            Toast.makeText(MainActivity.this, "UP to Down Swap Performed", Toast.LENGTH_LONG).show();
                        }

                        //if Down to UP sweep event on screen
                        if (y1 > y2) {
                            Toast.makeText(MainActivity.this, "Down to UP Swap Performed", Toast.LENGTH_LONG).show();  // Show your view here when use slides up
                        }
                        break;
                    }
                }
                return false;
            }

        });
    }


}

1 个答案:

答案 0 :(得分:0)

float x1,y1,x2,y2;
 view.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent touchevent) {
             switch (touchevent.getAction())
             {
                    // when user first touches the screen we get x and y coordinate
                     case MotionEvent.ACTION_DOWN: 
                     {
                         x1 = touchevent.getX();
                         y1 = touchevent.getY();
                         break;
                    }
                     case MotionEvent.ACTION_UP: 
                     {
                         x2 = touchevent.getX();
                         y2 = touchevent.getY(); 

                         //if left to right sweep event on screen
                         if (x1 < x2) 
                         {
                             Toast.makeText(this, "Left to Right Swap Performed", Toast.LENGTH_LONG).show();
                          }

                         // if right to left sweep event on screen
                         if (x1 > x2)
                         {
                             Toast.makeText(this, "Right to Left Swap Performed", Toast.LENGTH_LONG).show();
                         }

                         // if UP to Down sweep event on screen
                         if (y1 < y2) 
                         {
                             Toast.makeText(this, "UP to Down Swap Performed", Toast.LENGTH_LONG).show();
                         }

                         //if Down to UP sweep event on screen
                         if (y1 > y2)
                         {
                             Toast.makeText(this, "Down to UP Swap Performed", Toast.LENGTH_LONG).show();  // Show your view here when use slides up
                          }
                         break;
                     }
             }
             return false;
        }

    });