Adding a ProgressBar to a ListView/OnClick called a single time

前端 未结 2 1705
走了就别回头了
走了就别回头了 2021-02-09 15:26

The idea is to have a list of items where after clicking an item, a ProgressBar will slowly fill as the task is completed. For example, picture a list of files, with a Download

2条回答
  •  旧巷少年郎
    2021-02-09 16:25

    Did you try setting click listener to download item in adapter itself like following:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        if(row == null) {
            // Inflate
            Log.d(TAG, "Starting XML inflation");
            LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.download_list_item, parent, false);
            Log.d(TAG, "Finished XML inflation");
        }
    
        final DownloadItem item = mItems.get(position);
    
        ProgressBar downloadProgressBar = (ProgressBar) row.findViewById(R.id.downloadProgressBar);
        Button downloadButton = (Button) row.findViewById(R.id.downloadButton);
    
        downloadButton.setTag(item);
        downloadProgressBar.setMax(item.length);
        downloadProgressBar.setProgress(item.progress);
    
        downloadButton.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                new DownloadTask(DownloadArrayAdapter.this, item).execute();
            }
        });
    
        return row;
    }
    

提交回复
热议问题