我在Cal Poly Lab3(http://sites.google.com/site/androidappcourse/labs/lab-3)上工作,一切都正常工作,直到我实现了扩展和折叠项的可检查接口。然后它缝合了我的逻辑是正确的(因为展开按钮文本仍在按原样更改)-但是布局没有正确地重新绘制-就像它在应该展开时被折叠,看起来很破碎。
在玩了一点日志之后,我发现每次单击任何item setchecked()方法时,都会在很短的时间内对每个item调用两次,这会影响绘图。第一次使用旧的选择数据-然后使用新的数据。
更奇怪的是,我通过设置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());

}

下面是catlog中的情况:
我启动了一个活动,该活动显示我的自定义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

我选择第一个项目(一个小…)
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个小时,最后我试着从extend和collapse方法中删除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应该打开和关闭。

07-24 09:47
查看更多