Scroll doesn't work in NestedScrollView when try to scroll from views with click events

前端 未结 4 987
时光说笑
时光说笑 2020-12-24 07:19

I\'m using a NestedScrollView in a layout, and am attempting to use the new CoordinatorLayout from the design support library for CollapsingToolbarLayout.

My layout

相关标签:
4条回答
  • 2020-12-24 07:29

    I had the same problem. It happens only when NestedScrollView content height is less than height of device screen. So the workaround is to use setMinimumHeight(..) method for the view inside your NestedScrollView to make it resize to screen height:

    DisplayMetrics displaymetrics = new DisplayMetrics();
    getBaseActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int screenHeight = displaymetrics.heightPixels;
    
    int actionBarHeight = 0;
    TypedValue tv = new TypedValue();
    if (getBaseActivity().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
        actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
    }
    
    view.setMinimumHeight(screenHeight - actionBarHeight);
    

    where view is your RelativeLayout

    It works fine fore me. Hope it helps you

    0 讨论(0)
  • 2020-12-24 07:29

    In your AndroidManifest.xml, make sure you have 'windowSoftInputMode' attribute set to 'adjustResize'.

    <activity android:name=".activities.YourActivity"  android:windowSoftInputMode="adjustResize">
    
    0 讨论(0)
  • 2020-12-24 07:45

    One of your ScrollViews will send all events to the first View that answer true on dispatchMotionEvent.

    You may avoid using such scenario in your app OR override all dispatchMotionEvent methods (from scrolls and Views) to not consume the ACTION_DOWN.

    0 讨论(0)
  • 2020-12-24 07:45

    The solution here (a workaround for this google issue by overriding the nestedScrollview) https://gist.github.com/chrisbanes/8391b5adb9ee42180893300850ed02f2 worked like a charm!

    Define FixAppBarLayoutBehavior.java

    /*
     * Copyright (C) 2017 The Android Open Source Project
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *      http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    
    package your.package;
    
    import android.content.Context;
    import android.support.design.widget.AppBarLayout;
    import android.support.design.widget.CoordinatorLayout;
    import android.support.v4.view.ViewCompat;
    import android.util.AttributeSet;
    import android.view.View;
    
    /**
     * Workaround AppBarLayout.Behavior for https://issuetracker.google.com/66996774
     *
     * See https://gist.github.com/chrisbanes/8391b5adb9ee42180893300850ed02f2 for
     * example usage.
     *
     * Change the package name as you wish.
     */
    public class FixAppBarLayoutBehavior extends AppBarLayout.Behavior {
    
        public FixAppBarLayoutBehavior() {
            super();
        }
    
        public FixAppBarLayoutBehavior(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        public void onNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target,
                int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type) {
            super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed,
                    dxUnconsumed, dyUnconsumed, type);
            stopNestedScrollIfNeeded(dyUnconsumed, child, target, type);
        }
    
        @Override
        public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child,
                View target, int dx, int dy, int[] consumed, int type) {
            super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);
            stopNestedScrollIfNeeded(dy, child, target, type);
        }
    
        private void stopNestedScrollIfNeeded(int dy, AppBarLayout child, View target, int type) {
            if (type == ViewCompat.TYPE_NON_TOUCH) {
                final int currOffset = getTopAndBottomOffset();
                if ((dy < 0 && currOffset == 0)
                        || (dy > 0 && currOffset == -child.getTotalScrollRange())) {
                    ViewCompat.stopNestedScroll(target, ViewCompat.TYPE_NON_TOUCH);
                }
            }
        }
    }
    

    Usage in java:

    AppBarLayout abl = findViewById(R.id.app_bar);
    ((CoordinatorLayout.LayoutParams) abl.getLayoutParams()).setBehavior(new FixAppBarLayoutBehavior());
    

    Usage in xml:

    <android.support.design.widget.CoordinatorLayout 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.support.design.widget.AppBarLayout
                android:id="@+id/app_bar"
                android:layout_height="..."
                android:layout_width="..."
                app:layout_behavior="your.package.FixAppBarLayoutBehavior">
    
        </android.support.design.widget.AppBarLayout>
    
        <!-- Content -->
    
    </android.support.design.widget.CoordinatorLayout>
    

    This is provided in the post Click not working on RecyclerView in CoordinatorLayout when scrolling.

    0 讨论(0)
提交回复
热议问题