【问题标题】:How to push recyclerView up when keyboard appear?键盘出现时如何向上推recyclerView?
【发布时间】:2015-12-07 00:41:19
【问题描述】:

我想在 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); 时,它可以工作,但是当有一条消息时,它会出现在页面底部。

【问题讨论】:

  • 请查看this 解决方法。
  • 你知道了吗@david?
  • 我从这个link得到了解决方案

标签: android android-recyclerview adjustpan


【解决方案1】:

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

这是我检查的方式:

if (recycleView.getChildCount() == items.size()){
    mLayoutManager.setStackFromEnd(true);
}
else{
    mLayoutManager.setStackFromEnd(false);
}

【讨论】:

  • 这里的条件是什么意思?如果setStackFromEnd(true) 适用于所有情况怎么办?
  • 不是一个好的解决方案。即使您将其插入到addOnLayoutChangeListener 中,如最佳答案所示,如果RecyclerView 已经滚动到末尾,它也会解除它。另外,如果RecyclerView 包含的项目很少,则上面会有一个空白区域,请参阅stackoverflow.com/questions/44873389/…
【解决方案2】:

使用 recyclerview 和 editText 为活动设置调整大小:

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);

}
    });

【讨论】:

  • 如果你使用 reverseLayout 和 setStackFromEnd = true 则需要使用 scrollToPosition(0),在什么情况下使用 post。示例:recycler.post{recycler.scrollToPosition(0)}
  • 谢谢!可能您应该检查 mMessages.size() > 0 以免出现异常。请参阅下面在 Kotlin 中的回答。
  • 实现这个之后,我什至无法滚动回收站视图,当我尝试滚动一点时,它会将我推到底部(对话视图案例)
【解决方案3】:

我从Push up content when clicking in edit text得到了解决方案

把下面的代码放在

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

【讨论】:

  • 仅仅指向另一个 SO问题确实不足以构成答案。
  • 这还不够。查看最佳答案。
【解决方案4】:

我英语不好。 使用 ChatRecyclerView 并将 linearlayoutmanager 的 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);
        }
    }
}

【讨论】:

    【解决方案5】:

    感谢 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 将滚动到最后一项。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-15
      • 1970-01-01
      相关资源
      最近更新 更多