【问题标题】:Disable and enable parent scroll based on recycler view visibility根据回收站视图可见性禁用和启用父滚动
【发布时间】:2025-12-19 11:15:11
【问题描述】:

我有一个编辑文本。当用户点击它时,我会根据键入的关键字在回收站视图中显示建议。包含编辑文本(以及其他项目)的父视图是可滚动的。我希望在回收器视图可见时禁用父滚动,并在回收器视图不可见时再次启用。

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/parent_scroll_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<EditText
..
..
..
..
</EditText


    <android.support.v7.widget.RecyclerView
    android:id="@+id/mentions_grid"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
<ScrollView>

当回收器视图作为edittext的下拉列表可见时,如何禁用主父滚动视图?

【问题讨论】:

    标签: java android android-recyclerview


    【解决方案1】:

    试试这个方法

    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    import android.widget.ScrollView;
    
     public class MyScrollView extends ScrollView {
    
    private boolean enableScrolling = true;
    
    public boolean isEnableScrolling() {
        return enableScrolling;
    }
    
    public void setEnableScrolling(boolean enableScrolling) {
        this.enableScrolling = enableScrolling;
    }
    
    public MyScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    
    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public MyScrollView(Context context) {
        super(context);
    }
    
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
    
        if (isEnableScrolling()) {
            return super.onInterceptTouchEvent(ev);
        } else {
            return false;
        }
    }
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
       if (isEnableScrolling()) {
            return super.onTouchEvent(ev);
       } else {
           return false;
       }
     }
     }
    

    并像这样制作xml

    <yourpackage.MyScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/parent_scroll_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    
    <EditText
    

    .. .. .. ..

    </yourpackage.MyScrollView>
    
    <android.support.v7.widget.RecyclerView
    android:id="@+id/mentions_grid"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
    

    并且根据您的要求,您可以启用或禁用滚动 像这样

    MyScrollView myScrollView = (MyScrollView) findViewById(R.id.myScroll);
    if(yourrecyclerview adapter item count > 0)//or you can make condition like if(yourrecyclerview.isShown())
    {
       myScrollView.setEnableScrolling(false); 
    }else
    {
        myScrollView.setEnableScrolling(true);
    }
    

    【讨论】:

    • 这应该只禁用父滚动视图,但同时回收器视图滚动也被禁用,应该启用。
    • 这是因为您在此自定义滚动视图中的回收器视图位于其外部。,显示更新的答案
    • 需要一个这样的版本,其中 recyclerview 在滚动视图内部,只有滚动视图被禁用,而不是 recyclerview。
    最近更新 更多