NestedScrollView is not scrolling due to Editext

2019-05-30 03:40发布

问题:

I am facing issue when i have following Structure in NestedScrollView .

Here is my xml file :

<android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/app_base_color"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:id="@+id/nested_view_editprofile"
        android:focusable="true"

        >


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


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/app_base_color"
                android:weightSum="3">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1">

                    <TextView
                        android:id="@+id/txtvEditProfileUserName"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.7"
                        android:gravity="center"
                        android:paddingBottom="10dp"
                        android:paddingLeft="3dp"
                        android:paddingTop="10dp"
                        android:textColor="@color/orange" />

                </LinearLayout>

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

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:paddingBottom="5dp"
                        android:paddingTop="10dp"
                        android:weightSum="2">

                        <TextView
                            android:id="@+id/txvEditProfileEntertainer"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="1.3"
                            android:gravity="right"
                            android:text="0"
                            android:textColor="@color/orange" />

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.7"
                            android:paddingLeft="3dp"
                            android:singleLine="true"
                            android:text="Entertainers"
                            android:textColor="@color/cement_grey" />

                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:paddingBottom="10dp"
                        android:paddingTop="5dp"
                        android:weightSum="2">

                        <TextView
                            android:id="@+id/txvEditProfileSpectator"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="1.3"
                            android:gravity="right"
                            android:text="0"
                            android:textColor="@color/orange" />

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.7"
                            android:paddingLeft="3dp"
                            android:singleLine="true"
                            android:text="Spectators"
                            android:textColor="@color/cement_grey" />

                    </LinearLayout>

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1.15"
                    android:gravity="center"
                    android:orientation="vertical">

                    <ImageButton
                        android:id="@+id/imgbtnEditProfileSpactate"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@android:color/transparent"
                        android:paddingBottom="5dp"
                        android:paddingTop="10dp"
                        android:src="@drawable/plus" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="0.7"
                        android:gravity="center"
                        android:paddingBottom="10dp"
                        android:paddingLeft="3dp"
                        android:text="SPACTATE"
                        android:textColor="@color/orange" />

                </LinearLayout>

            </LinearLayout>

            <TextView
                android:id="@+id/txtvcom"                    android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/app_base_color"
                android:padding="10dp"
                android:textColor="@color/cement_grey" />

            <TextView
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:text="Personal Information"
                android:textColor="@color/orange"
                android:background="@drawable/app_base_background"/>




           <com.customview.CustomEditext
                android:id="@+id/edtxEdtPrflName"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:hint="Name"
                android:textColor="@color/cement_grey"
                android:inputType="textImeMultiLine"
                android:drawableLeft="@drawable/name"
                android:drawablePadding="10dp"
                android:background="@drawable/profile_pallets_boxes_background"
                android:enabled="true"
               android:focusableInTouchMode="true"
               />

            <com.customview.CustomEditext
                android:id="@+id/edtxEdtPrflUserName"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:hint="Username"
                android:textColor="@color/cement_grey"
                android:inputType="textImeMultiLine"
                android:drawableLeft="@drawable/username"
                android:drawablePadding="10dp"
                android:background="@drawable/profile_pallets_boxes_background"
                android:enabled="false"
                android:focusableInTouchMode="true"
                />

            <com.customview.CustomEditext
                android:id="@+id/edtxEdtPrflBio"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:hint="Bio"
                android:textColor="@color/cement_grey"
                android:inputType="textImeMultiLine"

                android:drawableLeft="@drawable/bio"
                android:drawablePadding="10dp"
                android:background="@drawable/profile_pallets_boxes_background"
                android:enabled="true"
                android:focusableInTouchMode="true"
                />

            <com.customview.CustomEditext
                android:id="@+id/edtxEdtPrflDOB"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:hint="Date of Birth"
                android:textColor="@color/cement_grey"

                android:inputType="date|textImeMultiLine"
                android:drawableLeft="@drawable/dob"
                android:drawablePadding="10dp"
                android:background="@drawable/profile_pallets_boxes_background"
                android:focusableInTouchMode="true"
                android:enabled="true"

                />

            <com.customview.CustomEditext
                android:id="@+id/edtxEdtPrflGender"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:hint="Gender"
                android:textColor="@color/cement_grey"
              android:inputType="textImeMultiLine"
                android:editable="false"
                android:drawableLeft="@drawable/gender"
                android:drawablePadding="10dp"
                android:background="@drawable/profile_pallets_boxes_background"
                android:enabled="false"
                android:focusableInTouchMode="true"
                />

            <TextView
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:text="Private Information"
                android:textColor="@color/orange"
                android:background="@drawable/app_base_background"/>

            <com.customview.CustomEditext
                android:id="@+id/edtxEdtPrflEmail"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:hint="Email"
                android:textColor="@color/cement_grey"

                android:inputType="textEmailAddress|textImeMultiLine"
                android:drawableLeft="@drawable/mail"
                android:drawablePadding="10dp"
                android:background="@drawable/profile_pallets_boxes_background"
                android:enabled="true"
                android:focusableInTouchMode="true"
                />

            <com.customview.CustomEditext
                android:id="@+id/edtxEdtPrflPhone"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:hint="Phone"
                android:textColor="@color/cement_grey"

                android:inputType="phone|textImeMultiLine"
                android:maxLength="10"
                android:drawableLeft="@drawable/phone"
                android:drawablePadding="10dp"
                android:background="@drawable/profile_pallets_boxes_background"
                android:enabled="false"
                android:focusableInTouchMode="true"
                />

        </LinearLayout>

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

Now due to Editext i am unable to scroll my NestedScrollView for that i use below solution and which work fine but it prevent Softkeyboard Opening for any Editext .

public class CustomEditext extends EditText {



    public CustomEditext(Context _Context){

        super(_Context);
    }


    public CustomEditext(Context context, AttributeSet attrs){

        super(context,attrs);
    }


    public CustomEditext(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
         super.onTouchEvent(event);

        return false;
    }


}

回答1:

I had a similar issue. The Design Support Library is a great lib, but it's a bit buggy at the moment. You can find some issues regarding NestedScrollView here: NestedScrollView Issues. We will have to wait for the next updates until we have a fully working lib.

Until then, you can try some alternative libs, such as: ObservableScrollView.

It is pretty simple to use:

1 - Add the dependency to your build.gradle file:

repositories {
    mavenCentral()
}

dependencies {
    // Other dependencies are omitted
    compile 'com.github.ksoichiro:android-observablescrollview:1.5.0'
}

2 - In your Layout, declare the ObservableScrollView like:

<com.github.ksoichiro.android.observablescrollview.ObservableListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

3 - In your Activity, implement ObservableScrollViewCallbacks:

public class MainActivity extends AppCompatActivity
  implements ObservableScrollViewCallbacks {

4 - Implement the required methods and play with them:

@Override
  public void onScrollChanged(int scrollY, boolean firstScroll,
    boolean dragging) {
  }

  @Override
  public void onDownMotionEvent() {
  }

  @Override
  public void onUpOrCancelMotionEvent(ScrollState scrollState) {
  }

5 - Example on showing/hiding the ActionBar on scrolling the list:

@Override
  public void onUpOrCancelMotionEvent(ScrollState scrollState) {
    ActionBar ab = getSupportActionBar();
    if (scrollState == ScrollState.UP) {
      if (ab.isShowing()) {
        ab.hide();
      }
    } else if (scrollState == ScrollState.DOWN) {
      if (!ab.isShowing()) {
        ab.show();
      }
    }
  }

Hope it helps.



回答2:

adding android:windowSoftInputMode="adjustResize" to the activity in the Manifest file solved my problem.

It was suggested by Kosh20 here:https://code.google.com/p/android/issues/detail?id=182362&q=nestedscrollview&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars