【问题标题】:Tab Layout cover part of fragment标签布局覆盖片段的一部分
【发布时间】:2018-01-08 01:22:17
【问题描述】:

我在Tab Layout covering parts of fragment, layout_below not working 的帖子中遇到了类似的问题。不同之处在于我使用的是NonSwipeableViewPager

更新

该布局在模拟器中运行良好,但一旦我在我的真实设备中测试它,它就会像上面的帖子一样发生。

我的 xml 文件

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

有什么建议吗?谢谢。

NonSwipeableViewPager 类

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

【问题讨论】:

  • 不同之处在于,您提到的问题是使用RelativeLayout 作为容器,而您使用的是LinearLayout
  • 奥普斯。您是正确的,但选项卡布局仍然覆盖片段。有什么建议吗?
  • 您是否尝试过使用ViewPager 而不是NonSwipeableViewPager。如果是这样,它有效吗?也许这是NonSwipeableViewPager 上的一个错误。当我使用TabLayout 时,我将它放在AppBarLayout 节点下,ViewPagerAppBarLayout 的同一层级上。
  • 好的。让我试试。刚刚发现一个有趣的情况 - 布局在模拟器中运行良好,但是一旦我在我的真实设备中测试它,它就会像上面的帖子一样发生。

标签: android android-layout android-fragments android-viewpager android-tablayout


【解决方案1】:

在您的 gridView 或 listView 上使用 setFocusable(false)

推荐看看:setFocusable 主要用于启用/禁用视图在触摸模式和键盘模式下的焦点事件(使用上/下/下一个键)。 What is the difference between setFocusable and setFocusableInTouchMode?

【讨论】:

    猜你喜欢
    • 2016-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-13
    • 2020-02-07
    • 2012-07-19
    • 1970-01-01
    相关资源
    最近更新 更多