可检查视图中的 setChecked() 方法每次扰乱布局时都会触发两次
我当时正在 Cal Poly lab3 (http://sites.google.com/site/androidappcourse/labs/lab-3) 上工作,一切都按预期进行,直到我在扩展和折叠项目上实现了可检查的界面。然后,我的逻辑似乎是正确的(因为展开按钮文本仍在按其应有的方式变化) - 但布局没有正确重绘 - 就像它在应该展开时折叠起来并且看起来已损坏一样。
在玩了一下日志记录后,我发现每次我单击任何项目时,setChecked() 方法都会在很短的时间跨度内对每个项目调用两次 - 这会扰乱绘图。第一次使用旧的选择数据 - 然后使用新的选择数据。
更奇怪的是,我设法通过设置 setChecked() 每隔一段时间触发来解决这个问题:
public void setChecked(boolean checked)
{
counter++;
if (counter%2==0)
{
if(checked) expandJokeView();
else collapseJokeView();
}
Log.d("SimpleJokeListLog", "SetChecked"+" "+checked+" "+counter+" "+m_joke.toString());
}
这是它在目录中的样子: 我开始活动,在我的自定义 JokeView 中显示项目列表,实现可检查,
12-18 02:58:16.598: D/SimpleJokeListLog(742): SetChecked false 1 A small
12-18 02:58:16.628: D/SimpleJokeListLog(742): SetChecked false 1 Cartoonist
12-18 02:58:16.648: D/SimpleJokeListLog(742): SetChecked false 1 I wonde
12-18 02:58:16.728: D/SimpleJokeListLog(742): SetChecked false 2 A small
12-18 02:58:16.748: D/SimpleJokeListLog(742): SetChecked false 2 Cartoonist
12-18 02:58:16.748: D/SimpleJokeListLog(742): SetChecked false 2 I wondere
我选择第一个项目(一个小...),
12-18 02:59:17.628: D/SimpleJokeListLog(742): SetChecked false 3 A small
12-18 02:59:17.628: D/SimpleJokeListLog(742): SetChecked false 3 Cartoonis
12-18 02:59:17.628: D/SimpleJokeListLog(742): SetChecked false 3 I wondere
12-18 02:59:17.838: D/SimpleJokeListLog(742): SetChecked true 4 A small boy
12-18 02:59:17.847: D/SimpleJokeListLog(742): SetChecked false 4 Cartoonist
12-18 02:59:17.858: D/SimpleJokeListLog(742): SetChecked false 4 I wondered
我选择第二个
12-18 02:59:24.768: D/SimpleJokeListLog(742): SetChecked true 5 A small boy
12-18 02:59:24.768: D/SimpleJokeListLog(742): SetChecked false 5 Cartoonist
12-18 02:59:24.768: D/SimpleJokeListLog(742): SetChecked false 5 I wondered w
12-18 02:59:24.968: D/SimpleJokeListLog(742): SetChecked false 6 A small
12-18 02:59:24.968: D/SimpleJokeListLog(742): SetChecked true 6 Cartooni
12-18 02:59:24.978: D/SimpleJokeListLog(742): SetChecked false 6 I wonde
,第三个
12-18 02:59:27.828: D/SimpleJokeListLog(742): SetChecked false 7 A small
12-18 02:59:27.838: D/SimpleJokeListLog(742): SetChecked true 7 Cartoonis
12-18 02:59:27.838: D/SimpleJokeListLog(742): SetChecked false 7 I wonder
12-18 02:59:28.048: D/SimpleJokeListLog(742): SetChecked false 8 A small
12-18 02:59:28.058: D/SimpleJokeListLog(742): SetChecked false 8 Cartooni
12-18 02:59:28.068: D/SimpleJokeListLog(742): SetChecked true 8 I wondered
,再次第三个(即使这次没有这个计数器,它也无法正确绘制):
12-18 02:59:31.197: D/SimpleJokeListLog(742): SetChecked false 9 A small b
12-18 02:59:31.197: D/SimpleJokeListLog(742): SetChecked false 9 Cartoonist
12-18 02:59:31.197: D/SimpleJokeListLog(742): SetChecked true 9 I wondered
12-18 02:59:31.417: D/SimpleJokeListLog(742): SetChecked false 10 A small bo
12-18 02:59:31.417: D/SimpleJokeListLog(742): SetChecked false 10 Cartoonist f
12-18 02:59:31.427: D/SimpleJokeListLog(742): SetChecked true 10 I wondered why
I was working on Cal Poly lab3 (http://sites.google.com/site/androidappcourse/labs/lab-3) and everything worked as it should until I came with implementing checkable interface on expanding and collapsing items. Then it seamed that my logic was right (because the expand button text was still changing as it should) - but the layout wasn't redrawing properly - like it was collapsed when it should been expanded and looked broken.
After playing a little bit with logging I found out that every time i click on any item setChecked() method is called two times on every item in very short time-span - which messes with the drawing. First time with the old selection data - and then with the new one.
What is even more curious that I managed to kind of fix the problem by setting that setChecked() only fires every other time:
public void setChecked(boolean checked)
{
counter++;
if (counter%2==0)
{
if(checked) expandJokeView();
else collapseJokeView();
}
Log.d("SimpleJokeListLog", "SetChecked"+" "+checked+" "+counter+" "+m_joke.toString());
}
And here is how it looks like in catlog:
I start activity that displays list with items in my custom JokeView implementing checkable
12-18 02:58:16.598: D/SimpleJokeListLog(742): SetChecked false 1 A small
12-18 02:58:16.628: D/SimpleJokeListLog(742): SetChecked false 1 Cartoonist
12-18 02:58:16.648: D/SimpleJokeListLog(742): SetChecked false 1 I wonde
12-18 02:58:16.728: D/SimpleJokeListLog(742): SetChecked false 2 A small
12-18 02:58:16.748: D/SimpleJokeListLog(742): SetChecked false 2 Cartoonist
12-18 02:58:16.748: D/SimpleJokeListLog(742): SetChecked false 2 I wondere
I select first item (A small...)
12-18 02:59:17.628: D/SimpleJokeListLog(742): SetChecked false 3 A small
12-18 02:59:17.628: D/SimpleJokeListLog(742): SetChecked false 3 Cartoonis
12-18 02:59:17.628: D/SimpleJokeListLog(742): SetChecked false 3 I wondere
12-18 02:59:17.838: D/SimpleJokeListLog(742): SetChecked true 4 A small boy
12-18 02:59:17.847: D/SimpleJokeListLog(742): SetChecked false 4 Cartoonist
12-18 02:59:17.858: D/SimpleJokeListLog(742): SetChecked false 4 I wondered
I select second one
12-18 02:59:24.768: D/SimpleJokeListLog(742): SetChecked true 5 A small boy
12-18 02:59:24.768: D/SimpleJokeListLog(742): SetChecked false 5 Cartoonist
12-18 02:59:24.768: D/SimpleJokeListLog(742): SetChecked false 5 I wondered w
12-18 02:59:24.968: D/SimpleJokeListLog(742): SetChecked false 6 A small
12-18 02:59:24.968: D/SimpleJokeListLog(742): SetChecked true 6 Cartooni
12-18 02:59:24.978: D/SimpleJokeListLog(742): SetChecked false 6 I wonde
Third one
12-18 02:59:27.828: D/SimpleJokeListLog(742): SetChecked false 7 A small
12-18 02:59:27.838: D/SimpleJokeListLog(742): SetChecked true 7 Cartoonis
12-18 02:59:27.838: D/SimpleJokeListLog(742): SetChecked false 7 I wonder
12-18 02:59:28.048: D/SimpleJokeListLog(742): SetChecked false 8 A small
12-18 02:59:28.058: D/SimpleJokeListLog(742): SetChecked false 8 Cartooni
12-18 02:59:28.068: D/SimpleJokeListLog(742): SetChecked true 8 I wondered
Again third one (even this time without this counter thingy it doesn't draw properly):
12-18 02:59:31.197: D/SimpleJokeListLog(742): SetChecked false 9 A small b
12-18 02:59:31.197: D/SimpleJokeListLog(742): SetChecked false 9 Cartoonist
12-18 02:59:31.197: D/SimpleJokeListLog(742): SetChecked true 9 I wondered
12-18 02:59:31.417: D/SimpleJokeListLog(742): SetChecked false 10 A small bo
12-18 02:59:31.417: D/SimpleJokeListLog(742): SetChecked false 10 Cartoonist f
12-18 02:59:31.427: D/SimpleJokeListLog(742): SetChecked true 10 I wondered why
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在这个问题上被困了三四个小时,最后我尝试从扩展和折叠方法中删除 setChecked() 调用(不记得为什么我把它们放在那里......)。也许它对你有帮助,这是我的代码片段:
现在,一切都完美地工作:没有多次调用 setChecked,并且 ListItems 像他们应该的那样打开和关闭。
I m stuck on this promlem last 3 or 4 hours, at the end i tried remove setChecked() calls from extend and collapse methods (don`t remember why i placed them there...). Maybe it helps you, here is my snippet code:
Now, all work perfectly: no multiple calls of setChecked, and ListItems open and close like they should.