NestedScrollView,约束布局为子级不滚动

问题描述 投票:0回答:1

我想在键盘上方滚动整个布局,但不起作用。我可以手动滚动,但希望它滚动当用户将焦点放在编辑文本时以编程方式进行。

请帮助我。在此先感谢

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
    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"
    android:id="@+id/scrollView"
    android:fillViewport="true"
    android:paddingBottom="?attr/actionBarSize">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
......

edit_text_code.setOnFocusChangeListener { view, hasFocus ->

            if (hasFocus) {

                scrollView.post {

                    scrollView.fullScroll(NestedScrollView.FOCUS_DOWN)
                }
            }
        }
android scrollview android-constraintlayout
1个回答
0
投票

方法1:

首先使用View.getLocationOnScreen (int[] location)获取Edittext的x / y坐标(方法返回后,数组按该顺序包含x和y位置)

然后做:

scrollView.scrollTo(x, y);

如果以上方法不起作用,请尝试以下方法:

方法2:

 edit_text_code.setOnTouchListener(new OnTouchListener() {
       public boolean onTouch(View view, MotionEvent event) {
            // put your edit text id below
            if (view.getId() ==R.id.DwEdit) {
                view.getParent().requestDisallowInterceptTouchEvent(true);
                switch (event.getAction()&MotionEvent.ACTION_MASK){
                case MotionEvent.ACTION_UP:
                    view.getParent().requestDisallowInterceptTouchEvent(false);
                    break;
                }
            }
            return false;
        }
    });

XML代码:

 <EditText
android:id="@+id/DwEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minLines="10"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical" 
android:overScrollMode="always"
android:inputType="textCapSentences">
</EditText> 
© www.soinside.com 2019 - 2024. All rights reserved.