List view with custom view items with partially ov

2019-02-10 14:35发布

I want to implement a list in Android that contains some customized views. My problem is that I want the the views will be put one after the other with a little overlap between them. like in the following schema: enter image description here

I also want to control this overlap in such a way that when the user clicks on one of the items, they will move apart from each other.

I tried to extend ListView but it seems to be very obscured, any suggestions?

Edit: This can be more clear:

enter image description here

I did it by setting the divider height to -50dp. this is exactly what I want to achieve, but somehow it doesn't reflect on my app.

2条回答
Juvenile、少年°
2楼-- · 2019-02-10 14:46

This can probably be achieved by using the Camera.setTranslate function. See Android: Vertical ListView with overlaped rows and Android: vertical 3d listview for similar questions (with solutions)

查看更多
Emotional °昔
3楼-- · 2019-02-10 14:50

I managed to achieve this by using scroll view with a single relative layout as a child. I then dynamically place the views by defining a rule and margin:

for (int i = 0; i < 30; i++) {
        TextView tv = new TextView(context);
        tv.setText("Text \n Text" + i);

        tv.setBackgroundColor(i % 2 == 0 ? Color.RED : Color.GREEN);

        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        lp.leftMargin = 0;
        lp.topMargin = (i * 45);
        rl.addView(tv, lp);
}

Later, you can control the positioning of the sub-views by changing their y value (for example: if you want to add animation).

This is the final result:

Layout containing overlapping items

查看更多
登录 后发表回答