【发布时间】: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节点下,ViewPager在AppBarLayout的同一层级上。 -
好的。让我试试。刚刚发现一个有趣的情况 - 布局在模拟器中运行良好,但是一旦我在我的真实设备中测试它,它就会像上面的帖子一样发生。
标签: android android-layout android-fragments android-viewpager android-tablayout