如何在不使用notifyDataSetChanged()的情况下更新ListView中的一些数据?

23

我正在尝试创建一个包含下载任务列表的ListView

下载任务是在Service(DownloadService)中管理的。每次接收到一块数据时,任务通过Broadcast发送进度,然后由包含ListView(SavedShowListFragment)的Fragment接收。在接收到Broadcast消息时, SavedShowListFragment会更新适配器中下载任务的进度并触发notifyDataSetChanged()

列表中的每一行都包含一个ProgressBar、一个用于显示正在下载文件标题的TextView和一个用于显示数字进度值的TextView,以及在下载完成时暂停/恢复下载或播放已保存的节目的Button

问题是,暂停/恢复/播放Button经常没有响应(onClick()未被调用),我认为这是因为整个列表非常频繁地使用notifyDataSetChanged()进行更新(每次接收到一块数据,即1024字节时进行更新,这可能是多个下载任务同时运行时每秒钟多次更新)。

我想我可以增加下载任务中的数据块大小,但我真的认为我的方法根本不是最优的!

频繁调用notifyDataSetChanged()会使ListView界面不响应吗?

在不调用刷新整个列表的notifyDataSetChanged()的情况下,是否有办法仅更新ListView行中的某些Views,即在我的情况下为ProgressBar和显示进度数字值的TextView

更新ListView中下载任务的进度,有没有比“getChunk/sendBroadcast/updateData/notifyDataSetChanged”更好的选项?

以下是我代码的相关部分。

Download Service中的下载任务

public class DownloadService extends Service {

    //...

    private class DownloadTask extends AsyncTask<SavedShow, Void, Map<String, Object>> {

        //...

        @Override
        protected Map<String, Object> doInBackground(SavedShow... params) { 

            //...

            BufferedInputStream in = new BufferedInputStream(connection.getInputStream());

            byte[] data = new byte[1024];
            int x = 0;

            while ((x = in.read(data, 0, 1024)) >= 0) {

                if(!this.isCancelled()){
                    outputStream.write(data, 0, x);
                    downloaded += x;

                    MyApplication.dbHelper.updateSavedShowProgress(savedShow.getId(), downloaded);

                    Intent intent_progress = new Intent(ACTION_UPDATE_PROGRESS);
                    intent_progress.putExtra(KEY_SAVEDSHOW_ID, savedShow.getId());
                    intent_progress.putExtra(KEY_PROGRESS, downloaded );
                    LocalBroadcastManager.getInstance(DownloadService.this).sendBroadcast(intent_progress);         
                }
                else{
                    break;
                }
            }

            //...
        }

        //...
    }
}

SavedShowListFragment

public class SavedShowListFragment extends Fragment {   

    //...

    @Override
    public void onResume() {         
        super.onResume();

        mAdapter = new SavedShowAdapter(getActivity(), MyApplication.dbHelper.getSavedShowList());

        mListView.setAdapter(mAdapter);

        //...
    }


    private ServiceConnection mDownloadServiceConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className, IBinder service) {

            // Get service instance

            DownloadServiceBinder binder = (DownloadServiceBinder) service;
            mDownloadService = binder.getService();

            // Set service to adapter, to 'bind' adapter to the service

            mAdapter.setDownloadService(mDownloadService);

            //...
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {

            // Remove service from adapter, to 'unbind' adapter to the service

            mAdapter.setDownloadService(null);
        }
    };


    private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            String action = intent.getAction();

            if(action.equals(DownloadService.ACTION_UPDATE_PROGRESS)){  
                mAdapter.updateItemProgress(intent.getLongExtra(DownloadService.KEY_SAVEDSHOW_ID, -1),
                        intent.getLongExtra(DownloadService.KEY_PROGRESS, -1));
            }

            //...
        }
    };

    //...

}
SavedShowAdapter 保存的节目适配器。
public class SavedShowAdapter extends ArrayAdapter<SavedShow> { 

    private LayoutInflater mLayoutInflater;

    private List<Long> mSavedShowIdList; // list to find faster the position of the item in updateProgress

    private DownloadService mDownloadService;

    private Context mContext;

    static class ViewHolder {
        TextView title;
        TextView status;
        ProgressBar progressBar;
        DownloadStateButton downloadStateBtn;
    }

    public static enum CancelReason{ PAUSE, DELETE };

    public SavedShowAdapter(Context context, List<SavedShow> savedShowList) {
        super(context, 0, savedShowList);       
        mLayoutInflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE ); 

        mContext = context;

        mSavedShowIdList = new ArrayList<Long>();

        for(SavedShow savedShow : savedShowList){
            mSavedShowIdList.add(savedShow.getId());
        }
    }

    public void updateItemProgress(long savedShowId, long progress){
        getItem(mSavedShowIdList.indexOf(savedShowId)).setProgress(progress);
        notifyDataSetChanged();
    }

    public void updateItemFileSize(long savedShowId, int fileSize){
        getItem(mSavedShowIdList.indexOf(savedShowId)).setFileSize(fileSize);
        notifyDataSetChanged();
    }


    public void updateItemState(long savedShowId, int state_ind, String msg){

        SavedShow.State state = SavedShow.State.values()[state_ind];

        getItem(mSavedShowIdList.indexOf(savedShowId)).setState(state);

        if(state==State.ERROR){
            getItem(mSavedShowIdList.indexOf(savedShowId)).setError(msg);
        }

        notifyDataSetChanged();
    }

    public void deleteItem(long savedShowId){
        remove(getItem((mSavedShowIdList.indexOf(savedShowId))));       
        notifyDataSetChanged();
    }

    public void setDownloadService(DownloadService downloadService){
        mDownloadService = downloadService;
        notifyDataSetChanged();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        View v = convertView;

        if (v == null) {

            v = mLayoutInflater.inflate(R.layout.saved_show_list_item, parent, false);

            holder = new ViewHolder();

            holder.title = (TextView)v.findViewById(R.id.title);
            holder.status = (TextView)v.findViewById(R.id.status);
            holder.progressBar = (ProgressBar)v.findViewById(R.id.progress_bar);
            holder.downloadStateBtn = (DownloadStateButton)v.findViewById(R.id.btn_download_state);

            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }

        holder.title.setText(getItem(position).getTitle());

        Integer fileSize = getItem(position).getFileSize();
        Long progress = getItem(position).getProgress();
        if(progress != null && fileSize != null){
            holder.progressBar.setMax(fileSize);

            holder.progressBar.setProgress(progress.intValue());

            holder.status.setText(Utils.humanReadableByteCount(progress) + " / " +
                    Utils.humanReadableByteCount(fileSize));
        }

        holder.downloadStateBtn.setTag(position);

        SavedShow.State state = getItem(position).getState();

        /* set the button state */

        //...

        /* set buton onclicklistener */

        holder.downloadStateBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                int position = (Integer) v.getTag();

                SavedShow.State state = getItem(position).getState();

                if(state==SavedShow.State.DOWNLOADING){

                    getItem(position).setState(SavedShow.State.WAIT_PAUSE);
                    notifyDataSetChanged();

                    mDownloadService.cancelDownLoad(getItem(position).getId(), CancelReason.PAUSE);

                }
                else if(state==SavedShow.State.PAUSED || state==SavedShow.State.ERROR){                 

                    getItem(position).setState(SavedShow.State.WAIT_DOWNLOAD);
                    notifyDataSetChanged();

                    mDownloadService.downLoadFile(getItem(position).getId());

                }
                if(state==SavedShow.State.DOWNLOADED){

                    /* play file */
                }

            }
        });

        return v;
    }
} 

我认为progressBar.setProgress足以更新列表,我错了吗?这个链接有帮助吗?https://dev59.com/9WXWa4cB1Zd3GeqPMWb2 - Srinath Ganesh
我的问题不在于如何设置进度,而是如何仅更新ListView行中的某些视图。无论如何感谢您。 - jul
1
如果我正确理解了您的问题,那么您的问题是notifyDataSetChanged会在1秒内被多次调用。我不知道如何在不使用notifyDataSetChanged的情况下更新ListView UI,但在这种情况下,我会每4到6秒钟调用一次notifyDataSetChanged,而不是在数据更改时每时每刻都调用它。 - edisonthk
@edisonthk 是的,我可以做到这一点。但是每4-6秒更新一次进度条并不是很好的解决方案!如果我找不到其他解决方案,那可能就是我要做的了... - jul
@jul,不一定非得是4-6s。比如说,如果下载过程每2毫秒更新一次,那么UI线程将会很繁重,需要每次服务更新时都进行更新。为了解决这个问题,我会让UI线程每100毫秒或500毫秒更新一次,而500毫秒对于人类来说已经足够快了。 - edisonthk
3个回答

22

当然,正如pjco所说的那样,不要以那个速度进行更新。我建议按照一定的时间间隔发送广播。更好的方法是,在数据方面有一个容器,比如进度,并通过轮询每个时间间隔进行更新。

然而,我认为在某些情况下没有使用notifyDataSetChanged也是值得的,特别是当应用程序更新频率较高时,这样做非常实用。请记住:我并不是说您的更新机制是正确的。


解决方案

基本上,您想要在不使用notifyDataSetChanged的情况下更新特定位置。在以下示例中,我假设以下情况:

  1. 您的ListView名为mListView。
  2. 您只想要更新进度。
  3. convertView中进度条的id为R.id.progress

public boolean updateListView(int position, int newProgress) {
    int first = mListView.getFirstVisiblePosition();
    int last = mListView.getLastVisiblePosition();
    if(position < first || position > last) {
        //just update your DataSet
        //the next time getView is called
        //the ui is updated automatically
        return false;
    }
    else {
        View convertView = mListView.getChildAt(position - first);
        //this is the convertView that you previously returned in getView
        //just fix it (for example:)
        ProgressBar bar = (ProgressBar) convertView.findViewById(R.id.progress);
        bar.setProgress(newProgress);
        return true;
    }
}

注意事项

这个示例当然并不完整。您可以按照以下顺序进行操作:

  1. 更新您的数据(当您收到新进度时)
  2. 调用updateListView(int position),它应该使用相同的代码,但是使用您的数据集进行更新,而不需要参数。

此外,我刚刚注意到您已经发布了一些代码。由于您正在使用Holder,因此可以在函数内部简单地获取holder。我不会更新代码(我认为它已经很容易理解了)。

最后,只是为了强调,更改您的整个代码以触发进度更新。一个快速的方法是修改您的Service:将发送广播的代码包装在if语句中,该语句检查上次更新是否已超过一秒钟或半秒钟,并且下载是否已完成(无需检查完成,但确保在完成时发送更新):

在您的下载服务中

private static final long INTERVAL_BROADCAST = 800;
private long lastUpdate = 0;

现在在doInBackground中,用if语句包装发送意图的代码

if(System.currentTimeMillis() - lastUpdate > INTERVAL_BROADCAST) {
    lastUpdate = System.currentTimeMillis();
    Intent intent_progress = new Intent(ACTION_UPDATE_PROGRESS);
    intent_progress.putExtra(KEY_SAVEDSHOW_ID, savedShow.getId());
    intent_progress.putExtra(KEY_PROGRESS, downloaded );
    LocalBroadcastManager.getInstance(DownloadService.this).sendBroadcast(intent_progress);
}

жҲ‘们жҳҜдёҚжҳҜиҝҷж ·е®һзҺ°иҮӘе·ұзҡ„notifyDataSetChanged()ж–№жі•пјҹ жҹҘзңӢзӯ”жЎҲ - https://dev59.com/uGct5IYBdhLWcg3wQ7My#12234498 еӣ дёәеҚідҪҝnotifyDataSetChanged()еҸӘи°ғз”ЁеҸҜи§ҒйЎ№дёҠзҡ„getView()ж–№жі•гҖӮ - Darpan
1
@Darpan notifyDataSetChanged() 只会调用可见项的 getView() 方法;这是正确的。但请注意,它会销毁所有可用的视图并将 null 的 convertViews 传递给 getView()。换句话说,可见视图会被重新创建。 - Sherif elKhatib
这就是我所说的,但我理解你的观点。区别在于重新创建“所有”视图和仅像您在代码中所做的那样“更新”“一个”视图。不错。 - Darpan

9

简短回答:不要根据数据速度更新UI

除非您正在编写速度测试样式的应用程序,否则没有更新此方式的用户利益。

ListView已经进行了非常好的优化(因为您似乎已经知道并使用了ViewHolder模式)。

您尝试过每1秒调用notifyDataSetChanged()吗?

每1024字节非常快。如果有人以8Mbps下载,这可能会更新超过1000次/秒,这肯定会导致ANR。

与其根据下载量更新进度,不如以不会导致UI阻塞的间隔轮询数量。

无论如何,为了帮助避免阻塞UI线程,您可以将更新发布到Handler

玩弄sleep的值,以确保您没有更新得太频繁。您可以尝试降至200ms,但我不建议低于500ms以确保。确切的值取决于您所针对的设备和需要布局传递的项目数。

注意:这只是一种方法,有许多方法可以完成此类循环。

private static final int UPDATE_DOWNLOAD_PROGRESS = 666;

Handler myHandler = new Handler()
{
    @Override
    handleMessage(Message msg)
    {
        switch (msg.what)
        {
            case UPDATE_DOWNLOAD_PROGRESS:
                myAdapter.notifyDataSetChanged();
                break;
            default:
                break;
        }
    }
}



private void runUpdateThread() { 
    new Thread(
     new Runnable() {
         @Override
         public void run() {
             while ( MyFragment.this.getIsDownloading() )
             {
                  try 
                  {    
                      Thread.sleep(1000); // Sleep for 1 second

                      MyFragment.this.myHandler
                          .obtainMessage(UPDATE_DOWNLOAD_PROGRESS)
                          .sendToTarget();
                  } 
                  catch (InterruptedException e) 
                  {
                      Log.d(TAG, "sleep failure");
                  }
             }

         }
     } ).start(); 
}

3

虽然这不是对你问题的回答,但是你可以优化 getView() 方法,而不是每次都像这样创建和设置点击监听器:

holder.downloadStateBtn.setTag(position); 
holder.downloadStateBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) { 
            int position = (Integer) v.getTag(); 
             // your current normal click handling
        }
    });

您可以将其作为类变量创建一次,并在创建行的View时设置它:
final OnClickListener btnListener = new OnClickListener() {

    @Override
    public void onClick(View v) { 
        int position = (Integer) v.getTag();
        // your normal click handling code goes here
    }
}

然后在 getView() 方法中:

 if (v == null) {
        v = mLayoutInflater.inflate(R.layout.saved_show_list_item, parent, false);
        // your ViewHolder stuff here 
        holder.downloadStateBtn.setOnClickListener(btnClickListener);//<<<<<
        v.setTag(holder);
    } else {
        holder = (ViewHolder) v.getTag();
    }

哦,不要忘记在 getView() 中设置此按钮的标签,就像您已经做的那样:

holder.downloadStateBtn.setTag(position);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接