| | |
| | | - 自定义绑定适配器 |
| | | - 表达式支持 |
| | | |
| | | ### RecyclerView 列表为空时的实现 |
| | | 在 RecyclerView 适配器中,当列表数据为空时,显示一个空视图(EmptyView)的实现方式: |
| | | |
| | | 1. 继承 BaseRecycleAdapter: |
| | | ```kotlin |
| | | class YourAdapter : BaseRecycleAdapter<RecyclerView.ViewHolder>() { |
| | | // 实现必要的方法 |
| | | } |
| | | ``` |
| | | |
| | | 2. 在适配器中定义视图类型常量(已在 BaseRecycleAdapter 中定义): |
| | | ```kotlin |
| | | companion object { |
| | | const val VIEW_TYPE_ITEM = 1 |
| | | const val VIEW_TYPE_EMPTY = 0 |
| | | } |
| | | ``` |
| | | |
| | | 3. 重写 getItemViewType 方法: |
| | | ```kotlin |
| | | override fun getItemViewType(position: Int): Int { |
| | | if (dataList.isEmpty()) { |
| | | return VIEW_TYPE_EMPTY |
| | | } |
| | | return VIEW_TYPE_ITEM |
| | | } |
| | | ``` |
| | | |
| | | 4. 重写 getItemCount 方法: |
| | | ```kotlin |
| | | override fun getItemCount(): Int { |
| | | if (dataList.isEmpty()) { |
| | | return 1 // 返回1表示显示空视图 |
| | | } |
| | | return dataList.size |
| | | } |
| | | ``` |
| | | |
| | | 5. 在 onCreateViewHolder 中处理不同类型的视图: |
| | | ```kotlin |
| | | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { |
| | | if (viewType == VIEW_TYPE_EMPTY) { |
| | | val emptyView: ItemNoMoreBinding = DataBindingUtil.inflate( |
| | | (parent.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater)!!, |
| | | R.layout.item_no_more, |
| | | parent, |
| | | false |
| | | ) |
| | | return ViewHolderEmpty(emptyView) |
| | | } else { |
| | | val binding = ItemListBinding.inflate( |
| | | parent.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater, |
| | | parent, |
| | | false |
| | | ) |
| | | return ItemViewHolder(binding.root) |
| | | } |
| | | } |
| | | ``` |
| | | |
| | | 6. 重写 onBindViewHolder 方法: |
| | | ```kotlin |
| | | override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { |
| | | if (holder is ViewHolderEmpty) { |
| | | // 空视图不需要绑定数据 |
| | | return |
| | | } |
| | | |
| | | // 绑定列表项数据 |
| | | if (holder is ItemViewHolder) { |
| | | val item = dataList[position] |
| | | holder.bind(item) |
| | | } |
| | | } |
| | | ``` |
| | | |
| | | 7. 空视图的布局文件示例(item_no_more.xml): |
| | | ```xml |
| | | <?xml version="1.0" encoding="utf-8"?> |
| | | <layout xmlns:android="http://schemas.android.com/apk/res/android"> |
| | | <LinearLayout |
| | | android:layout_width="match_parent" |
| | | android:layout_height="wrap_content" |
| | | android:gravity="center" |
| | | android:orientation="vertical" |
| | | android:padding="16dp"> |
| | | |
| | | <ImageView |
| | | android:layout_width="48dp" |
| | | android:layout_height="48dp" |
| | | android:src="@drawable/ic_empty" /> |
| | | |
| | | <TextView |
| | | android:layout_width="wrap_content" |
| | | android:layout_height="wrap_content" |
| | | android:layout_marginTop="8dp" |
| | | android:text="暂无数据" |
| | | android:textColor="@color/text_gray" |
| | | android:textSize="14sp" /> |
| | | </LinearLayout> |
| | | </layout> |
| | | ``` |
| | | |
| | | 这种实现方式的优点: |
| | | 1. 统一的空视图处理逻辑 |
| | | 2. 支持自定义空视图样式 |
| | | 3. 不影响列表正常数据的显示 |
| | | 4. 便于维护和扩展 |
| | | |
| | | ## 数据库结构 |
| | | |
| | | ### GeneralLibrary 数据库 |