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

所属分类: 软件编程 / Android 阅读数: 40
收藏 0 赞 0 分享

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

本文涉及到的相关代码已经上传到 https://github.com/r17171709/android_demo/tree/master/WeixinEditText

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

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

最简单的RecyclerView

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

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="match_parent"
  android:layout_height="wrap_content">
  <TextView
    android:id="@+id/tv_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="12sp" />
  <TextView
    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" />
</LinearLayout>

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

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent" android:layout_height="300dip">
  <ImageView
    android:layout_centerInParent="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher"/>
</RelativeLayout>

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

data class TopClass(val value: String)

实现一个adapter

class MainAdapter(private val beans: ArrayList<Any>, val context: Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
  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来进行占位

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1">
  </ScrollView>
  <View
    android:layout_width="match_parent"
    android:layout_height="0.5dip"
    android:background="#666666"></View>
  <LinearLayout
    android:id="@+id/dialog_layout_comment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <EditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1"/>
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="确认"/>
  </LinearLayout>
</LinearLayout>

只有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<LinearLayout>(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<LinearLayout>(R.id.dialog_layout_comment))
  if (position == arrays.size - 1) {
    arrays.add(BottomClass(""))
    adapter?.height = dialog!!.findViewById<LinearLayout>(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仿微信朋友圈点击评论自动定位到相关行功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多精彩内容其他人还在看

使用ViewPager实现android软件使用向导功能实现步骤

现在的大部分android软件,都是使用说明,就是第一次使用该软件时,会出现向导,可以左右滑动,然后就进入应用的主界面了,下面我们就实现这个功能
收藏 0 赞 0 分享

android在异步任务中关闭Cursor的代码方法

android在异步任务中如何关闭Cursor?在我们开发应用的时候,很多时候会遇到这种问题,下面我们就看看代码如何实现
收藏 0 赞 0 分享

Android自定义桌面功能代码实现

android自定义桌面其实很简单,看一个例子就明白了
收藏 0 赞 0 分享

android将图片转换存到数据库再从数据库读取转换成图片实现代码

有时候我们想把图片存入到数据库中,尽管这不是一种明智的选择,但有时候还是不得以会用到,下面说说将图片转换成byte[]数组存入到数据库中去,并从数据库中取出来转换成图像显示出来
收藏 0 赞 0 分享

TextView显示系统时间(时钟功能带秒针变化

用System.currentTimeMillis()可以获取系统当前的时间,我们可以开启一个线程,然后通过handler发消息,来实时的更新TextView上显示的系统时间,可以做一个时钟的功能
收藏 0 赞 0 分享

Android用ListView显示SDCard文件列表的小例子

本文简单实现了用ListView显示SDCard文件列表,目录的回退等功能暂不讨论,获取文件列表,files即为所选择目录下的所有文件列表
收藏 0 赞 0 分享

Android拦截外拨电话程序示例

这篇文章主要介绍了Android拦截外拨电话的示例,大家参考使用吧
收藏 0 赞 0 分享

通过Html网页调用本地安卓(android)app程序代码

如何使用html网页和本地app进行传递数据呢?经过研究,发现还是有方法的,总结了一下,大致有一下几种方式
收藏 0 赞 0 分享

android Textview文字监控(Textview使用方法)

以手机号充值为例,当用户输入最后一位数时候,进行汇率的变换,本文就实现类似这样的功能
收藏 0 赞 0 分享

Android ListView长按弹出菜单二种实现方式示例

这篇文章主要介绍了Android ListView长按弹出菜单的方法,大家参考实现
收藏 0 赞 0 分享
查看更多