23

我想建立一个像whatsapp一样的消息布局。我有一个edittext 和一个recyclerView。

问题是当键盘出现时它会隐藏消息!

所以让我们说这个 RecyclerView:

---item 1---  
---item 2---  
---item 3---  
---item 4---  
---EditText---

当键盘出现时,我得到了这个:

---item 1---  
---item 2---  
---EditText---
---Keyboard---  

但我想得到这个:

---item 3---  
---item 4---  
---EditText---
---Keyboard---

注意:当我设置linearLayoutManager.setStackFromEnd(true);它时它可以工作,但是当有一条消息时,它会出现在页面底部。

4

5 回答 5

28

使用 recyclerview 和 editText 为活动设置 adjustresize :

android:windowSoftInputMode="adjustResize"

将 onLayoutChangeListener 添加到 RecyclerView 并在 onLayoutChange 中将 scrollToPosition 设置为 data.size() -1:

  mRecyclerView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
 @Override

public void onLayoutChange(View v, int left, int top, int right,int bottom, int oldLeft, int oldTop,int oldRight, int oldBottom)
{

mRecyclerView.scrollToPosition(mMessages.size()-1);

}
    });
于 2016-05-26T09:05:20.980 回答
2

我这样做的方式是根据item holder的大小设置setStackFromEnd(),并在androidManifest中设置adjustResize。

这就是我检查的方式:

if (recycleView.getChildCount() == items.size()){
    mLayoutManager.setStackFromEnd(true);
}
else{
    mLayoutManager.setStackFromEnd(false);
}
于 2015-10-31T01:57:41.703 回答
2

我不擅长英语。使用 ChatRecyclerView 并将线性布局管理器的 stackfromend 设置为 false。

public class ChatRecyclerView extends RecyclerView {
    private int oldHeight;

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

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

    public ChatRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        int delta = b - t - this.oldHeight;
        this.oldHeight = b - t;
        if (delta < 0) {
            this.scrollBy(0, -delta);
        }
    }
}
于 2018-04-04T14:40:24.333 回答
1

感谢 Kotlin 中的devdoot Ghosh(我检查适配器大小是否 > 0):

recycler_view.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ ->
    // Wait till recycler_view will update itself and then scroll to the end.
    recycler_view.post {
        adapter?.itemCount?.takeIf { it > 0 }?.let {
            scrollToPosition(it - 1)
        }
    }
}

除了AndroidManifest活动:

<activity
    ...
    android:windowSoftInputMode="adjustResize"
    />

请注意,RecyclerView将滚动到最后一项。

于 2020-08-31T10:17:07.057 回答
0

单击编辑文本时,我从推送内容中获得了解决方案

只需将以下代码放入

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
于 2017-09-20T10:21:01.973 回答