首页 > 其他分享 >如何开发直播软件,卡片式界面实现

如何开发直播软件,卡片式界面实现

时间:2022-09-19 14:22:13浏览次数:71  
标签:卡片式 layout dishes parent app 直播 界面 android id

如何开发直播软件,卡片式界面实现

1、添加recyclerview的布局fragment_all_dishes.xml

 


<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_dishes_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="visible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />
 
    <TextView
        android:id="@+id/tv_no_dishes_added_yet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/label_no_dishes_added_yet"
        android:textSize="@dimen/_16sdp"
        android:textStyle="bold"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
 
</FrameLayout>

2、添加recyclerview中条目的布局item_dish_layout.xml

 


<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardCornerRadius="@dimen/_5sdp"
    app:cardElevation="@dimen/_5sdp"
    app:cardUseCompatPadding="true">
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
 
        <ImageView
            android:id="@+id/iv_dish_image"
            android:layout_width="match_parent"
            android:layout_height="@dimen/_120sdp"
            android:layout_margin="@dimen/_1sdp"
            android:contentDescription="@string/image_content_description"
            android:scaleType="fitXY"
            android:src="@mipmap/ic_launcher"/>
 
        <TextView
            android:id="@+id/tv_dish_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/_10sdp"
            android:gravity="center_vertical"
            android:textColor="@color/grey_900"
            android:textSize="@dimen/_16sdp"
            android:textStyle="bold"
            android:text="Dish Title"/>
 
    </LinearLayout>
 
 
</androidx.cardview.widget.CardView>

 

3、创建adapter

 


package com.example.mykotlin1.view.adapters
 
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.mykotlin1.databinding.ItemDishLayoutBinding
import com.example.mykotlin1.model.entities.FavDish
 
class FavDishAdapter(private val fragment : Fragment) : RecyclerView.Adapter<FavDishAdapter.ViewHolder>(){
 
    private var dishes : List<FavDish> = listOf()
 
    class ViewHolder(view:ItemDishLayoutBinding) : RecyclerView.ViewHolder(view.root){
        val ivDishImage = view.ivDishImage
        val tvTitle = view.tvDishTitle
    }
 
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding:ItemDishLayoutBinding = ItemDishLayoutBinding.inflate(
            LayoutInflater.from(fragment.context),
            parent,
            false
        )
        return ViewHolder(binding)
    }
 
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val dish = dishes[position]
        Glide.with(fragment)
            .load(dish.image)
            .into(holder.ivDishImage)
        holder.tvTitle.text = dish.title
    }
 
    override fun getItemCount(): Int {
        return dishes.size
    }
 
    fun dishesList(list : List<FavDish>){
        dishes = list
        notifyDataSetChanged()
    }
}

 

 

4、加载布局并设置数据

 


    mBinding.rvDishesList.layoutManager = GridLayoutManager(requireActivity(),2)
        val favDishAdapter = FavDishAdapter(this@AllDishesFragment)
        mBinding.rvDishesList.adapter = favDishAdapter
 
        mFavDishViewModel.allDishList.observe(viewLifecycleOwner){
                dishes ->
            dishes.let{
                for(item in it){
                    Log.i("Dish Title","${item.id} :: ${item.title}")
                }
 
                if(it.isNotEmpty()){
                    mBinding.rvDishesList.visibility = View.VISIBLE
                    mBinding.tvNoDishesAddedYet.visibility = View.GONE
                    favDishAdapter.dishesList(it)
                }else{
                    mBinding.rvDishesList.visibility = View.GONE
                    mBinding.tvNoDishesAddedYet.visibility = View.VISIBLE
                }
            }
        }

 

以上就是如何开发直播软件,卡片式界面实现, 更多内容欢迎关注之后的文章

 

标签:卡片式,layout,dishes,parent,app,直播,界面,android,id
From: https://www.cnblogs.com/yunbaomengnan/p/16707540.html

相关文章