单击RecyclerView的空白区域 [英] click through RecyclerView empty space

查看:581
本文介绍了单击RecyclerView的空白区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个布局,其中RecyclerView位于带有几个按钮的另一个布局之上.第一个回收站项目是顶部较大的页眉,可在其上方创建空白空间.现在,我希望在整个开放空间中进行点击操作,同时滑动还可以滚动浏览回收站.这些视图位于一个简单的框架中.

I have a layout where a RecyclerView is on top of another layout with a few buttons. The first recycler item is a header with a large top margin to create an empty space above it. Now I want clicks to work through that open space, swiping should also scroll the recycler. The views are in a simple frame.

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

    <package.FeedBackgroundView
        android:id="@+id/feed_background"
        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="match_parent"
        android:orientation="vertical"
        android:scrollbars="vertical"/>

</FrameLayout>

回收商仅使用顶部填充或边距消耗所有点击.我需要点击才能通过,但是滑动应该留在回收站中以便滚动.

The recycler consumes all clicks with just top padding or margin. I need clicks to pass through, but swipes should stay in the recycler for scrolling.

我的点击有效,解决方法是:

I have the clicks working, the solution was:

recycler.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            background.dispatchTouchEvent(event);
            return false;
        }
    });

现在,我遇到了问题,因为我翻译了背景(视差),因此点击没有到达正确的位置.我也必须翻译事件吗?

Now I have a problem, since I translate the background (parallax) the clicks aren't arriving on the correct positions. Do I have to translate the events too?

推荐答案

好的,我解决了两个问题.

Okay, I solved both issues.

recycler.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            MotionEvent e = MotionEvent.obtain(event);
            Matrix m = new Matrix();
            m.setTranslate(0f, -backgroundTranslation);
            e.transform(m);
            background.dispatchTouchEvent(e);
            e.recycle();
            return false;
        }
    });

这将复制回收站触摸事件并修复"其位置.翻译是背景的translationY.

This duplicates the recycler touch event and "fixes" it's position. The translation is translationY for the background.

这篇关于单击RecyclerView的空白区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆