Koltin44,android基础面试

本文详细介绍了Android开发中的Kotlin基础知识,包括数据绑定、条件展示及RecyclerView适配器的使用。通过实例展示了如何在Adapter中处理不同类型的Item视图,以及点击事件如何跳转到详情Activity并传递数据。同时,文章还涵盖了商品列表的缓存数据读取和商品详情页的缓存检查机制,体现了Android开发中的数据管理和页面跳转逻辑。
摘要由CSDN通过智能技术生成

}

tvCount.text = goodsInfo.count.toString()

if (goodsInfo.count > 0) {

tvCount.visibility = View.VISIBLE

btnMinus.visibility = View.VISIBLE

} else {

tvCount.visibility = View.INVISIBLE

btnMinus.visibility = View.INVISIBLE

}

}

}

override fun getCount(): Int {

return goodsList.size

}

override fun getItem(position: Int): Any {

return goodsList.get(position)

}

override fun getItemId(position: Int): Long {

return position.toLong()

}

override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {

var itemView: View

val goodsItemHolder: GoodsItemHolder

if (convertView == null) {

itemView = LayoutInflater.from(context).inflate(R.layout.item_goods, parent, false)

goodsItemHolder = GoodsItemHolder(itemView)

itemView.tag = goodsItemHolder

} else {

itemView = convertView

goodsItemHolder = convertView.tag as GoodsItemHolder

}

goodsItemHolder.bindData(goodsList.get(position))

return itemView

}

override fun getHeaderView(position: Int, convertView: View?, parent: ViewGroup?): View {

val goodsInfo: GoodsInfo = goodsList.get(position)

val typeName = goodsInfo.typeName

val textView: TextView = LayoutInflater.from(context).inflate(R.layout.item_type_header, parent, false) as TextView

textView.text = typeName

textView.setTextColor(Color.BLACK)

return textView

}

override fun getHeaderId(position: Int): Long {

val goodsInfo: GoodsInfo = goodsList.get(position)

return goodsInfo.typeId.toLong()

}

}

HomeRvAdapter.kt点击跳转到activity以前会先获取缓存的数据

package com.example.takeout.ui.adapter

import android.content.Context

import android.content.Intent

import android.view.View

import android.view.ViewGroup

import android.widget.ImageView

import android.widget.RatingBar

import android.widget.TextView

import androidx.recyclerview.widget.RecyclerView

import com.daimajia.slider.library.SliderLayout

import com.daimajia.slider.library.SliderTypes.TextSliderView

import com.example.takeout.R

import com.example.takeout.model.beans.Seller

import com.example.takeout.ui.activity.BusinessActivity

import com.example.takeout.utils.TakeoutApp

import com.squareup.picasso.Picasso

import org.jetbrains.anko.find

class HomeRvAdapter(val context: Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

//定义常量

companion object {

val TYPE_TITLE = 0

val TYPE_SELLER = 1

}

val host = “http://127.0.0.1:8090/image?name=”

var mDatas: ArrayList = ArrayList()

fun setData(data: ArrayList) {

this.mDatas = data

notifyDataSetChanged()

}

/**

  • 不同position对应不同类型

*/

override fun getItemViewType(position: Int): Int {

if (position == 0) {

return TYPE_TITLE

} else {

return TYPE_SELLER

}

}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

val viewType = getItemViewType(position)

when (viewType) {

TYPE_TITLE -> (holder as TitleHolder).bindData(“我是title----------------------------------------”)

TYPE_SELLER -> (holder as SellerHolder).bindData(mDatas[position - 1])

}

}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {

when (viewType) {

TYPE_TITLE -> return TitleHolder(View.inflate(context, R.layout.item_title, null))

TYPE_SELLER -> return SellerHolder(View.inflate(context, R.layout.item_seller, null))

else -> return TitleHolder(View.inflate(context, R.layout.item_home_common, null))

}

}

override fun getItemCount(): Int {

if (mDatas.size > 0) {

return mDatas.size + 1

} else {

return 0

}

}

//内部类,商家的holder

inner class SellerHolder(item: View) : RecyclerView.ViewHolder(item) {

val tvTitle: TextView

val ivLogo: ImageView

val rbScore: RatingBar

val tvSale: TextView

val tvSendPrice: TextView

val tvDistance: TextView

lateinit var mSeller: Seller

init {

tvTitle = item.find(R.id.tv_title)

ivLogo = item.find(R.id.seller_logo)

rbScore = item.find(R.id.ratingBar)

tvSale = item.find(R.id.tv_home_sale)

tvSendPrice = item.find(R.id.tv_home_send_price)

tvDistance = item.find(R.id.tv_home_distance)

item.setOnClickListener {

val intent: Intent = Intent(context, BusinessActivity::class.java)//去取小明在田老师这家店是否有缓存信息

//逐层读取,判断整个这家店是否有缓存

var hasSelectInfo = false

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值