Dynamic view with swiping horizontally and vertically

早过忘川 提交于 2019-11-30 07:32:56
Amit Gupta

There is a way to do this with out using any lib.

In your xml file design another layout and put all the widget which you want to show at the time of sliding after that apply sliding animation like left to right and up to down as you want. After that with the help of gesture detector you can get the event of sliding, and perform your task.

Here is my code

Animation Left slide
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fromXDelta="100%"
    android:toXDelta="0%" >
</translate>

Animation Right slide
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fromXDelta="-100%"
    android:toXDelta="0%" >
</translate>

Animation Slide up
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fromYDelta="100%"
    android:toYDelta="0%" >
</translate>

Animation Slide Down
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fromYDelta="-100%"
    android:toYDelta="0%" >
</translate>

Code used in class file for animation

case SimpleGestureFilter.SWIPE_RIGHT:
    ScreenAnimation.setVisibility(View.VISIBLE);
    ScreenAnimation.startAnimation(RightSwipe);

    break;
case SimpleGestureFilter.SWIPE_LEFT:
    ScreenAnimation.setVisibility(View.VISIBLE);
    break;
case SimpleGestureFilter.SWIPE_DOWN:
    ScreenAnimation.setVisibility(View.VISIBLE);
    ScreenAnimation.startAnimation(DownSwipe);
    break;
case SimpleGestureFilter.SWIPE_UP:
    ScreenAnimation.setVisibility(View.VISIBLE);
    ScreenAnimation.startAnimation(UpSwipe);
    break;

There's an open source component called TwoWayGridView. It provides both horizontal and vertical scrolling. Maybe you can customize it to fit your needs.

The other way is to use GestureDetector and do the track yourself. There's also a sample project on Android developer site, go to Training→Best Practices for User Input→Using Touch Gestures (I can't post more than two links). It looks complicated at first, but then you can customize each gesture so you can differentiate between swipe up and down, for instance.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!