android 微信评论,Android实现微信朋友圈评论EditText效果

本文介绍了如何实现在微信朋友圈评论区点击时,EditText跟随键盘滑动并隐藏的技巧,通过监听键盘显示与隐藏,配合Dialog和滑动列表操作,确保用户体验。适合学习Android UI交互设计的开发者参考。
摘要由CSDN通过智能技术生成

本文主要讲解实现微信朋友圈评论EditText效果思路,供大家参考,具体内容如下

效果图

eede4bc767b9570a8f9f8ebc5218afd4.png

当我们点击某一天朋友圈的评论是,列表也会跟随着滑动,使得键盘刚好在我们点击的那条评论上方

getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

@Override

public void onGlobalLayout() {

// 这里可以监听到键盘显示与隐藏时界面可视区域的变化

Rect rect = new Rect();

View decorView = getWindow().getDecorView();

decorView.getWindowVisibleDisplayFrame(rect);

int displayHeight = rect.bottom - rect.top;

// 拿到键盘的高度,可能会有误差,需要优化

keyboardHeight = decorView.getHeight() - displayHeight;

if (displayHeight * 1.0 / decorView.getHeight() > 0.8) {

dialog.dismiss();

}

}

});

考虑到评论的EditText是可以隐藏的,所以把它写到Dialog中,初始化Dialog的代码就不贴出来了

点击弹出Dialog

private void showInputComment(View commentView, final int position) {

// 拿到评论按钮在屏幕中的坐标

final int rvInputY = getY(commentView);

// 拿到评论按钮高度

final int rvInputHeight = commentView.getHeight();

dialog.show();

handler.postDelayed(new Runnable() {

@Override

public void run() {

int dialogY = getY(dialog.findViewById(R.id.dialog_layout_comment));

// 滑动列表

rv.smoothScrollBy(0, rvInputY - keyboardHeight + dialogY + rvInputHeight);

}

}, 300);

}

/**

* 拿到View在屏幕中的坐标

* @param commentView

* @return

*/

private int getY(View commentView) {

int[] outLocation = new int[2];

commentView.getLocationOnScreen(outLocation);

return outLocation[1];

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程圈。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值