gpt4 book ai didi

android - 像 Snapchat-Discover 这样的 Spannable RecyclerView

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:10:50 26 4
gpt4 key购买 nike

我正在尝试使用 TwoWayView 库创建一个如下图所示的可跨越式 recyclerView:https://github.com/lucasr/twoway-view

但我无法获得所需的 View ,并且遗漏了一些空单元格。

        final SpannableGridLayoutManager.LayoutParams lp = (SpannableGridLayoutManager.LayoutParams) itemView.getLayoutParams();

//final int span1 = (itemId == 0 || itemId == 3 ? 2 : 1);
//final int span2 = (itemId == 0 ? 2 : (itemId == 3 ? 3 : 1));

int span1 = 0; //h
int span2 = 0; //w

switch(itemId)
{
case 0:
span1 = 2;
span2 = 1;
break;

case 1:
span1 = 2;
span2 = 1;
break;

case 2:
span1 = 2;
span2 = 1;
break;

case 3:
span1 = 3;
span2 = 1;
break;

case 4:
span1 = 3;
span2 = 1;
break;

case 5:
span1 = 4;
span2 = 3;
break;

case 6:
span1 = 2;
span2 = 1;
break;

case 7:
span1 = 2;
span2 = 1;
break;

case 8:
span1 = 2;
span2 = 1;
break;

case 9:
span1 = 2;
span2 = 1;
break;

case 10:
span1 = 4;
span2 = 3;
break;
}

final int colSpan = span2;
final int rowSpan = span1;

if (lp.rowSpan != rowSpan || lp.colSpan != colSpan)
{
lp.rowSpan = rowSpan;
lp.colSpan = colSpan;
itemView.setLayoutParams(lp);
}

XML:

<org.lucasr.twowayview.widget.TwoWayView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
style="@style/TwoWayView"
app:twowayview_layoutManager="ListLayoutManager"/>

引用: enter image description here

编辑 1我能够通过将 XML 中的 rowWidth 更改为 12 和以下跨度来解决此问题:

        //PADDING-- left / top / right / bottom
switch(itemId%11)
{
case 0:
span1 = 4;
span2 = 7;
//holder.cont_frienddetail.setPadding(dpAsPixels0, dpAsPixels1, dpAsPixels0, dpAsPixels1);
break;

case 1:
span1 = 4;
span2 = 7;
//holder.cont_frienddetail.setPadding(dpAsPixels2, dpAsPixels1, dpAsPixels2, dpAsPixels1);
break;

case 2:
span1 = 4;
span2 = 7;
//holder.cont_frienddetail.setPadding(dpAsPixels0, dpAsPixels1, dpAsPixels0, dpAsPixels1);
break;

case 3:
span1 = 4;
span2 = 7;
//holder.cont_frienddetail.setPadding(dpAsPixels0, dpAsPixels1, dpAsPixels0, dpAsPixels1);
break;

case 4:
span1 = 8;
span2 = 14;
//holder.cont_frienddetail.setPadding(dpAsPixels2, dpAsPixels1, dpAsPixels0, dpAsPixels1);
break;

case 5:
span1 = 4;
span2 = 7;
//holder.cont_frienddetail.setPadding(dpAsPixels0, dpAsPixels1, dpAsPixels0, dpAsPixels1);
break;

case 6:
span1 = 6;
span2 = 10;
//holder.cont_frienddetail.setPadding(dpAsPixels0, dpAsPixels1, dpAsPixels2, dpAsPixels1);
break;

case 7:
span1 = 6;
span2 = 10;
//holder.cont_frienddetail.setPadding(dpAsPixels0, dpAsPixels1, dpAsPixels0, dpAsPixels1);
break;

case 8:
span1 = 8;
span2 = 14;
//holder.cont_frienddetail.setPadding(dpAsPixels0, dpAsPixels1, dpAsPixels2, dpAsPixels1);
break;

case 9:
span1 = 4;
span2 = 7;
///holder.cont_frienddetail.setPadding(dpAsPixels0, dpAsPixels1, dpAsPixels0, dpAsPixels1);
break;

case 10:
span1 = 4;
span2 = 7;
//holder.cont_frienddetail.setPadding(dpAsPixels0, dpAsPixels1, dpAsPixels0, dpAsPixels1);
break;
}

最佳答案

想象一下,使用您的示例图像并将每个单元格的垂直边界延伸到整个屏幕。如果这样做,您会看到有六列。这就是将解决您的问题的洞察力。

现在您可以使用常规的 RecyclerViewGridLayoutManager 来完成此操作,而不必依赖于修复 GitHub 库中的所有错误。

创建一个包含 6 列的 GridLayoutManager,然后指定一个自定义 SpanSizeLookup,这将使您的单元格具有不同的宽度。

在您的示例图像中,第一行中的三个单元格将分别跨越 2 列。第二行中两个较大的单元格各跨越 3 列。左下方的单元格跨越 4 列。

现在您所要做的就是创建一个 SpanSizeLookup 来设置正确的宽度。

    // Create a grid layout with 6 columns
// (least common multiple of 2 and 3)
GridLayoutManager layoutManager = new GridLayoutManager(this, 6);

layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {

int widthClass = determineWidth(position) // TODO here you figure out what the cell width should be

switch (widthClass) {
case SMALL:
return 2; // fits 3 per row

case MEDIUM:
return 3; // fits 2 per row

case LARGE: // fits 2 per row, one LARGE and one SMALL
return 4;

default:
throw new IllegalStateException("don\'t know about widthClass " + widthClass);
}
}
});

您可以查看我对类似问题的原始回答here .

关于android - 像 Snapchat-Discover 这样的 Spannable RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39195008/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com