Kotlin RecyclerView实现多布局

  • 头布局
  • 底布局
  • Activity
  • 请求数据
  • retrofit2接口
class HomeRvAdapter(val context: Context?) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {var list:ArrayList<Food> = ArrayList()fun setData(data: ArrayList<Food>){list.clear()list.addAll(data)notifyDataSetChanged()}companion object{val TYPE_TITLE=0val TYPE_SELLER=1}override fun getItemViewType(position: Int): Int {if (position==0){return TYPE_TITLE}else{return TYPE_SELLER}}override fun onCreateViewHolder(p0: ViewGroup, viewtype: Int): RecyclerView.ViewHolder {when(viewtype){TYPE_TITLE-> return TitleHolder(View.inflate(context,R.layout.ltem_home_common,null))TYPE_SELLER-> return SellerHolder(LayoutInflater.from(p0.context).inflate(R.layout.ltem_seller,p0,false))else -> return TitleHolder(View.inflate(context,R.layout.ltem_home_common,null))}}override fun getItemCount(): Int {if (list.size>0){return list.size+1}else{return 0}}override fun onBindViewHolder(viewHolder: RecyclerView.ViewHolder, p1: Int) {val viewType=getItemViewType(p1)when(viewType){TYPE_TITLE ->  (viewHolder as TitleHolder).bindData("woshi")TYPE_SELLER->  (viewHolder as SellerHolder).bindData(list[p1-1])}}var list1:MutableList<String> = mutableListOf()inner class TitleHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {val banner:Bannerinit {banner = itemView.findViewById(R.id.banner)}fun bindData(data:String){list1.add("http://img4.imgtn.bdimg.com/it/u=2387632678,1385604828&fm=26&gp=0.jpg")list1.add("http://img5.imgtn.bdimg.com/it/u=3010433732,2078711647&fm=26&gp=0.jpg")list1.add("http://img3.imgtn.bdimg.com/it/u=3114408890,3227379941&fm=26&gp=0.jpg")list1.add("http://img4.imgtn.bdimg.com/it/u=3398248196,2128220394&fm=26&gp=0.jpg")banner.setImages(list1)banner.setIndicatorGravity(BannerConfig.CIRCLE_INDICATOR)banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR)banner.setImageLoader(object:ImageLoader(){override fun displayImage(context: Context?, path: Any?, imageView: ImageView?) {Glide.with(context).load(path).into(imageView)}})banner.setBannerAnimation(Transformer.Accordion)banner.setDelayTime(3000)banner.isAutoPlay(true)banner.start()}}inner class SellerHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {var seller_logo:ImageView//头像var tv_title:TextView//店名var tv_home_distance:TextView //地址var ratingBar:RatingBarvar tv_ratingBar:TextViewinit {seller_logo =itemView.findViewById(R.id.seller_logo)tv_title = itemView.findViewById(R.id.tv_title)tv_home_distance = itemView.findViewById(R.id.tv_home_distance)ratingBar = itemView.findViewById(R.id.ratingBar)tv_ratingBar = itemView.findViewById(R.id.tv_ratingBar)}fun bindData(data: Food){tv_title.text=data.nametv_home_distance.text = data.addressGlide.with(context).load(data.pic).into(seller_logo)ratingBar.rating=data.rating.toFloat()tv_ratingBar.text=data.rating}}
}

头布局

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"
><com.youth.banner.Bannerandroid:id="@+id/banner"android:layout_width="match_parent"android:layout_height="200dp"></com.youth.banner.Banner><LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:orientation="horizontal"><RadioGroupandroid:layout_width="wrap_content" android:layout_height="wrap_content"android:layout_weight="1.0"android:gravity="center"android:orientation="vertical"><RadioButtonandroid:textAlignment="center"android:drawableTop="@drawable/cangyin"android:button="@null"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:layout_marginTop="5dp"android:layout_marginBottom="5dp"android:text="餐饮"/><RadioButtonandroid:drawableTop="@drawable/pingpai"android:textAlignment="center"android:button="@null"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:layout_marginTop="5dp"android:layout_marginBottom="5dp"android:text="品牌馆"/></RadioGroup><RadioGroupandroid:layout_width="wrap_content" android:layout_height="wrap_content"android:layout_weight="1.0"android:gravity="center"android:orientation="vertical"><RadioButtonandroid:textAlignment="center"android:drawableTop="@drawable/dingdan"android:button="@null"android:layout_width="70dp"android:layout_height="wrap_content"android:layout_weight="1"android:layout_marginTop="5dp"android:layout_marginBottom="5dp"android:text="即时达"/><RadioButtonandroid:drawableTop="@drawable/youhuijuan"android:textAlignment="center"android:button="@null"android:layout_width="70dp"android:layout_height="wrap_content"android:layout_weight="1"android:layout_marginTop="5dp"android:layout_marginBottom="5dp"android:text="领劵"/></RadioGroup><RadioGroupandroid:layout_width="wrap_content" android:layout_height="wrap_content"android:layout_weight="1.0"android:gravity="center"android:orientation="vertical"><RadioButtonandroid:textAlignment="center"android:drawableTop="@drawable/xiaochi"android:button="@null"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:layout_marginTop="5dp"android:layout_marginBottom="5dp"android:text="特色小吃"/><RadioButtonandroid:drawableTop="@drawable/xiawucha"android:textAlignment="center"android:button="@null"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:layout_marginTop="5dp"android:layout_marginBottom="5dp"android:text="下午茶"/></RadioGroup><RadioGroupandroid:layout_width="wrap_content" android:layout_height="wrap_content"android:layout_weight="1.0"android:gravity="center"android:orientation="vertical"><RadioButtonandroid:textAlignment="center"android:drawableTop="@drawable/chifan"android:button="@null"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:layout_marginTop="5dp"android:layout_marginBottom="5dp"android:text="夜宵"/><RadioButtonandroid:drawableTop="@drawable/dangao"android:textAlignment="center"android:button="@null"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:layout_marginTop="5dp"android:layout_marginBottom="5dp"android:text="鲜花蛋糕"/></RadioGroup></LinearLayout><View android:layout_width="match_parent" android:layout_height="4dp" android:background="#F0F0F0"/><LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:orientation="horizontal"><ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:background="@drawable/shangjia"/><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:text="附近商家"android:layout_marginLeft="5dp"/></LinearLayout></LinearLayout>

底布局

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:background="#fff"android:orientation="horizontal"android:layout_height="wrap_content"><FrameLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"><ImageViewandroid:id="@+id/seller_logo"android:layout_width="90dp"android:layout_height="90dp"android:layout_margin="8dp"/><TextViewandroid:background="@drawable/circle_red"android:id="@+id/tvCount"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="right"android:gravity="center"android:text="1"android:textColor="#fff"android:textSize="12sp"android:visibility="gone"/></FrameLayout><LinearLayout android:layout_margin="8dp" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"><LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"><TextViewandroid:background="@drawable/shap_backgroud_oeder"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:paddingLeft="4dp"android:paddingRight="4dp"android:text="品牌"android:textColor="@android:color/black"/><TextViewandroid:id="@+id/tv_title"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:layout_weight="1"android:singleLine="true"android:text="肯德基宅急送(文化路店)"android:textColor="#000"android:textSize="24sp"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:paddingRight="2dp"android:paddingLeft="2dp"android:text="票"/></LinearLayout><LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:gravity="center_vertical"><RatingBarandroid:id="@+id/ratingBar"style="?android:attr/ratingBarStyleSmall"android:layout_width="wrap_content"android:layout_height="wrap_content"android:rating="2.8"/><TextViewandroid:id="@+id/tv_ratingBar"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:layout_marginRight="5dp"android:text="2.8"android:textColor="#FFDE3D"android:textSize="12sp"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/tv_home_sale"android:text="月售243单"android:textSize="12sp"/></LinearLayout><LinearLayoutandroid:gravity="center_vertical"android:layout_width="match_parent"android:layout_height="wrap_content"><TextViewandroid:layout_weight="1"android:id="@+id/tv_home_send_price"android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="left"android:text="¥20元起送/配送费¥9元"android:textSize="12sp"/><TextViewandroid:maxEms="13"android:layout_gravity="right"android:singleLine="true"android:id="@+id/tv_home_distance"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="556米/33分钟"android:textSize="12sp"/></LinearLayout><View android:layout_width="match_parent" android:layout_height="2dp"android:layout_marginBottom="5dp"android:layout_marginTop="5dp"/><LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:gravity="center_vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:paddingLeft="2dp"android:paddingRight="2dp"android:background="@drawable/shap_backgroud_oeder"android:text="减"android:textColor="#fff"/><TextView android:layout_width="match_parent" android:layout_height="wrap_content"android:text="在线支付满49元减10元,满89元减16元"android:textSize="12sp"/></LinearLayout><LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:gravity="center_vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:paddingLeft="2dp"android:paddingRight="2dp"android:background="@drawable/shap_backgroud_oeder"android:text="增"android:textColor="#fff"/><TextView android:layout_width="match_parent" android:layout_height="wrap_content"android:text="满45元赠九珍珠果汁饮料"android:textSize="12sp"/></LinearLayout></LinearLayout></LinearLayout>

Activity

class HomeFragment :Fragment(){lateinit var homeRvAdapter: HomeRvAdaptervar list:ArrayList<Food> = ArrayList()var sum:Int =0var distance:Int = 0var  alpha = 55lateinit var homeFragmentPersenter:HomeFragmentPersenteroverride fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {val view:View = inflater.inflate(R.layout.homefragment,container,false)return view}override fun onViewCreated(view: View, savedInstanceState: Bundle?) {super.onViewCreated(view, savedInstanceState)var linearLayoutManager=LinearLayoutManager(context)rv_home.layoutManager = linearLayoutManagerhomeRvAdapter = HomeRvAdapter(context)rv_home.adapter = homeRvAdapterhomeFragmentPersenter = HomeFragmentPersenter(this)}override fun onActivityCreated(savedInstanceState: Bundle?) {super.onActivityCreated(savedInstanceState)initData()}fun Int.dp2px():Int{return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,toFloat(),resources.displayMetrics).toInt()}private fun initData() {homeFragmentPersenter.getHomeInfo()distance= 120.dp2px()//有数据rv_home.setOnScrollListener(object :RecyclerView.OnScrollListener(){override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {super.onScrollStateChanged(recyclerView, newState)}override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {super.onScrolled(recyclerView, dx, dy)sum+=dyif (sum > distance){alpha =225}else{alpha = sum*200/distancealpha +=55}ll_title_container.setBackgroundColor(Color.argb(alpha,0x31,0x90,0xe8))}})}//成功fun onHomeSuccess(address: String, url: String, name: String, rating: String) {var food: Food = Food(name,address,url,rating)list.add(food)homeRvAdapter.setData(list)homeRvAdapter.notifyDataSetChanged()}//失败fun onHomeFailed() {}
}

请求数据

class HomeFragmentPersenter(val homeFragment: HomeFragment) {val service:TaKeoutServiceinit {val retrofit = Retrofit.Builder().baseUrl("http://restapi.amap.com/v3/place/").addConverterFactory(GsonConverterFactory.create()).build()service = retrofit.create<TaKeoutService>(TaKeoutService::class.java!!)}fun getHomeInfo(){var homeInfo = service.getHomeInfo("http://restapi.amap.com/v3/place/text?key=0e96c9281f28f99c7621464bf20a7e6f&keywords=%E7%BE%8E%E9%A3%9F&city=%E5%8C%97%E4%BA%AC&extensions=all")homeInfo.enqueue(object :Callback<ResponseBody>{override fun onFailure(call: Call<ResponseBody>?, t: Throwable?) {Log.e("#######食物",""+t?.message)}override fun onResponse(call: Call<ResponseBody>?, response: Response<ResponseBody>?) {val string = response?.body()?.string()perserjson(string)}})}private fun perserjson(string: String?) {var jsonObject:JSONObject = JSONObject(string)var jsonArray = jsonObject.getJSONArray("pois")for (i in 0 until jsonArray.length()){var jsonObject1 = jsonArray.getJSONObject(i)var address = jsonObject1.getString("address")val jsonArray1 = jsonObject1.getJSONArray("photos")val jsonObject3 = jsonObject1.getJSONObject("biz_ext")val rating = jsonObject3.getString("rating")val jsonObject2 = jsonArray1.getJSONObject(0)var url = jsonObject2.getString("url")val name = jsonObject1.getString("name")//有数据成功homeFragment.onHomeSuccess(address,url,name,rating)}}}

retrofit2接口

interface TaKeoutService {@GETfun getHomeInfo(@Url string: String):Call<ResponseBody>
}

Kotlin RecyclerView实现多布局相关推荐

  1. android开发 RecyclerView 瀑布列表布局

    android开发 RecyclerView 瀑布列表布局 1.写一个内容的自定义小布局: <?xml version="1.0" encoding="utf-8& ...

  2. 优雅的为RecyclerView添加头尾布局HeaderAndFooterWrapper

    日常的开发中,我们需要为RecyclerView添加头尾布局,当我们已经写好了一个adapter,这时候如果要现在添加头尾布局,通常的做法是重写刚写完的adapter,再getItemViewType ...

  3. Android开发之LayoutInflater.from(context).inflate()方法参数介绍解决RecyclerView加载布局不全的问题

    咱们先看下item的xml布局高度为64dp <?xml version="1.0" encoding="utf-8"?> <Relative ...

  4. Android 自动动画布局更新 使用,在RecyclerView上使用布局动画(Layout animation)

    自从Material Design发布以来,我对一些视频中演示的网格铺开动画感到惊讶.这是一种斜对角线动画,让activity从上到下从左到右铺开.非常漂亮. 我一直试图尝试所有能得到那种效果的方法. ...

  5. Recyclerview 显示多种布局 demo

    效果图 第一种布局文件 item_title <?xml version="1.0" encoding="utf-8"?> <layout x ...

  6. android 5.0+原生质设计,cardview、Recyclerview、百分比布局PercentRelativeLayout简单使用

    Android 5.0出来已经快一年时间了,全新的系统采用Material Design设计,耳目一新的感觉,当然新系统的出来也增加了新的API为开发带来便捷,现在就说说新出的几个控件,cardvie ...

  7. 解决:RecyclerView 滑动后布局错乱

    一.问题说明 我的需求是要做一个类似这样的列表,整体是一个 RecyclerView,每个 item 内部会有一个流式布局,在流式布局内部会动态添加 TextView,TextView 的大小是自适应 ...

  8. RecyclerView实现多种布局样式

    1.添加依赖 compile 'com.android.support:recyclerview-v7:25.3.0' 2.布局 <android.support.v7.widget.Recyc ...

  9. RecyclerView流式布局

    最近在做需求的时候,碰到有各种筛选项的界面,类似这样 这种筛选界面也比较常见,一般我们都采用RecyclerView来实现,当筛选项比较少的时候,我们选择使用线性布局来实现,当筛选项比较多,但是每个筛 ...

最新文章

  1. 2020-08-23 W7电脑锁屏后,能让电脑处于运行状态吗?
  2. vs报错 已经在 helpers.obj 中定义
  3. VS2010 加入OpenCV 提示 0xc000007b 的错误
  4. Unity-Animator深入系列---控制IK
  5. java面试题十一 基本数据类型
  6. mfc大观之一(全局对象)
  7. 加入了开源组织datawhale
  8. linux 查找html文件,linux查找文件内容包含
  9. (Android-RTC-8)分析HardwareVideoEncoder—BitrateAdjuster
  10. 软件质量保障之代码走查
  11. f分布表完整图a=0.01_SQL数据库完整性
  12. 2018-10-8-如何安装-btsync
  13. GAN的量化评估方法——IS和FID,及其pytorch代码
  14. console接口是干嘛的(console接口是干嘛的电信室外光猫)
  15. K8S—pv和pvs
  16. 快钱 支付 php,GitHub - laraveler/omnipay-99bill: 基于Omnipay的快钱支付SDK
  17. WACV 2021 论文大盘点-人体动作检测与识别篇
  18. 3D卷积的GEMM+IM2COL实现
  19. 8ms入门教程| 安利!开发者用好这个嵌入式开发平台,准点下班不是梦!
  20. 3款国产软件,让你工作效率翻倍,白嫖党的最爱,还不来看

热门文章

  1. android 修改 dpi_魅族 16s|魔趣100|安卓10.0|10.10定制|归属地|机型修改|性能调整|稳定流畅...
  2. c语言程序表白实例,C语言实现520表白代码 祝你表白成功!
  3. 摩根大通认为三支上市公司股票将受益于区块链技术
  4. 一篇文章带你了解,什么是物联网技术?
  5. Springboot简单练手的记账本
  6. 【数据分析Tushare】金融数据处理
  7. 动静新闻显示连接服务器失败是怎么回事,许嵩终于记起微博密码,5个月没动静的他,6个字让服务器瘫了...
  8. 名人经典语录:关于爱情
  9. 华为麒麟659和麒麟710处理器参数比较
  10. 高中数学——函数(1)