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

£可爱£侵袭症+ 提交于 2019-11-30 15:26:40
Marc Van Daele

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)

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:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!