List view with custom view items with partially overlaps (Android)

后端 未结 2 1570
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-03 08:02

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 betw

相关标签:
2条回答
  • 2021-01-03 08:30

    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

    0 讨论(0)
  • 2021-01-03 08:35

    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)

    0 讨论(0)
提交回复
热议问题