简体   繁体   English

ScrollView不能完全滚动到子视图

[英]Scrollview not scrolling to the child-views completely

So I' added a bunch of EditTexts programatically to a ScrollView (the quantity depends on users' input) and I'm trying to check if the user left one empty, set an error and focus the particular edittext. 因此,我以编程方式向ScrollView中添加了一堆EditText(数量取决于用户的输入),我试图检查用户是否将其留空,设置错误并集中处理特定的Edittext。 The problem is that when I try to scroll to that specific edittext, it doesn´t scroll enough, so some of those won't event reach the visible part of the scrollview. 问题是,当我尝试滚动到该特定的edittext时,滚动效果不够,因此其中一些不会事件到达scrollview的可见部分。

I want the EditText to be in the uppest visible part of the ScrollView so that the keyboard won't hide it. 我希望EditText位于ScrollView的最高可见部分,以便键盘不会将其隐藏。

Any ideas? 有任何想法吗?

Here is that part of the method: 这是方法的一部分:

public void StartNextActivity(View view) {
    String et = null;
    ArrayList<String> ids = new ArrayList<String>();
    for (int i = 0; i < x; i++) {                    //x is users' input
        et = editTexts.get(i).getText().toString();
        if (TextUtils.isEmpty(et)){
            editTexts.get(i).setError("Please insert an Id")
            ScrollView sView = (ScrollView) findViewById(R.id.scrollView3);
            sView.fullScroll(editTexts.get(i).getTop());
            return;
        }else {
            ids.add(et);
        }
    }
    Intent next_intent = new Intent(this, NextActivity.class);
    startActivity(next_intent);
}

And this is the xml: 这是xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:fillViewport="true"
tools:context=".FirstActivity"
tools:layout_editor_absoluteY="25dp">


<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:layout_marginEnd="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:text="Please insert something"
    android:textSize="18sp"
    app:layout_constraintBottom_toTopOf="@+id/scrollView3"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ScrollView
    android:id="@+id/scrollView3"
    android:layout_width="379dp"
    android:layout_height="0dp"
    android:layout_marginBottom="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:fillViewport="true"
    android:isScrollContainer="true"
    app:layout_constraintBottom_toTopOf="@+id/button4"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView">

    <LinearLayout
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" />
</ScrollView>

<Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:onClick="StartCarRentActivity"
    android:text="Continue"
    android:textSize="18sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/scrollView3" />

你可以试试这个

sView.smoothScrollTo((int)editTexts.getX(),(int)editTexts.getY());

try using a view below scrollview and set its visibility to invisible 尝试使用scrollview下面的view并将其可见性设置为invisible

<view
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:layout_alignBottom="@+id/relDisplayName"
    android:layout_alignParentStart="true"
    android:visibility="invisible"
    android:layout_alignParentLeft="true" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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