【问题标题】:Should i create custom class for every Recycler View Adapter我应该为每个 Recycler View Adapter 创建自定义类吗
【发布时间】:2019-02-25 05:59:30
【问题描述】:

我是 android 开发新手,经常使用回收站视图。现在回收器的数据来自 JSON 响应中的后端服务器。在我当前的每个回收站视图附近都有不同的数据。所有的回收站视图都有超过 4-5 个数据,例如在一个回收站视图上我有一个名称、价格、数量在另一个回收站上我有通知类型、它的图标等等。因此,直到现在,对于每个回收器视图,我都使用 getter 和 setter 创建了一个模型类,并将该类类型添加到数组列表中并将其发送到适配器。但我看到模型类的数量正在增加。所以我的问题是,有什么方法可以解决一次又一次创建类的问题。我是android开发的新手,所以请把我的概念说清楚

【问题讨论】:

  • 所有适配器的视图持有者都相同吗?

标签: android android-recyclerview


【解决方案1】:

如果您有多个回收器视图,则在同一个类的 xml 中,然后在同一类中创建不同的不同适配器,无需为每个回收器视图创建不同的不同类。

【讨论】:

    【解决方案2】:

    是的,否则您无法绑定数据。

    使用RecyclerView 有以下关键步骤:

     1.Add RecyclerView support library to the gradle build file
    
     2.Define a **model class** to use as the data source
    
     3.Add a RecyclerView to your activity to display the items
    
     4.Create a custom row layout XML file to visualize the item
    
     5.Create a RecyclerView.Adapter and ViewHolder to render the item
    
     6.Bind the adapter to the data source to populate the RecyclerView
    

    每个适配器都有三种主要方法:onCreateViewHolder 膨胀项目布局并创建支架,onBindViewHolder 根据数据设置视图属性,getItemCount 确定项目数量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-04
      • 1970-01-01
      相关资源
      最近更新 更多