Nexussim Lements
Nexussim Lements

Reputation: 787

Android Staggered Grdiv View last element full screen if standalone

I have the following recycler view with Staggered Grid Layout Manager:

enter image description here

I would like to adapt each item of the staggered grid view like this (the last element full-screen if its stand alone) :

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"  />

How Can I achieve this ?

EDIT : apparently , setting :

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

In view holder results in this :

enter image description here

However , this is not exactly what I want , I only want the last element fullscreen

Upvotes: 2

Views: 890

Answers (1)

Jyotish Biswas
Jyotish Biswas

Reputation: 574

You can try this

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

Reference Setting span size of single row in StaggeredGridLayoutManager

EDIT

You can also try gridlayout if you want set this when you set layoutManager to your recyclerview and size = size of your list which you pass in your adapter

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

Upvotes: 2

Related Questions