Android 领券联盟:记录RecyclerView的使用
生活随笔
收集整理的這篇文章主要介紹了
Android 领券联盟:记录RecyclerView的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
介紹:
此部分截取自領券聯盟,源碼GitHub或者Gitee上面應該有不少,Java和Kotlin都有,我這里采用kotlin
有完整視頻講解以及具體API:點擊此處
內容多有不完整,只是記錄下自己當下的學習內容,并非教程,可以自行去GitHub或者Gitee或者找視頻觀看學習
學到了很多,感謝!
1. 在項目的 build.gradle 中添加依賴
implementation 'androidx.recyclerview:recyclerview:1.2.0'implementation 'com.github.bumptech.glide:glide:4.11.0'implementation 'com.github.bumptech.glide:okhttp3-integration:4.9.0'// 版本可以去官網查看2. 需要兩個布局( 下面給出完整效果圖,下面代碼并不完整 )
一個是RecyclerView布局,作為總體骨架,另一個則是RecyclerView列表中的那些子項
總體骨架命名為:fragment_home_pager.xml(自行命名即可)
<?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"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"tools:ignore="MissingConstraints"android:layout_height="match_parent"><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/rv_home_pager_content_list"android:layout_width="match_parent"android:layout_height="match_parent"/> </androidx.constraintlayout.widget.ConstraintLayout>子項命名為:item_category_page_content.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="100dp"android:background="@color/white"android:gravity="center_vertical"android:orientation="horizontal"><ImageViewandroid:id="@+id/goods_cover"android:layout_width="80dp"android:layout_height="80dp"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:background="@color/black"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><TextViewandroid:id="@+id/goods_title"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginRight="10dp"android:ellipsize="end"android:maxLines="2"android:text="這是標題內容呀..."android:textSize="14sp"android:textStyle="bold" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="5dp"android:layout_marginBottom="5dp"android:orientation="horizontal"><TextViewandroid:id="@+id/off_amount"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@color/deep_red"android:paddingLeft="5dp"android:paddingTop="2dp"android:paddingRight="5dp"android:paddingBottom="2dp"android:text="天貓"android:textColor="@color/white"android:textSize="12sp" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:id="@+id/off_prise"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="239.90"android:textColor="@color/origin"android:textSize="14sp"android:textStyle="bold" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:text="¥"android:textColor="@color/grey"android:textSize="10sp" /><TextViewandroid:id="@+id/origin_prise"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="2349.90"android:textColor="@color/grey"android:textSize="12sp" /><TextViewandroid:id="@+id/bought_count"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="15dp"android:text="2349·"android:textColor="@color/grey"android:textSize="12sp"android:textStyle="bold" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="人已購買"android:textColor="@color/grey"android:textSize="12sp"android:textStyle="bold" /></LinearLayout></LinearLayout></LinearLayout>3. 創建一個實體類
data class CategoryPagerContent(val code: Int,val `data`: List<Data>,val message: String,val success: Boolean ) {data class Data(val category_id: Int,val category_name: Any,val click_url: String,val commission_rate: String,val coupon_amount: Int,val coupon_click_url: String,val coupon_end_time: String,val coupon_info: Any,val coupon_remain_count: Int,val coupon_share_url: String,val coupon_start_fee: String,val coupon_start_time: String,val coupon_total_count: Int,val item_description: String,val item_id: Long,val level_one_category_id: Int,val level_one_category_name: String,val nick: String,val pict_url: String,val seller_id: Long,val shop_title: String,val small_images: SmallImages,val title: String,val user_type: Int,val volume: Int,val zk_final_price: String)data class SmallImages(val string: List<String>) }4. 設置一個RecyclerView適配器
class HomePagerContentAdapter: RecyclerView.Adapter<HomePagerContentAdapter.InnerHolder>() {private val TAG = " HomePagerContentAdapter "// 創建一個集合來保存數據private val contentList: ArrayList<CategoryPagerContent.Data> = ArrayList()/*** 創建RecyclerView列表中的那一個個item* 每創建一個item,都會調用該方法*/override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): InnerHolder {val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item_category_page_content,parent,false)return InnerHolder(itemView)}/*** 實現具體功能 ,比如給控件賦值,控件的點擊事件等*/override fun onBindViewHolder(holder: InnerHolder, position: Int) {// 綁定數據val itemData = contentList[position]holder.bindContent(itemData)logD(TAG, " onBindViewHolder() $itemData")}/*** 通過onCreateViewHolder()方法,獲取子布局實例, 也就是找到子布局的控件* 此方法中的參數itemView就是子項的View,可以通過控件id獲取到該控件實例* 在此方法中可以完成具體功能,也可以在onBindViewHolder()方法中完成*/class InnerHolder(itemView: View): RecyclerView.ViewHolder(itemView) {fun bindContent(itemData: CategoryPagerContent.Data){// 設置信息itemView.findViewById<TextView>(R.id.goods_title).text = itemData.title //商品名稱val offPriseTv = itemView.findViewById<TextView>(R.id.off_prise) // 券后價val offPrise = ((itemData.zk_final_price.toFloat()) - itemData.coupon_amount)offPriseTv.text = "券后價:${String.format("%.2f", offPrise)}" // 券后價val originPriseTv = itemView.findViewById<TextView>(R.id.origin_prise) // 原價originPriseTv.paint.flags = Paint.STRIKE_THRU_TEXT_FLAG // 原價加上刪除線(中劃線)originPriseTv.text = itemData.zk_final_priceval offAmountTv = itemView.findViewById<TextView>(R.id.off_amount) // 共省*元offAmountTv.text = "省${itemData.coupon_amount}元"val boughtCountTv = itemView.findViewById<TextView>(R.id.bought_count) // 購買人數boughtCountTv.text = "${itemData.volume}·"val url = "https://${itemData.pict_url}"val cover = itemView.findViewById<ImageView>(R.id.goods_cover)/*** Glide: 圖片加載庫* .with: 當前上下文對象* .load: 加載圖片,提供多種加載方案* .override: 裁剪圖片* .into: 傳入要展示圖片的控件*/Glide.with(itemView.context).load(url).override(cover.layoutParams.width,cover.layoutParams.height).into(cover)}}// 子項item的數目override fun getItemCount(): Int {return contentList.size}/*** 此處向Activity或者Fragment提供一個方法,讓外部傳入數據* 外部獲取到該適配器的實例就可以調用該方法,傳入數據* 然后將數據傳入List列表中*/fun setData(data: List<CategoryPagerContent.Data>) {contentList.clear() // 首先清空以下保存的數據,用于實時更新新數據contentList.addAll(data)notifyDataSetChanged() // 通知更新}}5. 在Fragment中關聯Adapter適配器,因為源碼中涉及代碼較多,刪除不相關的代碼
主要完成的事情:
1. 獲取到適配器的實例
2. 布局中的RecyclerView適配器與自定義的適配器相關聯,此處便可展現出無數據的列表了
3. 將具體數據傳給自定義的適配器,通過適配器的實例調用類中的方法
總結
以上是生活随笔為你收集整理的Android 领券联盟:记录RecyclerView的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQL server 创建员工表、薪资表
- 下一篇: Java多线程【三种实现方法】