Koltin35,搭建android开发环境实验

package com.example.takeout.ui.fragment

import android.os.Bundle

import android.view.LayoutInflater

import android.view.View

import android.view.ViewGroup

import androidx.fragment.app.Fragment

import androidx.recyclerview.widget.LinearLayoutManager

import androidx.recyclerview.widget.RecyclerView

import com.example.takeout.R

import com.example.takeout.model.beans.GoodsTypeInfo

import com.example.takeout.presenter.GoodsFragmentPresenter

import com.example.takeout.ui.adapter.GoodsTypeRvAdapter

import org.jetbrains.anko.find

/**

  • 详情页商品列表界面

*/

class GoodsFragment : Fragment() {

lateinit var rvGoodsType: RecyclerView

lateinit var goodsFragmentPresenter: GoodsFragmentPresenter

lateinit var goodsTypeAdapter :GoodsTypeRvAdapter

override fun onCreateView(

inflater: LayoutInflater,

container: ViewGroup?,

savedInstanceState: Bundle?

): View? {

val goodsView =

LayoutInflater.from(activity).inflate(R.layout.fragment_goods, container, false)

rvGoodsType = goodsView.find(R.id.rv_goods_type)

rvGoodsType.layoutManager = LinearLayoutManager(activity)

goodsTypeAdapter = GoodsTypeRvAdapter(activity, this)

rvGoodsType.adapter = goodsTypeAdapter

goodsFragmentPresenter = GoodsFragmentPresenter(this)

return goodsView

}

override fun onActivityCreated(savedInstanceState: Bundle?) {

super.onActivityCreated(savedInstanceState)

goodsFragmentPresenter.getBusinessInfo()

}

fun onLoadBusinessSuccess(goodstypeList: List<GoodsTypeIn

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

浏览器打开:qq.cn.hn/FTe 开源分享

fo>) {

goodsTypeAdapter.setDatas(goodstypeList) //左侧列表

goodsTypeAdapter.notifyDataSetChanged()

}

}

GoodsFragmentPresenter.kt数据请求的

package com.example.takeout.presenter

import android.util.Log

import com.example.takeout.model.beans.GoodsTypeInfo

import com.example.takeout.ui.fragment.GoodsFragment

import com.google.gson.Gson

import com.google.gson.reflect.TypeToken

import org.json.JSONObject

class GoodsFragmentPresenter(val goodsFragment: GoodsFragment) : NetPresenter() {

var goodstypeList: List = arrayListOf()

//连接服务器拿到此商家所有商品

fun getBusinessInfo() {

val businessCall = takeoutService.getBusinessInfo()

businessCall.enqueue(callback)

}

override fun parserJson(json: String) {

val gson = Gson()

val jsoObj = JSONObject(json)

val allStr = jsoObj.getString(“list”)

//商品类型的集合

goodstypeList = gson.fromJson(allStr, object : TypeToken<List>() {

}.type)

Log.e(“business”, “该商家一共有” + goodstypeList.size + “个类别商品”)

goodsFragment.onLoadBusinessSuccess(goodstypeList)

}

}

GoodsTypeInfo.kt商品的bean

package com.example.takeout.model.beans

class GoodsTypeInfo {

var id: Int = 0//商品类型id

var name: String = “”//商品类型名称

var info: String = “”//特价信息

var list: List = listOf()//商品列表

constructor() : super() {}

constructor(id: Int, name: String, info: String, list: List) : super() {

this.id = id

this.name = name

this.info = info

this.list = list

}

override fun toString(): String {

return “GoodsTypeInfo [id= i d , n a m e = id, name= id,name=name, info= i n f o , l i s t = info, list= info,list=list]”

}

var redDotCount: Int = 0

}

GoodsTypeRvAdapter.kt商品左侧主界面的RecycleView,其中使用FragmentActivity?来作为androidx下面的Context

package com.example.takeout.ui.adapter

import android.view.LayoutInflater

import android.view.View

import android.view.ViewGroup

import android.widget.TextView

import androidx.fragment.app.FragmentActivity

import androidx.recyclerview.widget.RecyclerView

import com.example.takeout.R

import com.example.takeout.model.beans.GoodsTypeInfo

import com.example.takeout.ui.fragment.GoodsFragment

import org.jetbrains.anko.find

class GoodsTypeRvAdapter(val context: FragmentActivity?, val goodsFragment: GoodsFragment) :

RecyclerView.Adapter<RecyclerView.ViewHolder>() {

var goodsTypeList: List = listOf()

fun setDatas(list: List) {

this.goodsTypeList = list

notifyDataSetChanged()

}

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

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

return GoodsTypeItemHolder(itemView)

}

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

val goodsTypeItemHolder = holder as GoodsTypeItemHolder

goodsTypeItemHolder.bindData(goodsTypeList.get(position), position)

}

override fun getItemCount(): Int {

return goodsTypeList.size

}

inner class GoodsTypeItemHolder(val item: View) : RecyclerView.ViewHolder(item) {

val tvType: TextView

init {

tvType = item.find(R.id.type)

}

fun bindData(goodsTypeInfo: GoodsTypeInfo, position: Int) {

tvType.text = goodsTypeInfo.name

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值