简体   繁体   English

启用子ScrollView时禁用父ScrollView

[英]Disable parent ScrollView when child ScrollView is enabled

I have two ScrollViews in my layout like so 我的布局中有两个ScrollViews就像这样

<ScrollView
    android:id="@+id/news_page_scroller"
    android:layout_width="match_parent" 
    android:layout_height="match_parent">

    <!-- Extra Content -->

    <android.support.v7.widget.CardView
        android:id="@+id/news_comment_card"
        android:layout_width="match_parent"
        android:layout_height="180dp"
        app:cardCornerRadius="8sp"
        app:cardElevation="@dimen/elevation_card">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/news_comment_header"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Comments"
                android:textColor="@color/colorAccent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/news_comment_section"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintBottom_toTopOf="@+id/news_comment_expand"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/news_comment_header"/>

            <Button
                android:id="@+id/news_comment_expand"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="@string/show_all_comments"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"/>

        </android.support.constraint.ConstraintLayout>  
</ScrollView>

I initially disable news_comment_section using the following code in my Activity commentSection.setOnTouchListener(new IgnoreTouch()); 我最初在我的Activity commentSection.setOnTouchListener(new IgnoreTouch());使用以下代码禁用news_comment_section commentSection.setOnTouchListener(new IgnoreTouch()); .

IgnoreTouch is a helper class that ignores touch events like so IgnoreTouch是一个帮助类,它忽略了触摸事件

 private class IgnoreTouch implements View.OnTouchListener{
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true;
    }
}

This works in disabling the scrolling for commentsList . 这适用于禁用commentsList的滚动。 When the Button news_comment_expand is clicked the CardView news_comment_card expands it's height to fill the screen and I call the following 当单击Button news_comment_expand ,CardView news_comment_card扩展它的高度以填满屏幕,我调用以下内容

newsPageScroller.setOnTouchListener(new IgnoreTouch());
newsPageScroller.requestDisallowInterceptTouchEvent(true);
commentSection.setOnTouchListener(null);

This disables news_page_scroller and enables news_comment_section . 这会禁用news_page_scroller并启用news_comment_section This works up until I reach the top or bottom of news_comment_section . 这一直有效,直到我到达news_comment_section的顶部或底部。 If I continue to scroll news_comment_section after I reach the top or bottom of the list, the parent ScrollView, news_page_scroller , starts scrolling. 如果我在到达列表的顶部或底部后继续滚动news_comment_section ,则父ScrollView news_page_scroller开始滚动。 How do I completely disable any scrolling from happening on the parent ScrollView news_page_scroller ? 如何完全禁用父ScrollView news_page_scroller上的任何滚动?

I was able to solve this by creating a class that extends ScrollView 我能够通过创建一个扩展ScrollView的类来解决这个问题

public class PausableScrollView extends ScrollView {
    private boolean scrollEnabled = true;
    public void setScrollEnabled(boolean scrollEnabled){
        this.scrollEnabled = scrollEnabled;
    }

    public PausableScrollView(Context context) { super(context); }

    public PausableScrollView(Context context, AttributeSet attrs) { super(context, attrs); }

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

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt){
        if(scrollEnabled) {
            super.onScrollChanged(l, t, oldl, oldt);
        }
    }

    @Override
    public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
        if(scrollEnabled) {
            super.onNestedScroll(target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
        }
    }
}

Basically, I can call newsPageScroller.setScrollEnabled(false) to ignore all scrolling events taking place on the newsPageScroller . 基本上,我可以调用newsPageScroller.setScrollEnabled(false)来忽略newsPageScroller上发生的所有滚动事件。

This will disable scrolling the parent from a child during fling and scroll without having to manually enable and disable: 这将禁止在投掷和滚动期间从子项滚动父项,而无需手动启用和禁用:

class ContainerScrollView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) 
    : ScrollView(context, attrs, defStyleAttr) {

    private var isScrollingChild = false

    override fun onScrollChanged(l: Int, t: Int, oldl: Int, oldt: Int) {
        if (!isScrollingChild) {
            super.onScrollChanged(l, t, oldl, oldt)
        }
    }

    override fun onNestedScroll(target: View?, dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int) {
        if (!isScrollingChild) {
            super.onNestedScroll(target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed)
        }
    }

    override fun onStartNestedScroll(child: View?, target: View?, nestedScrollAxes: Int): Boolean {
        isScrollingChild = true
        return super.onStartNestedScroll(child, target, nestedScrollAxes)
    }

    override fun onStopNestedScroll(target: View?) {
        isScrollingChild = false
        super.onStopNestedScroll(target)
    }
}

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

相关问题 启用viewSwitcher后,如何在ScrollView中禁用滚动? - How to disable scrolling in ScrollView when the viewSwitcher is enabled? 当ScrollView滚动时,Android禁用子onTouch - Android disable child onTouch when ScrollView scrolls ScrollView 不作为父级滚动,RelativeLayout 作为子级滚动 - ScrollView not scrolling as parent and RelativeLayout as child 当ScrollView作为父布局时,ScrollView不滚动 - ScrollView not scrolling when ScrollView as parent layout 当ScrollView不在顶部时,禁用SwipeRefreshLayout - Disable SwipeRefreshLayout when the ScrollView is not on the top 平移时禁用滚动视图 map - disable scrollview when panning map 固定高度的TextView禁用父级scrollview的滚动 - Fixed height TextView disable scrolling of the parent scrollview 当android:softinputmode =“adjustpan”启用时,scrollview不会滚动 - scrollview will not scroll when android:softinputmode=“adjustpan” enabled 使子视图与父滚动视图的宽度匹配 - Make a child view match the width of a parent scrollview 带有expandablelistview父级水平scrollview子项的视频库 - Video gallery with expandablelistview parent horizontal scrollview child
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM