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

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

1、添加recyclerview的布局fragment_all_dishes.xml

 

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

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

 

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

 

3、创建adapter

 

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

 

 

4、加载布局并设置数据

 

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

 

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

 

posted @   云豹科技-苏凌霄  阅读(79)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示