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

被刻印的时光 ゝ 提交于 2019-11-30 15:57:00

问题


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:

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:

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.


回答1:


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)




回答2:


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:



来源:https://stackoverflow.com/questions/13551501/list-view-with-custom-view-items-with-partially-overlaps-android

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