Home > database >  How to detect swipe up in horizontal scroll view - Android
How to detect swipe up in horizontal scroll view - Android

Time:05-24

I have a HorizontalScrollView which has 2 cards that scroll horizontally, I want to detect swipe-up gestures to perform certain actions but that is not happening.

I reviewed other solutions over here, but they just don't work with my problem.

Here is my XML:

 <HorizontalScrollView
    android:id="@ id/cardScroll"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@ id/cardBalanceValue"
    android:layout_marginStart="20dp"
    android:layout_marginTop="60dp"
    android:scrollbars="none">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:verticalScrollbarPosition="left">

        <com.google.android.material.card.MaterialCardView
            android:id="@ id/cardOne"
            android:layout_width="200dp"
            android:layout_height="300dp"
            android:elevation="50dp"
            android:transitionName="cardTransition"
            app:cardBackgroundColor="@android:color/holo_blue_dark"
            app:cardCornerRadius="8dp"
            app:cardElevation="20dp"
            app:rippleColor="#102A5C"
            app:strokeWidth="2dp" />

        <com.google.android.material.card.MaterialCardView
            android:id="@ id/cardTwo"
            android:layout_width="200dp"
            android:layout_height="300dp"
            android:layout_marginStart="30dp"
            android:layout_marginEnd="20dp"
            app:cardBackgroundColor="@android:color/holo_orange_light"
            app:cardCornerRadius="8dp"
            app:cardElevation="20dp"
            app:strokeWidth="2dp" />
    </LinearLayout>
</HorizontalScrollView>

CodePudding user response:

Try to use onTouchListner for example :

   view.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {

                }
                case MotionEvent.ACTION_UP: {
                    //do something with up swipe
                }
            }

            return false;
        }
    });
  • Related