ScrollView not working with Tabbed layout in Android

江枫思渺然 提交于 2019-12-08 11:39:37

问题


I am a newbie to Android and I am trying to use a ScrollView in the Tabbed layout. When I include the fragment layout to the tabbed view, the last element is not showing when we scroll down. I tried setting the height of the "LinearLayout" to "wrap_content" but still it is not working.

Ideally I needed only one button, other button are added just for testing purposes.

The same layout is working fine if I add it to an Activity and not a Tab view.

Here is the layout which contains a ScrollView

<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<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"
    android:scrollbars="vertical"
    tools:context="com.adroitminds.tabbedview.Section2">


    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="16dp"
        android:layout_weight="1"
        android:hint="First Name" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="16dp"
        android:layout_weight="1"
        android:hint="Last Name" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="16dp"
        android:layout_weight="1"
        android:hint="First Name1" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="16dp"
        android:layout_weight="1"
        android:hint="Last Name2" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="16dp"
        android:layout_weight="1"
        android:hint="Phone2" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="16dp"
        android:layout_weight="1"
        android:hint="Email2" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="16dp"
        android:layout_weight="1"
        android:hint="Phone" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="16dp"
        android:layout_weight="1"
        android:hint="Email" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="16dp"
        android:layout_weight="1"
        android:hint="Password" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="16dp"
        android:layout_weight="1"
        android:hint="Confirm Password" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_weight="1"
        android:layout_marginBottom="16dp"
        android:layout_marginTop="16dp"
        android:text="Submit" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_weight="1"
        android:layout_marginBottom="16dp"
        android:layout_marginTop="16dp"
        android:text="Submit1" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_weight="1"
        android:layout_marginBottom="16dp"
        android:layout_marginTop="16dp"
        android:text="Submit2" />

</LinearLayout>

Tabbed Activity which has the issue

<android.support.design.widget.CoordinatorLayout 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:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay">

    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

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

<android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

Here are the screenshots for both scenarois

Fragment in an Activity - working fine

Fragment in an Tabbed layout - not working

I can resolve this issue by reducing the margins or size of the EditTexts, but I am curious to know where I am going wrong.Could anyone please help me resolving this issue.

I am using latest version of Android Studio
minSdkVersion - 18
targetSdkVersion - 23

Thanks,
Kapil


回答1:


Add this line to your ScrollView :

app:layout_behavior="@string/appbar_scrolling_view_behavior"

Also remove the same attribute from android.support.v4.view.ViewPager The ViewPager should be

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


来源:https://stackoverflow.com/questions/33354845/scrollview-not-working-with-tabbed-layout-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!