ListView的行标志翼的GMail [英] ListView row marker ala GMail
问题描述
我感兴趣的创建,其中每一行标记它是在GMail中为3.0+做的方式一个ListView。此创建左和右ListFragment的一个很好的分离。
I am interested in creating a ListView where each row is marked the way it is done in GMail for 3.0+. This creates a nice separation of the left and right ListFragment.
其他的例子包括还对2.3.4的情况,有一个颜色标记是在ListView控件的左谷歌日历。
Other examples include also Google Calendar on 2.3.4 for instance where a color marker is on the left of the ListView.
见这两个列表之间的灰色垂直分隔。一个人怎么achive这样的事情?奖金将是同样的交替宽度,但我想这只是一个小的布局变化。
See the grey vertical divider between the two lists. How does one achive something like this? A bonus would be also the alternating width, but I guess that is only a smaller layout change.
我知道我大概可以做这样的事情在里面插入一个ImageView的,然后用我喜欢的颜色填充,但在我看来,这是一个丑陋的黑客攻击。
I know I could probably do something like inserting an ImageView in there and then fill it with the color I would like but it seems to me that this is an ugly hack.
另一个问题是也如果有一种通用的方法,以两个ListView的片段以某种方式结合的方式的GMail或邮件应用程序做。
Another question would be also if there is a generalized way to combine the two ListView fragments somehow the way the GMail or Mail applications do it.
推荐答案
如果你想要的速度,那么选择我会去的,是使用自定义的视图类(如延长 RelativeLayout的
)的行容器视图,并覆盖 dispatchDraw(帆布油画)
方法。
If you want speed, then the option I would go for is to use a custom View class (e.g. extend RelativeLayout
) for the row container View and override the dispatchDraw(Canvas canvas)
method.
在 dispatchDraw
方法被调用的在查看已制定了自己的内容和之前它绘制其子 - 当你调用孩子们绘制的 super.dispatchDraw
。
The dispatchDraw
method is called after the View has drawn its own contents and before it draws its children - the children are drawn when you call super.dispatchDraw
.
使用该做这样的事情
private boolean mDrawMarker = false;
public void setShouldDrawMarker(boolean drawMarker) {
mDrawMarker = drawMarker;
}
public boolean getShouldDrawMarker() {
return mDrawMarker;
}
@Override
public void dispatchDraw(Canvas canvas) {
// draw the children of our view
super.dispatchDraw(canvas);
// draw our marker on top of the children if needed
if (mDrawMarker) {
// e.g. canvas.drawRect(...) or canvas.drawBitmap(...)
}
}
这样就避免增加任何额外欣赏到层次结构,这意味着你将不会承担任何罚款布局或测量阶段。请记住,再利用油漆
和矩形
的对象,如果绘制一个矩形,而不是创建一个新的每次。同样,如果你使用位图,你应该跨越你查看所有实例共享同一个位图实例,而不是每次(加载从资源,一个新的这样做的不可以的意思是把它们放在静态
字段)
This way you avoid adding any extra views to the hierarchy which means you won't incur any penalty in the layout or measuring phases. Remember to re-use Paint
and Rect
objects if drawing a rectangle rather than creating a new one each time. Similarly if you use a bitmap you should share the same Bitmap instance across all instances of your View rather than loading a new one from your resources each time (this does not mean putting them in static
fields)
有关的项目的缩进,因为在这种情况下,名单似乎并不被重叠的你可以(把我的头顶部):
For the indentation of the items, since in this case the lists don't seem to be overlapping you could (off the top of my head):
- 设置在行容器左边距(不能完全肯定这将工作)
- 裹在该行容器中的
的LinearLayout
,并设置左填充这个(如果上述不工作) - 使用自定义视图类(如果设置左边距不工作)
- 一起去@commonsware建议,并使用两种意见 - 一个在左边的灰色背景颜色和另一个的,与标记的颜色正确的 - 只需要设置在视图左侧可见/走了,如果你想缩进/无压痕
- Set a left margin on the row container (not totally sure this will work)
- Wrap the row container in a
LinearLayout
and set the left padding on this (if the above doesn't work) - Use a custom view class (if setting the left margin doesn't work)
- Go with @commonsware suggestion and use two Views - one on the left with the grey background color and another to the right of that with the marker color - then just set the view on the left to visible/gone if you want indentation/no-indentation
对于在第二个例子中,意见的重叠,我会推迟到@commonsware答案。
As for the overlapping of the Views in the second example, I'll defer to @commonsware answer.
这篇关于ListView的行标志翼的GMail的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!