1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Kotlin RecyclerView实现多布局

Kotlin RecyclerView实现多布局

时间:2019-01-03 21:06:40

相关推荐

Kotlin RecyclerView实现多布局

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./it/u=2387632678,1385604828&fm=26&gp=0.jpg")list1.add("http://img5./it/u=3010433732,2078711647&fm=26&gp=0.jpg")list1.add("http://img3./it/u=3114408890,3227379941&fm=26&gp=0.jpg")list1.add("http://img4./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="/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="/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(PLEX_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("/v3/place/").addConverterFactory(GsonConverterFactory.create()).build()service = retrofit.create<TaKeoutService>(TaKeoutService::class.java!!)}fun getHomeInfo(){var homeInfo = service.getHomeInfo("/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>}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。