带有部分重叠的自定义视图项的列表视图 (Android) [英] List view with custom view items with partially overlaps (Android)

查看:23
本文介绍了带有部分重叠的自定义视图项的列表视图 (Android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Android 中实现一个包含一些自定义视图的列表.我的问题是我希望视图将一个接一个放置,它们之间有一点重叠.类似于以下架构:

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.

我试图扩展 ListView 但它似乎很模糊,有什么建议吗?

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

这可以更清楚:

我通过将分隔线高度设置为 -50dp 来实现的.这正是我想要实现的目标,但不知何故它并没有反映在我的应用中.

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.

推荐答案

我小时候通过使用带有单个相对布局的滚动视图成功地实现了这一点.然后我通过定义规则和边距动态放置视图:

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 
 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);
}

稍后,您可以通过更改它们的 y 值来控制子视图的定位(例如:如果您想添加动画).

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

这是最终结果:

这篇关于带有部分重叠的自定义视图项的列表视图 (Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆