Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overlap positions/views in recyclerview

I've been trying to overlap two adjacent positions in a recyclerview, but fail to do so. I would like a part of the first position overlap/draw over the second position.

I've have been playing around with a custom RecyclerView.ItemDecoration and it's getItemOffsets, however that just seem to be able to add padding/margin on a position, not translating it to a new position outside it's viewparent.

Anyone got any pointers of how to achieve this with a recyclerview?

like image 637
Lucas Arrefelt Avatar asked Mar 13 '23 09:03

Lucas Arrefelt


1 Answers

Well, you can also add negative margin in a RecyclerView.ItemDecoration.


Example:

ItemDecorator.java

public class ItemDecorator extends RecyclerView.ItemDecoration {
    private final int mSpace;

    public ItemDecorator(int space) {
        this.mSpace = space;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        outRect.left = mSpace;
        outRect.right = mSpace;
        outRect.bottom = mSpace;
        outRect.top = mSpace;
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("Hi");
        arrayList.add("World");
        arrayList.add("What");

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rec1);
        //Negative margin!
        ItemDecorator itemDecorator = new ItemDecorator(-30);     
        recyclerView.setAdapter(new CustomAdapter(arrayList));
        recyclerView.addItemDecoration(itemDecorator);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

    }
}
like image 100
Evin1_ Avatar answered Mar 23 '23 19:03

Evin1_