2017-08-02 81 views
1

我有RecyclerView,某种聊天类型。项目是TextViews布局颠倒。输入字段在RecyclerView以下。当我滚动到RecyclerView的底部并点击底部项目时,它会自动滚动到该项目的顶部(当项目中的文本大于屏幕高度时),它会自动滚动到该项目的顶部(仅当它聚焦时(而不是每次点击))和RecyclerView自动滚动到该项目的顶部。为什么RecyclerView在查看焦点时滚动到视图顶部

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

我希望它保持在相同的位置,当它是当键盘被打开或当我点击某个项目时。

键盘是用这种方法

* * * * 
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项目 - 它得到专注并滚动到该项目的顶部

回答

0

我找到了解决方案,我扩展了LinearLayoutMa急于和重写一些方法。现在它不滚动到重点项目。也许有人更好的解决方案

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; 
} 
} 
0

这个答案可能是你的解决方案,以及:RecyclerView scrolls to top after keyboard hides

<activity 
    android:windowSoftInputMode="adjustPan" 

Android Developer Site link

“adjustResize”

活动的主窗口总是被调整大小以腾出空间屏幕上的软键盘 。不调整

“adjustPan”

活动的主窗口,以腾出空间给软键盘 。相反,窗口的内容会自动移动 ,以便当前的焦点永远不会被键盘遮挡,并且用户总是可以看到他们正在键入的内容。这通常不太理想 比调整大小,因为用户可能需要关闭软键盘 得到并与窗口的被遮蔽的部分进行交互。

相关问题