【问题标题】:how to refresh one row in listview android [duplicate]如何在listview android中刷新一行[重复]
【发布时间】:2013-06-19 11:09:30
【问题描述】:

我有一个包含自定义控件的列表视图
我的自定义控件中有进度条,我想更改进度条的值。

int iRowCount = 0;
    CMessage processMessage = null;
    for (CMessage msg : m_msgList)
    {
        iRowCount ++;
        if(msg._id == iMessageId)
        {
            processMessage = msg;
            break;
        }
    }

    if(processMessage == null)
        return;

    if(!processMessage.MessageType.equals(AttachType.Video.name()))
        if(!processMessage.MessageType.equals(AttachType.Audio.name()))
            return;

    int rowPosition = iRowCount; 
    View v = lstMessage.getChildAt(rowPosition);
    View updateView = lstMessage.getAdapter().getView(rowPosition - 1, v, lstMessage);
    ILSpeechBubble sBubble = (ILSpeechBubble) updateView.findViewById(R.id.iLSpeechBubble1);
    if(sBubble.getMessageId() == iMessageId)
    {
        iProgress = sBubble.getProgress();
        if(iProgress == 100)
            m_Downloading = false;
        else
            iProgress += 1;

        sBubble.SetProgress(iProgress);
        sBubble.postInvalidate();
        updateView.postInvalidate();

    }

使用此代码,我可以获得必须更改进度值的行
但我无法刷新 ui 中未更改的布局和进度
但是当我滚动列表视图时,进度值发生了变化。!
而且我不想使用 notifydatasetchanged()。
tnx 4 任何帮助

【问题讨论】:

  • 你为什么不想使用notifydatasetchanged
  • 因为我不想刷新整个视图
  • 与维护类似代码的复杂性相比,刷新整个视图的代价是什么? ;)
  • 列表视图中有更多项目可能包含图片、文本、音频、视频。不需要更多的 CPU 使用率?
  • 最后我使用 listview.invalidateViews(); !它类似于 notifyDatasetchanged()

标签: android android-layout android-listview


【解决方案1】:

您可以使用以下代码刷新列表视图行:

 ListView list = getListView();
    int start = list.getFirstVisiblePosition();
    for(int i=start, j=list.getLastVisiblePosition();i<=j;i++)
        if(target==list.getItemAtPosition(i)){
            View view = list.getChildAt(i-start);
            list.getAdapter().getView(i, view, list);
            break;
        }

【讨论】:

  • 究竟是哪一行刷新了行?
  • @Leon_SFS 在目标变量中提到。
【解决方案2】:

用它来清洁适配器:

adapter.items.clear();

【讨论】:

  • 请用英文写好吗?
  • 我在代码中的任何地方都看不到适配器。这个答案没有解释,似乎也没有回答问题。
猜你喜欢
  • 2012-08-14
  • 1970-01-01
  • 1970-01-01
  • 2011-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-21
  • 1970-01-01
相关资源
最近更新 更多