介绍:

此部分截取自领券联盟,源码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. 将具体数据传给自定义的适配器,通过适配器的实例调用类中的方法

class HomePageFragment{private lateinit var contentAdapter: HomePagerContentAdapteroverride fun onViewCreated(view: View, savedInstanceState: Bundle?) {super.onViewCreated(view, savedInstanceState)// 1. 获取到适配器的实例contentAdapter = HomePagerContentAdapter()// 2. 布局中的RecyclerView适配器与自定义的适配器相关联binding.rvHomePagerContentList.adapter = contentAdapter/*** RecyclerView提供了三种布局管理器即:* LinearLayoutManager 线性布局管理器, 横向纵向滑动都行* StaggeredGridLayoutManager 瀑布流布局管理器* GridLayoutManager 网格布局管理器*/binding.rvHomePagerContentList.layoutManager = LinearLayoutManager(context)/*** ItemDecoration只有3个重要的重写方法:* getItemOffsets  用于实现item的上下左右的间距大小* onDraw  在这个方法里绘制的文字、颜色、图形都会比item更低一层,这些绘制效果如果与item重叠,就会被item遮盖* onDrawOver  在这个方法绘制的文字、颜色、图形都会比item更高一层,这些绘制效果始终在最上层,不会被遮盖。**/binding.rvHomePagerContentList.addItemDecoration(object : RecyclerView.ItemDecoration(){override fun getItemOffsets(outRect: Rect, view: View,parent: RecyclerView, state: RecyclerView.State) {// 设置上下间距为10outRect.top = 10outRect.bottom = 10}})// setData() 方法为适配器中的方法,调用,并将数据传入contentAdapter.setData(result.data)}companion object{private const val CATEGORY_TITLE_KEY = "category_title"private const val CATEGORY_ID_KEY = "category_id"fun newInstance(data: MainCategoryItem.Data?): HomePageFragment{val bundle = Bundle()bundle.putString(CATEGORY_TITLE_KEY, data!!.title)bundle.putInt(CATEGORY_ID_KEY, data.id)val fragment = HomePageFragment()// fragment.arguments: 向fragment传参数fragment.arguments = bundlereturn fragment}}}

Android 领券联盟:记录RecyclerView的使用相关推荐

  1. Binary XML file line #8: Error inflating class android.support.v7.widget.RecyclerView

    今天创建了一个 demo ,然后就是复制RecyclerView 过去, 到最后完成运行的时候发现 系统奔溃了, 然后 提示 Binary XML file line #8: Error inflat ...

  2. Android监听左右滑删除通知,Android 滑动监听RecyclerView线性流+左右划删除+上下移动...

    废话不多说了,直接给大家贴代码了.具体代码如下所示: xmlns:tools="http://schemas.android.com/tools" android:layout_w ...

  3. 【Android实战】记录自学自己定义GifView过程,能同一时候支持gif和其它图片!【有用篇】...

    之前写了一篇博客.<[Android实战]记录自学自己定义GifView过程,具体解释属性那些事! [学习篇]> 关于自己定义GifView的,具体解说了学习过程及遇到的一些类的解释,然后 ...

  4. AndroidStudio中提示:Didn‘t find class “android.support.v7.widget.RecyclerView“

    场景 在Android Studio中使用Recycle View时提示: Didn't find class "android.support.v7.widget.RecyclerView ...

  5. android n等分 layout,RecyclerView GridLayoutManager 等分间距

    RecyclerView 表格实现 RecyclerView 配合GridLayoutManager 可以实现类似表格的样式,为了实现均分,adapter 的布局宽度改为匹配父元素,即 android ...

  6. int android.support.v7.widget.RecyclerView$ViewHolder.mItemViewType' on a null.....

    今天在做一个即时通讯回话列表时,遇到int android.support.v7.widget.RecyclerView$ViewHolder.mItemViewType' on a null..空指 ...

  7. Android 第二十一课 RecyclerView简单的应用之编写“精美”的聊天页面

    1.由于我们会使用到RecyclerView,因此首先需要在app/build.gradle当中添加依赖库.如下: apply plugin: 'com.android.application' .. ...

  8. android 日程安排view,RecyclerView 列表控件中简单实现时间线

    时间 时间,时间,时间啊:走慢一点吧- 看见很多软件中都有时间线的东西,貌似天气啊,旅游啊什么的最多了:具体实现方式很多,在本篇文章中讲解一种自定义View封装的方式. 效果 先来看看效果. 分析 软 ...

  9. android.support.v7.widget.,关于android.support.v7.widget.RecyclerView的使用,总是找不到类...

    各位高手求指导决,卡在这了,整了一个上午没搞定. log输出: 06-13 00:42:03.395: E/AndroidRuntime(1884): FATAL EXCEPTION: main 06 ...

最新文章

  1. linux查看文件安全权限,Linux系统下如何查看及修改文件读写权限
  2. 把偷快递的贼炸到怀疑人生!不愧是 NASA 工程师,奇思妙想
  3. VMware Workstation 14 激活码
  4. 全球与中国植物基液压油市场供需预测及未来发展展望报告2022-2028年版
  5. git 忽略文件失效
  6. SAP Spartacus里的feature module
  7. 使用URLRewriter实现URL重写
  8. future.cancel不能关闭线程_彻底弄懂线程池-newFixedThreadPool实现线程池
  9. background-size 兼容ie8以下浏览器的方法
  10. CountdownLatchTest
  11. java 封装返回json数据
  12. 灰度发布 java_灰度发布系统的实现步骤
  13. 高德地图导航和路径规划
  14. DELL win10插入耳机后声音仍然外放(亲测有效)
  15. C# vb .net实现相机视图效果滤镜
  16. Python之判断闰年
  17. 014 Unbox failed! RequestError: Error: getaddrinfo ENOENT raw.githubusercontent.com raw.githubuserco
  18. 正确将图片保存到相册的方法
  19. 优雅编程之这样使用枚举和注解,你就“正常”了(二十九)
  20. MS7024 TV Encode digital数字信号转AV/SV配置说明

热门文章

  1. 个税系统提示服务器不合法,最新金税三期个人所得税系统中申报了个税,但扣缴税时提示没有签订三方协议。...
  2. 【社区图书馆】-《科技服务与价值链》总结
  3. AndroidStudio中Method “xxx” is never used 解决方案
  4. Vue 打包 成 桌面应用 vue打包成桌面应用 vue 打包桌面应用 vue 部署 桌面应用 vue部署为桌面应用 vue部署 为 桌面应用 vue 桌面应用
  5. [生存志] 第46节 秦穆公任贤霸西戎
  6. python二级考试怎么报名_全国计算机二级考什么 怎么报名
  7. 如何利用 DITA 实现高效的跨部门内容共享?
  8. 浙江发布全国首个诉讼服务规范省级标准
  9. 联想G50-70开机不显示,不进bios,不显示Lenovo标志,屏幕背光微亮闪烁,电源亮 对应主板ACLU1/ACLU2 NM-A271 REV-1.0 。完美点亮开机
  10. 13岁女孩被摁在马桶中暴打,面对霸凌,是妥协还是反抗? 警醒 !