当视图聚焦时,为什么RecyclerView滚动到视图顶部 [英] Why does RecyclerView scroll to top of view when view is focused

查看:116
本文介绍了当视图聚焦时,为什么RecyclerView滚动到视图顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有RecyclerView,有些聊天.项目为TextViews布局相反. RecyclerView以下的输入字段.当我滚动到RecyclerView的底部并单击到底部的项目时,它会变得集中(仅在获得焦点时,而不是每次单击时),而RecyclerView会自动滚动到该项目的顶部(当项目中的文本较大时)比屏幕高度).

I have RecyclerView, some kind of chat. Items are TextViews The layout is reversed. Input field below RecyclerView. When I scroll to the bottom of RecyclerView and click to the bottom item it gets focused (only when it gets focused, not every click) and RecyclerView scrolls automatically to the top of that item (when the text in item is larger than screen height).

此外,当键盘可见并且我单击到RecyclerView项时-键盘隐藏并且回收站视图滚动到该项的顶部,如何禁用这种行为?

Also when the keyboard is visible and I click to RecyclerView item - keyboard hides and recycler view scrolls to the top of that item, how to disable such behavior?

我希望它保持在打开键盘时或单击某些项目时的位置.

I want it to stay at the same position when it was when the keyboard was opened or where it was when I clicked on some item.

此方法隐藏了键盘

* * * * 
inputView.setOnFocusChangeListener((v, hasFocus) -> {
        if (!hasFocus){
            hideKeyboard();
        } 
    });
* * * * 

public void hideKeyboard() {
    InputMethodManager keyboard = (InputMethodManager)

    getSystemService(Context.INPUT_METHOD_SERVICE);
    keyboard.hideSoftInputFromWindow(inputView.getWindowToken(), 0);
}

RecyclerView的配置方式如下:

    RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.messageList);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
    linearLayoutManager.setReverseLayout(true);
    recyclerView.setLayoutManager(linearLayoutManager);
    recyclerView.setAdapter(chatAdapter = new chatAdapter(getContext()));

如果我做出一个按钮,然后单击该按钮将调用hideKeyBoard()-确定,它不会滚动,但是当我单击RecyclerView项时-它会变得集中并滚动到该项的顶部

If I make a button and will call hideKeyBoard() by clicking that button - its ok, it does not scroll, but when I click on RecyclerView item - it gets focused and scroll to top of that item

推荐答案

我找到了一个解决方案,我扩展了LinearLayoutManager并覆盖了一些方法.现在,它不会滚动到重点项目.也许有人知道更好的解决方案?

I found a solution, I extended LinearLayoutManager and override some methods. Now it not scrolling to focused items. Maybe someone knows better solution?

public class NotScrollingToFocuesChildrenLinearLayoutManager extends LinearLayoutManager {
public NotScrollingToFocuesChildrenLinearLayoutManager(Context context) {
    super(context);
}

public NotScrollingToFocuesChildrenLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
    super(context, orientation, reverseLayout);
}

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

@Override
public boolean onRequestChildFocus(RecyclerView parent, RecyclerView.State state, View child, View focused) {
    //return super.onRequestChildFocus(parent, state, child, focused);
    return true;
}

@Override
public boolean onRequestChildFocus(RecyclerView parent, View child, View focused) {
    //return super.onRequestChildFocus(parent, child, focused);
    return true;
}
}

这篇关于当视图聚焦时,为什么RecyclerView滚动到视图顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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