我在加州理工学院lab3 (http://sites.google.com/site/androidappcourse/labs/lab-3)工作,一切都正常工作,直到我实现了扩展和折叠项目的checkable接口。然后看起来我的逻辑是正确的(因为展开按钮文本仍然在改变)-但是布局没有正确地重新绘制-就像它应该展开时被折叠并且看起来破碎了。
在玩了一点点日志记录之后,我发现每次我点击任何项目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实现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
我选择第一项(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
选择第二个
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
我被困在这个问题上最后3或4个小时,最后我试图从扩展和崩溃方法中删除setChecked()调用(不记得为什么我把它们放在那里…)。也许它对你有帮助,这里是我的代码片段:
private void collapseRow() {
mText.setMaxLines(2);
mText.setEllipsize(TruncateAt.MARQUEE);
mExpand.setText(COllAPS);
mWiki.setVisibility(GONE);
requestLayout();
state = COLLAPSED;
}
private void expandRow() {
mText.setMaxLines(Integer.MAX_VALUE);
mText.setEllipsize(null);
mExpand.setText(EXPAND);
mWiki.setVisibility(VISIBLE);
requestLayout();
state = EXPANDED;
}
private void setData() {
mText.setText(mItem.getDate());
}
@Override
public boolean isChecked() {
Log.d("isChecked", "!!!!!!!");
return mChecked;
}
@Override
public void setChecked(boolean state) {
count++;
if (state)
expandRow();
else
collapseRow();
Log.d("setChecked", ""+ state + "" + count);
}
@Override
public void toggle() {
Log.d("toggle", "!!!!!!!");
setChecked(!mChecked);
}
现在,所有的工作都很完美:没有多次调用setChecked,并且ListItems像它们应该的那样打开和关闭。