最近闲来无事,随便看看各种UI实现的代码

打开你的微信朋友圈,点击评论,你就会发现有一个小细节:文本输入框的高度恰好定位到这条信息的底部位置

这个实现起来其实很简单,咱们就来看看吧

最简单的RecyclerView

依然是先实现RecyclerView。跟朋友圈一样,我们也把头给加上去,这样我们在点第一条信息的时候,效果会更好一些

信息内容简单些,反正我们就看看效果

android:orientation="vertical" android:layout_width="match_parent"

android:layout_height="wrap_content">

android:id="@+id/tv_title"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textSize="12sp" />

android:id="@+id/tv_comment"

android:text="评论"

android:textSize="14sp"

android:layout_margin="5dip"

android:textColor="@color/colorAccent"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

头部也很简单,就一张图片作为区分

android:layout_width="match_parent" android:layout_height="300dip">

android:layout_centerInParent="true"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@mipmap/ic_launcher"/>

消息内容就以string作为信息数据类型,头的数据类型为TopClass

data class TopClass(val value: String)

实现一个adapter

class MainAdapter(private val beans: ArrayList, val context: Context) : RecyclerView.Adapter() {

var height = 0

enum class TYPE(val value: Int) {

TOP(0), NORMAL(1)

}

override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder {

when(viewType) {

TYPE.NORMAL.value -> {

val view = LayoutInflater.from(context).inflate(R.layout.adapter_main, parent, false)

return MainNormalViewHolder(view)

}

TYPE.TOP.value -> {

val view = LayoutInflater.from(context).inflate(R.layout.adapter_top, parent, false)

return MainTopViewHolder(view)

}

}

throw Exception()

}

override fun getItemCount() = beans.size

override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {

if (holder != null) {

when(getItemViewType(position)) {

TYPE.NORMAL.value -> {

(holder as MainNormalViewHolder).setText(beans[position] as String)

holder.clickComment(holder.layoutPosition)

}

TYPE.TOP.value -> {}

}

}

}

override fun getItemViewType(position: Int): Int {

when(beans[position]) {

is String -> return TYPE.NORMAL.value

is TopClass -> return TYPE.TOP.value

}

return super.getItemViewType(position)

}

inner class MainNormalViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

fun setText(text: String) {

itemView.tv_title.text = text

}

fun clickComment(position: Int) {

itemView.tv_comment.setOnClickListener {

(context as MainActivity).showInputComment(itemView.tv_comment, position)

}

}

}

inner class MainTopViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

}

这样一个列表就完成了

输入框的产生

这里有一个关键的地方,如何将EditText悬浮在键盘上,并且RecyclerView不会被挤上去。这里我们可以使用Dialog,同时在布局中要使用ScrollView来进行占位

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_weight="1">

android:layout_width="match_parent"

android:layout_height="0.5dip"

android:background="#666666">

android:id="@+id/dialog_layout_comment"

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="确认"/>

只有ScrollView进行配合,才能实现我们的效果。

来看看效果

列表的滚动

输入框也有了,这时候就差滚动了。我们可以通过smoothScrollBy来让RecyclerView按X或者Y轴进行滚动。那我们这里到底应该滚动多少距离才对呢?,咱们来计算一下吧

图中红色部分为键盘展现之前某条信息评论区所在位置;蓝色部分为键盘,当键盘打开的时候,我们需要将红色的部分移动到黄色的位置。这样黄色顶部与红色顶部中间的区域高度,就是RecyclerView需要滚动的数值这样就好办了,我们使用getLocationOnScreen去获取差值,再加上评论区域高度就行了

fun showInputComment(commentView: View, position: Int) {

// RV中评论区起始Y的位置

val rvInputY = getY(commentView)

val rvInputHeight = commentView.height

dialog = Dialog(this, android.R.style.Theme_Translucent_NoTitleBar)

dialog!!.setContentView(R.layout.dialog_comment)

dialog!!.show()

val handler = object : Handler() {}

handler.postDelayed({

// 对话框中的输入框Y的位置

val dialogY = getY(dialog!!.findViewById(R.id.dialog_layout_comment))

rv_main.smoothScrollBy(0, rvInputY - (dialogY - rvInputHeight))

}, 300)

}

private fun getY(view: View): Int {

val rect = IntArray(2)

view.getLocationOnScreen(rect)

return rect[1]

}

来看看效果

但是还有几个小问题,如果是点击最后一行的话,会因为滚动空间不足而不能实现相同的效果,并且按返回键的时候,键盘先消失,然后再按一次之后Dialog才消失。

针对第一个问题,我们直接添加一个空View作为列表最后一项即可,并且高度要等于输入框的高度;第二个问题也很简单,就是监听键盘弹出与隐藏时View高度发生的变化

data class BottomClass(val value: String)

点击的时候再添加

handler.postDelayed({

// 对话框中的输入框Y的位置

val dialogY = getY(dialog!!.findViewById(R.id.dialog_layout_comment))

if (position == arrays.size - 1) {

arrays.add(BottomClass(""))

adapter?.height = dialog!!.findViewById(R.id.dialog_layout_comment).height

adapter?.notifyDataSetChanged()

}

rv_main.smoothScrollBy(0, rvInputY - (dialogY - rvInputHeight))

}, 300)

关闭Dialog的时候删除这个对象

window.decorView.viewTreeObserver.addOnGlobalLayoutListener {

val rect = Rect()

window.decorView.getWindowVisibleDisplayFrame(rect)

val displayHeight = rect.bottom - rect.top

val height = window.decorView.height

val keyboardHeight = height - displayHeight

if (previousKeyboardHeight != keyboardHeight) {

val hide = displayHeight.toDouble() / height > 0.8

if (hide) {

if (arrays[arrays.size - 1] is BottomClass) {

arrays.removeAt(arrays.size - 1)

adapter?.notifyDataSetChanged()

}

dialog?.dismiss()

}

}

}

来看看最终效果

总结

以上所述是小编给大家介绍的Android仿微信朋友圈点击评论自动定位到相关行功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

android 微信评论功能,Android仿微信朋友圈点击评论自动定位到相关行功能相关推荐

  1. android 微信评论功能,Android仿微信朋友圈点击评论自动定位到相关行功能.pdf

    Android仿仿微微信信朋朋友友圈圈点点击击评评论论自自动动定定位位到到相相关关行行功功能能 这篇文章主要介绍了android仿微信朋友圈点击评论自动定位到相关行功能的实现,本文图文并茂给大家介绍的 ...

  2. android 查找朋友圈,Android仿微信朋友圈点击评论自动定位到相关行功能

    最近闲来无事,随便看看各种UI实现的代码 打开你的微信朋友圈,点击评论,你就会发现有一个小细节:文本输入框的高度恰好定位到这条信息的底部位置 这个实现起来其实很简单,咱们就来看看吧 最简单的Recyc ...

  3. 仿微信朋友圈点击评论自动定位到对应位置

    参考文章:仿微信朋友圈点击评论自动定位到相关行 一.思路: 1.在点击[评论]控件的时候弹出一个 dialog,dialog 上面为 ScrollView,下面为输入框,实现键盘弹出时把输入框顶上去的 ...

  4. 仿QQ空间、微信朋友圈点击评论弹出输入框

    从上周开始一直在寻找仿QQ空间.朋友圈类似的弹出输入框,并且需要解决键盘不遮挡内容的解决方案 在网上寻寻觅觅始终不得解,后来无意中看见一篇百度百科的文章总算给了我解决的思路 这个就是给了我启发的文章 ...

  5. Android 二维码扫描(仿微信界面),根据Google zxing

    Android 二维码扫描(仿微信界面),根据Google zxing Android项目开发中经常会用到二维码扫描,例如登陆.支付等谷歌方面已经有了一个开源库(地址: https://github. ...

  6. Android 使用 CameraX 快速实现仿微信短视频录制

    Android 使用 CameraX 快速实现仿微信短视频录制(轻触拍照.长按录像) https://github.com/ldlywt/MyCameraX 微信短视频android端 https:/ ...

  7. 漂流瓶php源码,微信小程序之仿微信漂流瓶

    [实例简介] 微信小程序之仿微信漂流瓶 [实例截图] [核心代码] a4c6f919-add7-4dc7-bafa-9a884a00f2e3 └── wx_plp ├── app.js ├── app ...

  8. android点击加号,Android仿微信朋友圈点击加号添加图片功能

    本文为大家分享了类似微信朋友圈,点击+号图片,可以加图片功能,供大家参考,具体内容如下 xml: xmlns:app="http://schemas.android.com/apk/res- ...

  9. html仿微信评论输入框,简单仿微信朋友圈评论功能

    [实例简介] 简单实现了微信朋友圈评论的功能,被点击的评论能够随着输入框高度的改变而改变位置! [实例截图] [核心代码] 简单仿微信朋友圈评论 └── TalkInTalk ├── AndroidM ...

最新文章

  1. 《深入浅出Ext JS(第2版)》获专家好评
  2. vCenter Server Appliance 5.5忘记root密码
  3. python官网下载步骤64位-windows下载并安装Python的具体步骤
  4. 终于有人把AI、BI、大数据、数据科学讲明白了
  5. (C语言)验证哥德巴赫猜想,输入一个大于6的偶数,输出这个数能被分解为哪两个质数的和
  6. java反射 获取属性_Java反射学习-3 - 反射获取属性,方法,构造器
  7. [转载] python.day02笔记
  8. 32线性空间06——行空间和左零空间
  9. [渝粤教育] 厦门理工学院 模拟电子技术实验 参考 资料
  10. 最新自动发卡网源码V7.0
  11. Oracle9i学习之boobooke小布版001
  12. 初始化oracle环境失败,Oracle登录显示无法初始化
  13. 拼多多竞价成功后多久有流量?万顿思教育
  14. TML5期末大作业:我的家乡网站设计——我的家乡大连 带登录注册表单 轮播
  15. 基于python和selenium爬取JD商城商品信息并且分析用户对于产品的满意程度
  16. 图片/视频url 转 File Blob
  17. 值得拥有并收藏的 3个安卓/鸿蒙手机解锁软件
  18. c语言头文件大全 chm,文件后缀名大全.pdf
  19. 163邮箱登录微信收发邮件,能给商务办公带来什么好处?
  20. 用MATLAB的APPDesigner对数字图像进行操作

热门文章

  1. 【UVM基础】工厂(factory)机制快速上手指南
  2. 新赛季上分神器 vivo X70 Pro+首批适配《王者荣耀》120Hz极高帧率
  3. xcode引入sdk_解决Xcode引入第三方SDK找不到头文件
  4. 单片机diy作品鉴赏,初学者进来膜拜
  5. 紫光云服务器芯片,紫光云与新华三半导体共建芯片设计云2.0 携手打造一站式云端芯片平台...
  6. 单片机定时器一1ms12MHz_【51单片机】基于STC89C52RC的多路电压采集系统
  7. html中table整体缩小,html-如何缩小表格样式中的间隙?
  8. php代码审计命令执行,PHP代码审计笔记--命令执行漏洞
  9. Perfmon - Windows 自带系统监控工具
  10. 2022 ICPC Gran Premio de Mexico Repechaje 题解