ViewPager中的LinearLayout禁用滑动

问题描述 投票:1回答:2

我在ViewPager的Fragment中有一个RecyclerView。 RecyclerView中的每个项目都是LinearLayout。我的问题是,当我触摸RecyclerView时,我无法刷页。我已经尝试使用RelativeLayout并且它有效,所以问题在于LinearLayout。

我的片段布局:

<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    />

行布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"
    android:weightSum="1">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.05"
        android:textAlignment="center"
        android:id="@+id/game_all_text_number"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        />
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.225"
        android:textAlignment="center"
        android:id="@+id/game_all_text_player1"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        />
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.225"
        android:textAlignment="center"
        android:id="@+id/game_all_text_player2"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        />
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.225"
        android:textAlignment="center"
        android:id="@+id/game_all_text_player3"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        />
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.225"
        android:textAlignment="center"
        android:id="@+id/game_all_text_player4"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        />
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.05"
        android:textAlignment="center"
        android:id="@+id/game_all_text_diff"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        />
</LinearLayout>

如果我将行的父级更改为RelativeLayout,我可以滑动并且它可以正常工作,但如果它是LinearLayout则不会。

我根本不需要点击RecyclerView,是否有办法可以禁用在RecyclerView上完成的任何滑动或任何触摸事件,而是将其发送到片段?

android android-viewpager android-recyclerview android-linearlayout swipe
2个回答
1
投票

尝试设置

android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"

在RecyclerView或项目LinearLayout上


0
投票

有相同的问题和原因是android:singleLine="true",删除这解决了问题,但以失去自动滚动文本的代价。但是,我通过向每个使用singleLine的视图添加onTouchListener找到了一种解决方法:

view.setOnTouchListener(new View.OnTouchListener()
        {
            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN)
                {
                    view.setSingleLine(false);
                    view.setMaxLines(1);
                }
                else if (event.getAction() == android.view.MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL)
                {
                    view.setSingleLine(true);
                }
                return true;
            }
        });

这将禁用singleLine,同时按下以进行滑动并在释放后重新启用它。

© www.soinside.com 2019 - 2024. All rights reserved.