0

I got a similar question with this post Tab Layout covering parts of fragment, layout_below not working. The difference is I am using NonSwipeableViewPager.

UPDATED

The layout works fine in emulator but once I test it in my real device, it happens like the above post.

My xml file

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

    <android.support.design.widget.TabLayout
        android:id="@+id/sliding_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed" />

    <com.pakhocheung.outbuy_android.NonSwipeableViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/sliding_tabs"
        android:background="@android:color/white"/>

</RelativeLayout>

Any suggestions? Thanks.

NonSwipeableViewPager class

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.DecelerateInterpolator;
import android.widget.Scroller;

import java.lang.reflect.Field;

public class NonSwipeableViewPager extends ViewPager {

    public NonSwipeableViewPager(Context context) {
        super(context);
        setMyScroller();
    }

    public NonSwipeableViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        setMyScroller();
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        // Never allow swiping to switch between pages
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // Never allow swiping to switch between pages
        return false;
    }

    //down one is added for smooth scrolling

    private void setMyScroller() {
        try {
            Class<?> viewpager = ViewPager.class;
            Field scroller = viewpager.getDeclaredField("mScroller");
            scroller.setAccessible(true);
            scroller.set(this, new MyScroller(getContext()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public class MyScroller extends Scroller {
        public MyScroller(Context context) {
            super(context, new DecelerateInterpolator());
        }

        @Override
        public void startScroll(int startX, int startY, int dx, int dy, int duration) {
            super.startScroll(startX, startY, dx, dy, 350 /*1 secs*/);
        }
    }
}
Pak Ho Cheung
  • 1,382
  • 6
  • 22
  • 52
  • The difference is, the question you referenced to is using `RelativeLayout` as the container while you are using `LinearLayout `. – Siu Jan 08 '18 at 01:34
  • Opps. You are correct but the tab layout still cover the fragment. Any suggestions? – Pak Ho Cheung Jan 08 '18 at 01:37
  • Have you tried to use `ViewPager` instead of `NonSwipeableViewPager`. If so, did it work? Maybe it is a bug on `NonSwipeableViewPager`. When I used `TabLayout` I put it under a `AppBarLayout` node and `ViewPager` on the same hierarchical level of `AppBarLayout`. – Ruben O. Chiavone Jan 08 '18 at 02:10
  • OK. Let me try it. Just found a interesting situation - The layout works fine in emulator but once I test it in my real device, it happens like the above post. – Pak Ho Cheung Jan 08 '18 at 02:16

1 Answers1

0

Use setFocusable(false) on your gridView or listView

Recommend to take a look: setFocusable mainly used for enable/disable view's focus event on both touch mode and keypad mode( using up/down/next key). What is the difference between setFocusable and setFocusableInTouchMode?

Pak Ho Cheung
  • 1,382
  • 6
  • 22
  • 52