每当我滑动到新页面时,Viewpager就会向下滚动

时间:2018-06-29 01:21:40

标签: android android-viewpager

每次我在ViewPager中滑动到新屏幕时,如果整个视图长于设备屏幕,它就会向下滚动。

我希望视图寻呼机从顶部开始。我尝试使用其他东西...

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

在editText周围的布局中,但实际上我什至不知道焦点在哪里。

我还尝试了不同的布局:

android:descendantFocusability="blocksDescendants"

我什至不确定焦点在哪里。

这是我的片段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>

更新: 这是我的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>

2 个答案:

答案 0 :(得分:0)

从“主要活动” xml中的工具栏中删除此代码

app:layout_scrollFlags="scroll|enterAlways"

答案 1 :(得分:0)

将layout_Margin更改为padding,并且由于某种原因解决了问题。向任何可以解释原因的人提供支持。

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

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