【问题标题】:Different card size不同的卡片大小
【发布时间】:2017-01-03 08:52:04
【问题描述】:

我怎样才能让recycleView 中有不同尺寸的卡片? (1x1、1x2 和 2x1,其中 1 是卡片长度) enter image description here

【问题讨论】:

    标签: android gridview android-recyclerview android-cardview cardview


    【解决方案1】:

    您可以创建两个视图持有者。其中一个持有同一行的两张牌,另一个持有整行的一张。它肯定看起来像您发布的图像。要实现具有多个视图持有者的回收器视图,请查看this

    【讨论】:

      【解决方案2】:

      您可以使用具有不同跨度计数的GridLayoutManager
      这是一些例子。

      活动中:

      //Initialize recyclerView and adapter before
      GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
      recyclerView.setLayoutManager(layoutManager);
      recyclerView.setAdapter(adapter);
      
      layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                  @Override
                  public int getSpanSize(int position) {
                          if (adapter.isHeader(position)) {
                              //Returns span count 2 if method isHeader() returns true. 
                              //You can use your own logic here.
                              return  mLayoutManager.getSpanCount()
                          } else {
                               return 1;
                          }
                      }
                  }
              });
      

      并将此方法添加到您的适配器类中:

      public boolean isHeader(int position) {
              return position == 0;//you can use some other logic in here
          }
      

      【讨论】:

        猜你喜欢
        • 2021-06-13
        • 1970-01-01
        • 2022-01-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-30
        • 2018-09-06
        • 2016-06-17
        相关资源
        最近更新 更多