1

我有以下使用交错网格布局管理器的回收器视图:

在此处输入图像描述

我想像这样调整交错网格视图的每个项目(如果它是独立的,则最后一个元素全屏显示):

在此处输入图像描述

主.java:

   staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
    superEffectiveRecyclerView.setLayoutManager(staggeredGridLayoutManager);
    effectivenessSuperEffectiveAdapter = new EffectivenessRecyclerAdapter(this, effectivenessesSuperEffective);
    superEffectiveRecyclerView.setItemAnimator(new DefaultItemAnimator());
    superEffectiveRecyclerView.setAdapter(effectivenessSuperEffectiveAdapter);

list_item_type.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">

<TextView
    android:id="@+id/effectiveness_effect"
    android:layout_width="33dp"
    android:layout_height="25dp"
    android:layout_marginTop="4dp"
    android:layout_marginBottom="4dp"
    android:gravity="center"
    android:text="@string/placeholder"
    android:textColor="#f000"
    android:textSize="14sp" />

<TextView
    android:id="@+id/effectiveness_name"
    android:layout_width="81dp"
    android:layout_height="25dp"
    android:layout_marginTop="4dp"
    android:layout_marginBottom="4dp"
    android:gravity="center"
    android:text="@string/placeholder"
    android:textColor="#f000"
    android:textSize="14sp" />
    </LinearLayout>

content_main.xml:

  <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/super_efective_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"  />

我怎样才能做到这一点?

编辑:显然,设置:

     StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
    if(Objects.nonNull(layoutParams)){
        if (getItemCount() % 3 == 1 && position == getItemCount() - 1) {
            layoutParams.setFullSpan(true);
        }
    }

在视图持有人的结果是:

在此处输入图像描述

但是,这不是我想要的,我只想要最后一个元素全屏

4

1 回答 1

1

你可以试试这个

public final void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
    if (size % 2 == 1 && position == size - 1) {
        StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) viewHolder.itemView.getLayoutParams();
        layoutParams.setFullSpan(true);
    }
}

参考在 StaggeredGridLayoutManager 中设置单行的跨度大小

编辑

如果您想在将 layoutManager 设置为 recyclerview 并且 size = 您在适配器中传递的列表的大小时设置此选项,您也可以尝试 gridlayout

 gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            // 3 is the sum of items in one repeated section
            if (size%2==1&&position==size-1)
                return 1;
            else return 2;
            throw new IllegalStateException("internal error");
        }
    });
于 2020-07-23T08:29:04.687 回答