Android 交错 Grdiv 全屏查看最后一个元素(如果独立)

标签 android android-recyclerview staggered-gridview staggeredgridlayoutmanager

我有以下带有交错网格布局管理器的回收器 View :

enter image description here

我想像这样调整交错 GridView 的每个项目(如果最后一个元素是独立的,则为全屏):

enter image description here

Main.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);
        }
    }

在 View 持有者中结果如下:

enter image description here

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

最佳答案

你可以试试这个

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);
    }
}

引用Setting span size of single row in StaggeredGridLayoutManager

编辑

如果你愿意,你也可以尝试gridlayout 当你将layoutManager设置为你的recyclerview时设置这个 size = 您在适配器中传递的列表的大小

 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");
        }
    });

关于Android 交错 Grdiv 全屏查看最后一个元素(如果独立),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63049487/

相关文章:

android - 向上滚动时 StaggeredGridLayout 困惑

java - 单击过滤后的 RecyclerView 项目时显示错误数据

java - RecyclerView 未附加到 Adapter

android - log.d 不适用于 Android 服务(Delphi Berlin)

android - 在 Android 应用程序中放入一些 JSON 以防止其进行逆向工程的最佳方法是什么?

RecyclerView 中的 Android 调色板

android - 带有 StaggeredGridLayoutManager 的 RecyclerView,图像从 Glide 加载

android - 单个 StaggeredGridView 中有多个列?

java - 如何在 android EditText 中实现撤消/重做并保留方向更改的值?

java - 如何在 .java 代码中启用禁用的(在 xml 中)ListPreference 对象?