小编给大家分享一下Android如何实现微信朋友圈评论EditText效果,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
具体内容如下
效果图
当我们点击某一天朋友圈的评论是,列表也会跟随着滑动,使得键盘刚好在我们点击的那条评论上方
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];
}
看完了这篇文章,相信你对“Android如何实现微信朋友圈评论EditText效果”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。