一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Android - Android仿微信朋友圈點擊評論自動定位到相關行功能

Android仿微信朋友圈點擊評論自動定位到相關行功能

2022-02-24 15:10haoxuhong Android

這篇文章主要介紹了android仿微信朋友圈點擊評論自動定位到相關行功能的實現,本文圖文并茂給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下

最近閑來無事,隨便看看各種UI實現的代碼

本文涉及到的相關代碼已經上傳到 https://github.com/r17171709/android_demo/tree/master/WeixinEditText

打開你的微信朋友圈,點擊評論,你就會發現有一個小細節:文本輸入框的高度恰好定位到這條信息的底部位置

Android仿微信朋友圈點擊評論自動定位到相關行功能

這個實現起來其實很簡單,咱們就來看看吧

最簡單的RecyclerView

依然是先實現RecyclerView。跟朋友圈一樣,我們也把頭給加上去,這樣我們在點第一條信息的時候,效果會更好一些

信息內容簡單些,反正我們就看看效果

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?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>

頭部也很簡單,就一張圖片作為區分

?
1
2
3
4
5
6
7
8
9
<?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

?
1
data class TopClass(val value: String)

實現一個adapter

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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)
}

這樣一個列表就完成了

Android仿微信朋友圈點擊評論自動定位到相關行功能

輸入框的產生

這里有一個關鍵的地方,如何將EditText懸浮在鍵盤上,并且RecyclerView不會被擠上去。這里我們可以使用Dialog,同時在布局中要使用ScrollView來進行占位

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?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進行配合,才能實現我們的效果。

來看看效果

Android仿微信朋友圈點擊評論自動定位到相關行功能

列表的滾動

輸入框也有了,這時候就差滾動了。我們可以通過smoothScrollBy來讓RecyclerView按X或者Y軸進行滾動。那我們這里到底應該滾動多少距離才對呢?,咱們來計算一下吧

Android仿微信朋友圈點擊評論自動定位到相關行功能

圖中紅色部分為鍵盤展現之前某條信息評論區所在位置;藍色部分為鍵盤,當鍵盤打開的時候,我們需要將紅色的部分移動到黃色的位置。這樣黃色頂部與紅色頂部中間的區域高度,就是RecyclerView需要滾動的數值這樣就好辦了,我們使用getLocationOnScreen去獲取差值,再加上評論區域高度就行了

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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]
}

來看看效果

Android仿微信朋友圈點擊評論自動定位到相關行功能

但是還有幾個小問題,如果是點擊最后一行的話,會因為滾動空間不足而不能實現相同的效果,并且按返回鍵的時候,鍵盤先消失,然后再按一次之后Dialog才消失。

針對第一個問題,我們直接添加一個空View作為列表最后一項即可,并且高度要等于輸入框的高度;第二個問題也很簡單,就是監聽鍵盤彈出與隱藏時View高度發生的變化

data class BottomClass(val value: String)

點擊的時候再添加

?
1
2
3
4
5
6
7
8
9
10
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的時候刪除這個對象

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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仿微信朋友圈點擊評論自動定位到相關行功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!

原文鏈接:https://blog.csdn.net/haoxuhong/article/details/80446806

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 免费理伦片手机在线播放 | 美妇在男人胯下哀求 | 久久se视频精品视频在线 | 色老头影视 | 亚洲 欧美 偷自乱 图片 | 被高跟鞋调教丨vk | 精品精品国产自在香蕉网 | 亚洲国产成人在人网站天堂 | 亚洲一区二区三区免费视频 | 深夜影院a| 亚欧日韩| 99久久免费看精品国产一区 | 2021最新国产成人精品视频 | 大陆国语自产精品视频在 | 国产成人h视频在线播放网站 | 国产成人福利免费观看 | 免费看成人毛片日本久久 | 美女黑人做受xxxxxⅹ | 久久综合亚洲色hezyo | 精品精品国产yyy5857香蕉 | 日韩视频在线免费观看 | 爽好舒服快想要免费看 | 成人私人影院在线观看网址 | caopren免费视频国产 | 韩国一区二区三区 | 亚洲精品第三页 | 色姑娘导航 | bt岛www| 免费成人在线观看视频 | 深夜www| 多人群p全肉小说 | 亚洲2卡三卡4卡5卡精品 | 日本护士xxxx视频免费 | 午夜毛片在线观看 | 青青热久免费精品视频精品 | 视频亚洲一区 | 日本韩国一区二区三区 | 国产高清不卡码一区二区三区 | 亚洲欧洲日产v特级毛片 | 精品无码人妻一区二区免费AV | 国产香蕉久久 |