0

I currently am developping an Android app in which I use a recyclerView to show the user a list of available services of the app. Right now the list's UI is a simple two-columns, scrollable recyclerView who look like the items are snapped just like in a gridview.

I'd like to offset the second column of the recyclerView so it would look kinda like how a keyboard's keys rows do, not aligning items perfectly.

I've seen another question that asks for the same thing as me (though horizontally and not vertically) but the answers didn't help and I don't want to necromance the thread back to life ^^ RecyclerView - horizontal, two-row grid, second row offset

Does anyone know of a way to do this ? maybe a library exists somewhere but I looked everywhere I could find answers without success...

Edit : Thanks to Bled K. for the solution :D I added this function and called it in the OnBindViewHolder :

public void setViewOffset(int position){
            if(position==1){
                ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) this.f.getLayoutParams();
                params.topMargin = cardView.getHeight()/3;
                this.f.setLayoutParams(params);
            }else{
                ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) this.f.getLayoutParams();
                params.topMargin = 0;
                this.f.setLayoutParams(params);
            }
        }

in my case this.f is the constraintLayout that contains my cardview and all.

1 Answers1

0

I'm going to assume you're using your own Adapter and ViewHolder. Create a method inside your custom ViewHolder say ... void setViewOffset(int position) where you'd set a different left margin to the view if the position is the one you want. Call this method in your adapter's onBindViewHolder(int position) and pass the possition.

Otherwise, a bit of code from your side would allow for more help.

Bled
  • 66
  • 1
  • 5