简体   繁体   中英

ScrollView is not scrolling in fragment or not working properly

I'm pretty new in android and I'm developing an app but I have an error that's driving me crazy. The thing is that it doesn't scroll completely, it leaves a button out of the screen so it cant be seen. Scrollview works fine in every activity but doesn't work in tabbed activity. this code android:layout_height="match_parent" is giving problem if i change the height match-parent to 500dp or to any integer value it works fine. I want to know that why match-parent is not working.

i have tried every suggested edit to this question but still no edit works.

here is my XML file

<RelativeLayout 
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:background="@mipmap/back"
android:paddingBottom="10dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="5dp"
tools:context="com.example.sahil.bloodbank.first"
>

<ScrollView
    android:id="@+id/sc1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:scrollbars="vertical"
>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#000000"
    android:textSize="20sp"
    android:text="Welcome,"/>

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/wall1"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:id="@+id/desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView"
        android:gravity="center"
        android:text="@string/data"
        android:textAlignment="gravity"
        android:textColor="@color/data"
        android:textSize="17sp" />

    <TextView
        android:id="@+id/data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/note"
        android:textColor="@color/data"
        android:textStyle="bold"
        android:layout_marginTop="40dp"
        android:layout_below="@+id/desc"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:id="@+id/donate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:text="I want to Donate Blood"
        android:textColor="@color/text"
        android:textSize="18sp"
        android:layout_below="@+id/data"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="14dp"
        />

</LinearLayout>
</ScrollView>
</RelativeLayout>

Scroll view must be the root element.

<ScrollView 
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"
tools:context="com.example.sahil.bloodbank.first"
>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@mipmap/back"
android:paddingBottom="10dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="5dp"
>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#000000"
    android:textSize="20sp"
    android:text="Welcome,"/>

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/wall1"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:id="@+id/desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView"
        android:gravity="center"
        android:text="@string/data"
        android:textAlignment="gravity"
        android:textColor="@color/data"
        android:textSize="17sp" />

    <TextView
        android:id="@+id/data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/note"
        android:textColor="@color/data"
        android:textStyle="bold"
        android:layout_marginTop="40dp"
        android:layout_below="@+id/desc"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:id="@+id/donate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:text="I want to Donate Blood"
        android:textColor="@color/text"
        android:textSize="18sp"
        android:layout_below="@+id/data"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="14dp"
        />

</LinearLayout>

</RelativeLayout>
</ScrollView>

your scrollview must be the MATCHPARENT height

 <ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

Make your Scrollview as root view and height should be MATCH_PARENT

<ScrollView 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">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="20sp"
        android:text="Welcome,"/>

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@mipmap/wall1"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <TextView
            android:id="@+id/desc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/imageView"
            android:gravity="center"
            android:text="@string/data"
            android:textAlignment="gravity"
            android:textColor="@color/data"
            android:textSize="17sp" />

        <TextView
            android:id="@+id/data"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/note"
            android:textColor="@color/data"
            android:textStyle="bold"
            android:layout_marginTop="40dp"
            android:layout_below="@+id/desc"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <Button
            android:id="@+id/donate"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:text="I want to Donate Blood"
            android:textColor="@color/text"
            android:textSize="18sp"
            android:layout_below="@+id/data"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="14dp"
            />
        </LinearLayout>
    </ScrollView>

Try this code and change this code accroding your need and make sure your view is large to screen size after that scrolling effect is occur.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:clickable="true">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="@dimen/layout_padding">

    <ImageView
        android:id="@+id/epfIvUserImage"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:src="@drawable/big_user" />

    <EditText
        android:id="@+id/epfEtFirstName"
        style="@style/EditText"
        android:drawableLeft="@drawable/user"
        android:hint="@string/user_name"
        android:singleLine="true"
        android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
        android:nextFocusForward="@id/epfEtEmail"
        android:imeOptions="actionNext"
        />

    <EditText
        android:id="@+id/epfEtEmail"
        style="@style/EditText"
        android:drawableLeft="@drawable/email"
        android:hint="@string/email"
        android:inputType="textEmailAddress"
        android:singleLine="true"
        android:nextFocusForward="@id/epfEtCountry"
        android:imeOptions="actionNext"
        />

    <EditText
        android:id="@+id/epfEtCountry"
        style="@style/EditText"
        android:drawableLeft="@drawable/country"
        android:hint="@string/country"
        android:singleLine="true"
        android:nextFocusForward="@id/epfEtState"
        android:imeOptions="actionNext"
        />

    <EditText
        android:id="@+id/epfEtState"
        style="@style/EditText"
        android:drawableLeft="@drawable/state"
        android:hint="@string/state"
        android:singleLine="true"
        android:nextFocusForward="@id/epfEtCity"
        android:imeOptions="actionNext"/>

    <EditText
        android:id="@+id/epfEtCity"
        style="@style/EditText"
        android:drawableLeft="@drawable/city"
        android:hint="@string/city"
        android:singleLine="true"
        android:nextFocusForward="@id/epfEtPincode"
        android:imeOptions="actionNext"/>
    <EditText
        android:id="@+id/epfEtPincode"
        style="@style/EditText"
        android:drawableLeft="@drawable/street"
        android:hint="@string/zip_code"
        android:singleLine="true"
        android:inputType="text"
        android:nextFocusForward="@id/epfEtDescription"
        android:imeOptions="actionNext"
        />

    <EditText
        android:id="@+id/epfEtDescription"
        style="@style/EditText"
        android:drawableLeft="@drawable/descriptions"
        android:hint="@string/description"
        android:maxLines="3"
        android:overScrollMode="always"
        android:scrollbarStyle="insideInset"
        android:scrollbars="vertical"
        android:paddingBottom="@dimen/_30sdp"
        />

    <TextView
        android:id="@+id/epfTvUpdate"
        android:text="@string/update"
        style="@style/TextViewButton"
        />


</LinearLayout>
</ScrollView>

You not need to take Scroll View as Root Layout not required. But you may need to add more properties to Scroll View like android:scrollbars="vertical" and android:fillViewport="true" . Also you can use approach like you take Button outside of Scroll View and only scroll other content so your Button always will be visible on screen only your content will scroll.

Like

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@mipmap/ic_launcher"
    android:paddingBottom="10dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="5dp">

    <ScrollView
        android:layout_above="@+id/donate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true"
        android:scrollbars="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Welcome,"
                android:textColor="#000000"
                android:textSize="20sp" />

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                app:srcCompat="@android:drawable/ic_menu_share" />

            <TextView
                android:id="@+id/desc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/imageView"
                android:gravity="center"
                android:text="Description DescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescription"
                android:textAlignment="gravity"
                android:textColor="@color/colorPrimary"
                android:textSize="17sp" />

            <TextView
                android:id="@+id/data"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/desc"
                android:layout_marginTop="40dp"
                android:text=" Note DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote DataNote Data"
                android:textColor="@color/colorPrimary"
                android:textStyle="bold" />



        </LinearLayout>
    </ScrollView>

    <Button
        android:id="@+id/donate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="10dp"
        android:background="@color/colorPrimary"
        android:text="I want to Donate Blood"
        android:textColor="@color/colorPrimary"
        android:textSize="18sp" />
</RelativeLayout>

Inside Fragment you can try this property android:isScrollContainer="false" inside Scroll View .

If that not work try with NestedScrollView in Fragment .

<android.support.v4.widget.NestedScrollView
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:fillViewport="true"
     app:layout_behavior="@string/appbar_scrolling_view_behavior" >


</android.support.v4.widget.NestedScrollView>

The standard ScrollView need to be used as parent. I think you are using ToolBar in your activity. So in that case you can use NestedScrollView with the attribute app:layout_behavior="@string/appbar_scrolling_view_behavior"

So replace your ScrollView with:---

<NestedScrollView
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:fillViewport="true"
  app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

  {Your Child View}

</android.support.v4.widget.NestedScrollView>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM