使用RecyclerView實(shí)現(xiàn)點(diǎn)贊頭像疊加效果
概要:點(diǎn)贊頭像效果使用的地方很多,實(shí)現(xiàn)的方式也很多,下面通過(guò)使用RecyclerView實(shí)現(xiàn)一下
1、創(chuàng)建布局文件 一個(gè)recyclerview,一個(gè)點(diǎn)贊圖片
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="50dp" android:orientation="horizontal"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/rv_like_header" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="15dp" android:layoutAnimation="@anim/rv_slide_in_anim" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <ImageView android:id="@+id/iv_like" android:layout_width="16dp" android:layout_height="16dp" android:layout_marginTop="15dp" android:layout_marginEnd="15dp" android:src="@drawable/img_explore_like" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_goneMarginEnd="15dp" /> </androidx.constraintlayout.widget.ConstraintLayout>
recyclerview添加了動(dòng)畫(huà)效果,動(dòng)畫(huà)文件如下
rv_slide_in_anim
<?xml version="1.0" encoding="utf-8"?> <layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android" android:animation="@anim/slide_in_left" android:animationOrder="normal" android:delay="20%" />
slide_in_left
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="350" android:fromXDelta="-100%p" android:toXDelta="0" /> <alpha android:fromAlpha="0.8" android:toAlpha="1.0" android:duration="350" /> </set>
2、Activity邏輯代碼
val headerUrl=R.drawable.dog_01
rv_like_header.layoutManager=LinearLayoutManager(this).apply { orientation=LinearLayoutManager.HORIZONTAL }
val headerList= arrayListOf(headerUrl)
val mAdapter= LikeMemberHeaderAdapter(this,headerList)
rv_like_header.adapter=mAdapter
iv_like.setOnClickListener {
mAdapter.addData(headerUrl)
}
3、Adapter代碼
/**
* @date 2020/7/24
* @author peter
* @desc 點(diǎn)贊頭像adapter
*/
class LikeMemberHeaderAdapter(context: Context, data: ArrayList<Int>) : RecyclerView.Adapter<LikeMemberHeaderAdapter.HeaderHolder>() {
private var mDatas: ArrayList<Int> = data
private val mContext = context
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HeaderHolder {
return HeaderHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_member_header, parent, false))
}
override fun getItemCount(): Int {
return mDatas.size
}
fun addData(headerUrl: Int) {
// 從左邊添加
mDatas.add(0, headerUrl)
notifyDataSetChanged()
}
override fun onBindViewHolder(holder: HeaderHolder, position: Int) {
Glide.with(mContext).load(mDatas[position]).into(holder.ivMemberHeader)
// 最后一個(gè)顯示全部
if (position == mDatas.size - 1) {
setMargins(holder.headerRoot, 0, 0, 0, 0)
} else {
setMargins(holder.headerRoot, 0, 0, -12, 0)
}
}
private fun setMargins(v: View, l: Int, t: Int, r: Int, b: Int) {
if (v.layoutParams is MarginLayoutParams) {
val p = v.layoutParams as MarginLayoutParams
p.setMargins(l, t, r, b)
v.requestLayout()
}
}
class HeaderHolder(view: View) : RecyclerView.ViewHolder(view) {
val headerRoot: FrameLayout = view.findViewById(R.id.lin_header_root)
val ivMemberHeader: RoundImageView = view.findViewById(R.id.iv_member_header)
}
}
核心代碼就是通過(guò)設(shè)置item的margin值實(shí)現(xiàn)疊加效果,通過(guò)添加數(shù)據(jù),刷新adapter實(shí)現(xiàn),示例代碼中默認(rèn)從索引0添加的,可以根據(jù)需求自行調(diào)整,RoundImageView是一個(gè)自定義的圓形圖片,此處不再贅述
4、 Adapter item布局文件
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/lin_header_root" android:layout_width="wrap_content" android:layout_height="wrap_content"> <com.example.myapplication.likeHeader.RoundImageView android:id="@+id/iv_member_header" android:layout_width="17dp" android:layout_height="17dp" android:scaleType="centerCrop" app:maskDrawable="@drawable/circle_mask" tools:src="@drawable/member_default_header" /> </FrameLayout>
5、效果圖

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)本地上傳圖片并設(shè)置為圓形頭像
- Android使用CircleImageView實(shí)現(xiàn)圓形頭像的方法
- Android根據(jù)電話號(hào)碼獲得聯(lián)系人頭像實(shí)例代碼
- Android Studio實(shí)現(xiàn)帶邊框的圓形頭像
- Android實(shí)現(xiàn)用戶頭像更換操作
- Android實(shí)現(xiàn)從本地圖庫(kù)/相機(jī)拍照后裁剪圖片并設(shè)置頭像
- Android一行代碼實(shí)現(xiàn)圓形頭像
- Android頭像上傳功能的實(shí)現(xiàn)代碼(獲取頭像加剪切)
- Android手機(jī)拍照或選取圖庫(kù)圖片作為頭像
- Android實(shí)現(xiàn)個(gè)人資料頁(yè)面頭像背景模糊顯示包(狀態(tài)欄)
相關(guān)文章
Android通過(guò)PHP服務(wù)器實(shí)現(xiàn)登錄功能
這篇文章主要為大家詳細(xì)介紹了Android通過(guò)PHP服務(wù)器實(shí)現(xiàn)登錄功能的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Android開(kāi)發(fā)之splash界面下詳解及實(shí)例
這篇文章主要介紹了 Android開(kāi)發(fā)之splash界面下詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-03-03
Android使用selector修改TextView中字體顏色和背景色的方法
這篇文章主要介紹了Android使用selector修改TextView中字體顏色和背景色的方法,實(shí)例分析了selector方法的相關(guān)使用技巧,需要的朋友可以參考下2016-01-01
android自定義Toast設(shè)定顯示時(shí)間
這篇文章主要為大家詳細(xì)介紹了android自定義Toast設(shè)定顯示時(shí)間,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
ActivityManagerService之Service啟動(dòng)過(guò)程解析
這篇文章主要為大家介紹了ActivityManagerService之Service啟動(dòng)過(guò)程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Android中ListView下拉刷新的實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了Android中ListView下拉刷新的實(shí)現(xiàn)方法,感興趣的小伙伴們可以參考一下2016-03-03
Android應(yīng)用程序的編譯流程及使用Ant編譯項(xiàng)目的攻略
這篇文章主要介紹了Android應(yīng)用程序的編譯流程及使用Ant編譯項(xiàng)目的攻略,Ant是集編譯測(cè)試部署于一體的Java自動(dòng)化工具,要的朋友可以參考下2016-04-04
Android中實(shí)現(xiàn)水平滑動(dòng)(橫向滑動(dòng))ListView示例
這篇文章主要介紹了Android中實(shí)現(xiàn)水平滑動(dòng)(橫向滑動(dòng))ListView示例,本文用自己封裝一個(gè)控件的方法解決了這個(gè)需求,需要的朋友可以參考下2015-06-06
Android自定義UI實(shí)現(xiàn)微信語(yǔ)音
這篇文章主要為大家詳細(xì)介紹了Android自定義UI實(shí)現(xiàn)微信語(yǔ)音的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11

