如果焦点为edittext,则禁止滚动recyclerview [英] Forbid scroll recyclerview if focus edittext

查看:251
本文介绍了如果焦点为edittext,则禁止滚动recyclerview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有水平滚动条的回收站视图. recyclerview中的每个项目都有edittext.当我单击edittext时,recyclerview滚动到结束.我如何禁止滚动? 谢谢!

I have a recycler view with horizontal scroll. Every item in recyclerview has edittext. When i click on edittext, recyclerview scroll to end. How i can forbid scrolling? Thanks!

推荐答案

这可以通过使用两个侦听器来解决.第一个在用户触摸输入字段时禁用滚动:

This can be fixed by using two listeners. The first one one disables scrolling while the user touches input field:

private View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus){
            manager.setScrollEnabled(false);
        }
    }
};

在onBindViewHolder()方法中,将onFocusChangeListener设置为RecyclerView的每个项目的每个输入字段.第二个侦听器可在用户触摸时滚动:

Set onFocusChangeListener to every input field of every item of your RecyclerView in onBindViewHolder() method. The second listener enables scrolling on user touch:

private RecyclerView.OnItemTouchListener onItemTouchListener = new RecyclerView.OnItemTouchListener() {
    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
        RecyclerView.LayoutManager manager = rv.getLayoutManager();
        if (manager instanceof CustomLinearLayoutManager){
            ((CustomLinearLayoutManager)manager).setScrollEnabled(true);
        }
        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView rv, MotionEvent e) {

    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

    }
};

此答案中描述了允许您启用或禁用滚动的自定义LinearLayoutManager的代码:如何禁用RecyclerView滚动?

The code for custom LinearLayoutManager, that allows you to enable or disable scrolling is described in this answer: How to disable RecyclerView scrolling?

诀窍在于覆盖一种方法:

The trick is in overriding one method:

@Override
public boolean canScrollHorizontally() {
    return isScrollEnabled && super.canScrollHorizontally();
}

要启用/禁用滚动行为时,将变量isScrollEnabled设置为:

And setting the variable isScrollEnabled when you want to enable/disable scrolling behavior:

public void setScrollEnabled(boolean flag) {
    this.isScrollEnabled = flag;
}

这篇关于如果焦点为edittext,则禁止滚动recyclerview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆