【问题标题】:TabLayout+ViewPager inside ScrollViewScrollView 内的 TabLayout+ViewPager
【发布时间】:2016-09-11 17:25:42
【问题描述】:

我试图让这个页面向下滚动,但现在只有ViewPager 中的内容在滚动,而不是整个页面。我想让整个页面从顶部向下滚动,但现在它只滚动ViewPager 内的ListViews。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/asa"
android:scrollbars="vertical"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:custom="http://schemas.android.com/apk/res-auto"
xmlns:mstb="http://schemas.android.com/apk/res-auto"

tools:context="geob.com.testjson.Profile">
<ImageView
    android:id="@+id/cover12"
    android:src="@drawable/asc"
    android:layout_width="fill_parent"
    android:layout_height="180dp"
    android:scaleType="fitXY" />

<ImageView
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:id="@+id/ddd"
    android:src="@android:drawable/star_big_on"
    android:layout_marginBottom="62dp"

    android:layout_above="@+id/tab_layout"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Taaran"
    android:textColor="@android:color/white"
    android:id="@+id/dpname"
    android:layout_above="@+id/dpuname"
    android:layout_alignLeft="@+id/ddd" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="\@taaran"
    android:textColor="@android:color/white"
    android:id="@+id/dpuname"
    android:layout_above="@+id/tab_layout"
    android:layout_alignLeft="@+id/ddd"
    android:layout_alignStart="@+id/ddd"
    android:layout_marginBottom="32dp" />


<!-- <android.support.design.widget.TabLayout
    android:id="@+id/tabLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:layout_below="@+id/cover"
    android:layout_alignLeft="@+id/cover"
    android:layout_alignStart="@+id/cover" /> -->

<!-- View pager to swipe views -->
<!-- <android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"/>  -->
<!--<org.honorato.multistatetogglebutton.MultiStateToggleButton
    android:id="@+id/mstb_multi_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    mstb:values="@array/planets_array"
    mstb:mstbPrimaryColor="@android:color/holo_blue_dark"
    mstb:mstbSecondaryColor="@color/gray_very_light"
    android:layout_below="@+id/cover"

     />-->
<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:layout_below="@+id/cover12"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/tab_layout"/>

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/l12"
    android:layout_below="@+id/tab_layout"
     />
<com.ashokvarma.bottomnavigation.BottomNavigationBar
    android:layout_gravity="bottom"
    android:id="@+id/bottom_navigation_bar3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Follow"
    android:id="@+id/followButton"
    android:layout_above="@+id/tab_layout"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

</RelativeLayout>
</ScrollView>

【问题讨论】:

  • 使用NestedScrollView 而不是ScrollView

标签: android listview tabs android-viewpager scrollview


【解决方案1】:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int height = 0;
    for (int i = 0; i < getChildCount(); ++i) {
        View child = getChildAt(i);
        child.measure(widthMeasureSpec,
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        int h = child.getMeasuredHeight();
        if (h > height) {
            height = h;
        }
    }

    heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

我发现这段代码解决了我的问题,当在 ScrollView 中使用 SlidingTabLayout + ViewPager 时显示不正确,您只需要覆盖 ViewPager 类的默认 onMeasure(int, int) 方法,然后使用重写的 MyMeasuredViewPager如下:

                <MyMeasuredViewPager 
                    android:id="@+id/pager"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>

@link http://hellsam.com/2015/01/29/Android%E7%9A%84%E5%9D%91%E4%B9%8BScrollView%E5%B5%8C%E5%A5%97ViewPager/

【讨论】:

    【解决方案2】:

    使用可以处理滚动子项的NestedScrollView,而不是不适合处理滚动子项的 ScrollView。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-08
      相关资源
      最近更新 更多