這篇文章主要介紹“Android怎么實(shí)現(xiàn)仿微信朋友圈點(diǎn)擊評(píng)論自動(dòng)定位到相關(guān)行功能”,在日常操作中,相信很多人在Android怎么實(shí)現(xiàn)仿微信朋友圈點(diǎn)擊評(píng)論自動(dòng)定位到相關(guān)行功能問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Android怎么實(shí)現(xiàn)仿微信朋友圈點(diǎn)擊評(píng)論自動(dòng)定位到相關(guān)行功能”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

打開(kāi)你的微信朋友圈,點(diǎn)擊評(píng)論,你就會(huì)發(fā)現(xiàn)有一個(gè)小細(xì)節(jié):文本輸入框的高度恰好定位到這條信息的底部位置

這個(gè)實(shí)現(xiàn)起來(lái)其實(shí)很簡(jiǎn)單,咱們就來(lái)看看吧
最簡(jiǎn)單的RecyclerView
依然是先實(shí)現(xiàn)RecyclerView。跟朋友圈一樣,我們也把頭給加上去,這樣我們?cè)邳c(diǎn)第一條信息的時(shí)候,效果會(huì)更好一些
信息內(nèi)容簡(jiǎn)單些,反正我們就看看效果
<?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="評(píng)論" android:textSize="14sp" android:layout_margin="5dip" android:textColor="@color/colorAccent" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
頭部也很簡(jiǎn)單,就一張圖片作為區(qū)分
<?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>
消息內(nèi)容就以string作為信息數(shù)據(jù)類型,頭的數(shù)據(jù)類型為T(mén)opClass
data class TopClass(val value: String)
實(shí)現(xiàn)一個(gè)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)
}這樣一個(gè)列表就完成了

輸入框的產(chǎn)生
這里有一個(gè)關(guān)鍵的地方,如何將EditText懸浮在鍵盤(pán)上,并且RecyclerView不會(huì)被擠上去。這里我們可以使用Dialog,同時(shí)在布局中要使用ScrollView來(lái)進(jìn)行占位
<?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="確認(rèn)"/> </LinearLayout> </LinearLayout>
只有ScrollView進(jìn)行配合,才能實(shí)現(xiàn)我們的效果。
來(lái)看看效果

列表的滾動(dòng)
輸入框也有了,這時(shí)候就差滾動(dòng)了。我們可以通過(guò)smoothScrollBy來(lái)讓RecyclerView按X或者Y軸進(jìn)行滾動(dòng)。那我們這里到底應(yīng)該滾動(dòng)多少距離才對(duì)呢?,咱們來(lái)計(jì)算一下吧

圖中紅色部分為鍵盤(pán)展現(xiàn)之前某條信息評(píng)論區(qū)所在位置;藍(lán)色部分為鍵盤(pán),當(dāng)鍵盤(pán)打開(kāi)的時(shí)候,我們需要將紅色的部分移動(dòng)到黃色的位置。這樣黃色頂部與紅色頂部中間的區(qū)域高度,就是RecyclerView需要滾動(dòng)的數(shù)值這樣就好辦了,我們使用getLocationOnScreen去獲取差值,再加上評(píng)論區(qū)域高度就行了
fun showInputComment(commentView: View, position: Int) {
// RV中評(píng)論區(qū)起始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({
// 對(duì)話框中的輸入框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]
}來(lái)看看效果

但是還有幾個(gè)小問(wèn)題,如果是點(diǎn)擊最后一行的話,會(huì)因?yàn)闈L動(dòng)空間不足而不能實(shí)現(xiàn)相同的效果,并且按返回鍵的時(shí)候,鍵盤(pán)先消失,然后再按一次之后Dialog才消失。
針對(duì)第一個(gè)問(wèn)題,我們直接添加一個(gè)空View作為列表最后一項(xiàng)即可,并且高度要等于輸入框的高度;第二個(gè)問(wèn)題也很簡(jiǎn)單,就是監(jiān)聽(tīng)鍵盤(pán)彈出與隱藏時(shí)View高度發(fā)生的變化
data class BottomClass(val value: String)
點(diǎn)擊的時(shí)候再添加
handler.postDelayed({
// 對(duì)話框中的輸入框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)關(guān)閉Dialog的時(shí)候刪除這個(gè)對(duì)象
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()
}
}
}來(lái)看看最終效果

到此,關(guān)于“Android怎么實(shí)現(xiàn)仿微信朋友圈點(diǎn)擊評(píng)論自動(dòng)定位到相關(guān)行功能”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
分享標(biāo)題:Android怎么實(shí)現(xiàn)仿微信朋友圈點(diǎn)擊評(píng)論自動(dòng)定位到相關(guān)行功能-創(chuàng)新互聯(lián)
URL鏈接:http://chinadenli.net/article20/gddco.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、網(wǎng)站收錄、網(wǎng)站制作、全網(wǎng)營(yíng)銷推廣、品牌網(wǎng)站建設(shè)、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容