【问题标题】:Pass touch event from recycler to its parent LinearLayout将触摸事件从回收器传递到其父 LinearLayout
【发布时间】:2017-12-27 16:30:45
【问题描述】:

我有这样的事情:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="cntxt">


<LinearLayout
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <!-- Title -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:gravity="center"
        android:padding="20dp"
        android:text="@string/title"
        android:textColor="@android:color/white"
        android:textSize="17sp" />

        <!-- Recycler -->
        <CustomRecyclerView
            android:id="@+id/main_activity_rv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:scrollingCache="false" />
</LinearLayout>

当布局滚动到顶部时,当用户用手指进行类似“滑动刷新”的移动时,我需要在 Y 轴上制作平移动画。换句话说,如果回收器在顶部,我需要跟随整个回收器的手指移动。

我尝试将 onTouch 侦听器附加到回收器,但回收器的内部滚动使情况不清楚。 有什么想法来实现这个动画?

如果我将 onTouchListener 附加到 main_layout 并从 iner textview 进行投掷,则动画会正常播放。但是当我触摸回收器时,什么也没有发生(回收器的内卷轴除外) 提前谢谢你。

【问题讨论】:

    标签: java android android-layout animation android-recyclerview


    【解决方案1】:

    将最外层设为a

    SwipeRefreshLayout 
    

    而不是相对布局。这将为您提供任何方式的刷新功能。

    另请注意,在提到的滑动布局下,只能有一个直接子视图,所以无论您放置什么...确保将其放在您拥有的线性布局中。

    您可以附加一个 OnRefreshListener 来监听刷新布局的拉动。

    【讨论】:

    • 您将获得默认的圆形动画,但无法更改。也许如果你想要另一个动画,你可以按照布局的代码,用你自己的动画集创建你自己的视图
    • 问题是回收器的滚动监听器。有什么办法可以绕过它,比如“在顶部”?
    • 不确定滚动侦听器,但我建议的方法不再需要您为回收站视图设置滚动侦听器。刷新布局负责您的回收站视图上下滚动并且仅在完全位于顶部时刷新
    猜你喜欢
    • 1970-01-01
    • 2017-04-28
    • 2015-06-07
    • 2011-09-16
    • 2013-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多