android - 将 Android NestedScrollView 水平滑动传递给父 View

标签 android android-custom-view ontouchlistener touch-event android-nestedscrollview

我正在尝试制作一个左右滑动的卡片系统(如 Tinder),卡片上有一个 NestedScrollView。目标是如果用户仅上下滑动,NestedScrollView 将滚动,但如果用户向左或向右滑动,Card 将使用该滑动。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="4dp"
    card_view:cardUseCompatPadding="true">

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/nested_scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="4dp">

            <TextView
                android:id="@+id/text_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>

</android.support.v7.widget.CardView>

(我用的卡库是https://github.com/wenchaojiang/AndroidSwipeableCardStack)

compile 'com.github.wenchaojiang:AndroidSwipeableCardStack:0.1.5'

当我在 NestedScrollView 上放置一个 margin_top 并在该边距内触摸时,CardStack 会正确抓取我的输入并且卡片向左或向右移动,但如果我触摸并拖动到其他任何地方,NestedScrollView 会抓取我的输入而不考虑方向。

我应该扩展/覆盖哪个类/onTouchEvent 才能产生这种效果,或者可能有更简单的方法?

谢谢!

最佳答案

是的,您应该通过在 Java 代码上实现 CardStack.CardEventListener 并将其设置为监听器 mCardStack.setListener(yourListener) 来完成此操作;它在项目内部的自述文件中。试试这个:

Class YourListener extends CardStack.CardEventListener{
    //implement card event interface
    @Override
    public boolean swipeEnd(int direction, float distance) {
        //if "return true" the dismiss animation will be triggered 
        //if false, the card will move back to stack
        //distance is finger swipe distance in dp


        return (distance>300)? true : false;
    }

    @Override
    public boolean swipeStart(int direction, float distance) {

        return true;
    }

    @Override
    public boolean swipeContinue(int direction, float distanceX, float distanceY) {

        return true;
    }

    @Override
    public void discarded(int id, int direction) {
       //this callback invoked when dismiss animation is finished. 
    }

    @Override
    public void topCardTapped() {
         //this callback invoked when a top card is tapped by user. 
    }
}

关于android - 将 Android NestedScrollView 水平滑动传递给父 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41927974/

相关文章:

android - 模拟 SetOnTouchListener 或它在 xamarin 中的使用方式,参数必须是什么

android - 如何在android webview中模拟特定坐标处的点击?

android - 将APK上传到Nexus,maven插件是唯一的方法吗?

java - File() 构造函数中是否需要 .txt 扩展名?

android - 如何在 android 的自定义 TextView 控件中更改字母的颜色?

android - 如何获取引用属性的值

java - 为什么没有为我设置 OnTouchListener?

java - 访问 HashMap Java Android 中对象的属性

java - 安卓辅助搜索 : The search button does not invoke the searchable Activity (Other Solutions did not help)

android - chris Jenx for Android Q 的书法库崩溃