Alex Ferg
Alex Ferg

Reputation: 881

LinearLayout inside ViewPager disables swiping

I have a RecyclerView inside a Fragment for ViewPager. Each item inside the RecyclerView is a LinearLayout. My problem is that I can't swipe pages when I touch the RecyclerView. I've tried it with RelativeLayout and it worked, so the problem is with the LinearLayout.

My fragment layout:

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

The row layout:

<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>

If I change the row's parent to RelativeLayout, I can swipe and it works just fine, but it doesn't if it's a LinearLayout.

I don't need to click on the RecyclerView at all, is there a way so that any swiping or any touch event done on the RecyclerView can be disabled and instead sent to the fragment?

Upvotes: 1

Views: 531

Answers (2)

Ernestas Venckus
Ernestas Venckus

Reputation: 3

Had the same issue and the cause of this was android:singleLine="true", removing this solves the problem but with the price of losing autoscrolling text. However, I found a workaround by adding onTouchListener to each view that uses singleLine:

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;
            }
        });

This will disable singleLine while pressing down to make swiping possible and re-enable it after releasing.

Upvotes: 0

Juvie22
Juvie22

Reputation: 371

try setting

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

on the RecyclerView or item LinearLayout

Upvotes: 1

Related Questions