Search code examples
androidandroid-viewpager

Viewpager scrolls down whenever I swipe to new page


Every time I swipe to a new screen in ViewPager, if the whole view is longer than my device screen, it scrolls down a hair.

I want the view pager to start at the top. I have tried using different things... like

    android:focusable="true"
    android:focusableInTouchMode="true"

in the layouts surrounding the editText, but actually I'm not even sure what is taking the focus.

I also tried, the different layouts:

android:descendantFocusability="blocksDescendants"

I'm not even sure what is taking the focus.

Here is my fragment xml:

<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="jeffpadgett.myapplication.Day"
android:focusable="true"
android:focusableInTouchMode="true"
android:id="@+id/scrollViewDay">

    <android.support.constraint.ConstraintLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_margin="20sp">

        <TextView
            android:id="@+id/tvDayNumber"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_centerHorizontal="true"
            android:gravity="center_horizontal"
            android:text="Day Number"
            android:textColor="@color/colorPrimary"
            android:textSize="32dp"
            android:textStyle="italic"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.13999999" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_below="@+id/tvDayNumber"
            android:text="Description of this action of love."
            android:textSize="24dp"
            android:textColor="@color/colorPrimaryDark"

            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tvDayNumber"
            app:layout_constraintVertical_bias="0.25" />

        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@id/textView1"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            android:orientation="vertical"
            android:layout_marginBottom="8dp"
            tools:layout_editor_absoluteX="8dp"
            android:focusableInTouchMode="true"
            android:focusable="true">  

        <android.support.design.widget.TextInputLayout
            android:id="@+id/textInputLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView1">

            <EditText
                android:id="@+id/edtComments"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="fill_horizontal"
                android:hint="Thoughts and Comments"
                android:inputType="textMultiLine"/>
        </android.support.design.widget.TextInputLayout>

        </LinearLayout>

        <ImageView
            android:layout_gravity="center_horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/holdinghands"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/linearLayout"
            android:focusableInTouchMode="false"
            android:focusable="false"/>

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_gravity="end"
            android:layout_marginBottom="8dp"

            android:background="@drawable/button"
            android:paddingLeft="16sp"
            android:paddingRight="16sp"
            android:text="Complete"
            android:focusableInTouchMode="false"
            android:focusable="false"

            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/linearLayout" />

    </android.support.constraint.ConstraintLayout>

UPDATE: And here is my mainActivity xml:

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

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
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="jeffpadgett.myapplication.MainActivity">


    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"

        app:theme="@style/ToolBarStyle"
        >

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

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

</LinearLayout>

Solution

  • Changed layout_Margin to padding, and for some reason problem was solved. Props to anyone who can explain why.

    Old:

    <android.support.constraint.ConstraintLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:layout_margin="20sp">
    

    New:

    <android.support.constraint.ConstraintLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:padding="20dp">