【问题标题】:Enable scrolling inside a Fragment that uses TabLayout and ViewPager在使用 TabLayout 和 ViewPager 的 Fragment 内启用滚动
【发布时间】:2017-03-27 19:27:18
【问题描述】:

你好! 我希望这个视图可以滚动:

如您所见,TAB1 中的“TEMP CONTENT”只是用来模拟可能出现的临时内容。

我现在要做的是让整个页面可滚动。我设法做的是使 TAB1 的内容,因此,TabLayout 的内容,可滚动,但这不是我想要做的。如上所述,应该是整个页面。

那是持有视图的 Layout:

<?xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="at.codecrane.whosnext.MainActivity">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="200dp">

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:srcCompat="@android:color/darker_gray" />

            <ImageView
                android:id="@+id/imageView3"
                android:layout_width="100dp"
                android:layout_height="100dp"
                app:srcCompat="@drawable/ic_face"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="32dp" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/imageView3"
                android:layout_centerHorizontal="true"
                android:text="Temp Name"
                android:textSize="18sp" />
        </RelativeLayout>



        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.design.widget.TabLayout
                android:id="@+id/tab_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabIndicatorColor="@color/colorPrimary">

                <android.support.design.widget.TabItem
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="General" />

                <android.support.design.widget.TabItem
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="Statistics" />

            </android.support.design.widget.TabLayout>

        </android.support.v4.view.ViewPager>
    </LinearLayout>

这是 ViewPager-Content 的片段布局:

<!-- TODO: Update blank fragment layout -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TEMP CONTET"
        android:textSize="20dp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TEMP CONTET"
        android:textSize="20dp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TEMP CONTET"
        android:textSize="20dp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TEMP CONTET"
        android:textSize="20dp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TEMP CONTET"
        android:textSize="20dp" />
</LinearLayout>

提前致谢!

【问题讨论】:

  • 在你的第一个 xml 中使用滚动视图而不是线性布局作为根
  • 我尝试改用滚动视图,但视图页面中根本没有显示任何内容。

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


【解决方案1】:

同时使用ScrollviewNestedScrollview

这是一个例子Code

  1. 在您的 View pager 片段中使用 NestedScrollview

  2. 为您的活动布局添加Scrollview作为根标签并添加android:fillViewport="true"

【讨论】:

  • 你的部分答案是正确的。当我使用 Scrollview 作为根标签并添加 android:fillViewPort="true" 时,它可以正常工作。但是,我必须将视图寻呼机的高度设置为 2000 dp,让滚动视图完成他的工作。我认为现在的问题是设置视图寻呼机的高度。但是,我不知道 ViewPager 的高度,我只是将视图寻呼机替换为片段的内容。你有什么想法吗?
  • 我不希望 ViewPager 被嵌套。我只想滚动整个页面,因为这将是一个普通页面。
  • 你对动态高度有什么想法吗?
猜你喜欢
  • 2017-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多