3

現在、GridViewがあり、すべての要素に個別のProgressBarが必要です。要素は個別のダウンロードを表します。これらのProgressBarを使用してダウンロードの状態を表示したいと思います。

しかし、どのように更新すればよいですか?私の問題は、ドキュメント(およびGoogle IOビデオで聞いたこと)によると、AdapterViewsの要素を更新する唯一の方法は、割り当てられたアダプターを更新し、更新のたびに.notifyDatasetChanged()を呼び出すことです。

もちろん、これはこの場合も実際に機能しますが、更新は非常に頻繁に行われます(5〜10更新秒)。また、この頻度で.notifyDatasetChangedを呼び出すと、GridViewとの相互作用が中断されます。たとえば、ユーザーがグリッド内のアイテムをロングクリックしようとしましたが、ダウンロードの進行状況を更新するために.notifyDatasetChangedが呼び出されたため、クリックイベントは停止します。

これを行う他の方法はありますか?AdapterViewsを捨てて、他のものを使用する必要がありますか?

4

2 に答える 2

2

頻繁に(1秒間に何度も)更新されるような状況では、notifyDatasetChangedは正しい方法ではありません-個々のビューを直接更新する必要があります。

コードと、ファイルのダウンロードとGridViewのエントリとの関係を確認しないと、必要な呼び出しを正確に言うことは困難ですが、次のようになります。

-GridViewには、特定のインデックスでビューを取得するために使用できる「getChildAt」があります。
-ビューが表示されていなくても、ビューを更新しても意味がありません。getFirstVisiblePosition()とgetLastVisiblePosition()の間にない場合は、無視してください。再描画されるたびに、更新されたデータで描画されます。

于 2011-11-14T23:55:33.143 に答える
0

私はこれを1つのプロジェクトで行いました。.notifyDatasetChanged()なしでAsysctaskを使用している場合は、UIを更新できます。たとえば、Utilsにダウンロード方法があります。クラス

public static String downloadFile(DownloadItem downItem, AsynBackGroundFileLoader asynBackGroundFileLoader, String fileName,
            String fileURL) {
                // Spiking my code 
            while (bufferLength = inputStream.read(buffer) > 0 ) {
                    fileOutput.write(buffer, 0, bufferLength);
                // add up the size so we know how much is downloaded
                downloadedSize += bufferLength;

                // this is where you would do something to report the progress,
                int pro = ((int) (downloadedSize * 100 / totalSize));

                asynBackGroundFileLoader.updateProgress(pro);

            }
    }

そして私のAsyncTaskで

public class AsynBackGroundFileLoader extends AsyncTask < Void , Integer , String > {



public AsynBackGroundFileLoader (DownloadItem dItem , TableLayout progressBarHodler , UpgradeActivity listener ) {
        try {
            mDownloadingProgressBarHolder = progressBarHodler ;
            this.mDownloadingProgressBarHolder.setVisibility ( View.VISIBLE ) ;

            mDownloadingProgressBar = ( ProgressBar ) mDownloadingProgressBarHolder.findViewById ( R.id.downloading_progress_bar ) ;
            downloadCancelButton = ( ImageButton ) mDownloadingProgressBarHolder.findViewById ( R.id.downloading_progress_cancel_btn ) ;
            downloadCancelButton.setImageResource ( R.drawable.btn_cancel );

    @ Override
    protected void onPreExecute ( ) {

    }

    @ Override
    protected String doInBackground ( Void ... params ) {

            boolean loadSuccess = Utils.downloadFile ( downItem , this , mFileName , downItem.getLink ( ) ) ;
        return ""+loadSuccess ;
    }

    @ Override
    protected void onPostExecute ( String result ) {

    }

    public void updateProgress ( int progressPercentage ) {
        try {
            if ( progressPercentage < 0 ) {
                progressPercentage = 0 ;
            } else if ( progressPercentage > 100 ) {
                progressPercentage = 100 ;
            }

//Here I am updating my UI without making list to notifyDataChanged()
                currentCompletedProgress = progressPercentage ;
                // This is my progressbar in UI that update , I got this progressbar by passing UI item view in constructor 
                mDownloadingProgressBar.setProgress ( progressPercentage ) ;
                downItem.setTotalProgress ( currentCompletedProgress ) ;
            } catch ( Exception e ) {
                Log.e ( TAG , "Exception = " + e.toString ( ) ) ;
                e.printStackTrace ( ) ;
            }

        }

    public ProgressBar getmDownloadinProgressBar ( ) {
        return mDownloadingProgressBar ;
    }
}

ユーザーがリスト行をなめるとダウンロードを開始します

public void onItemClick(AdapterView<?> tempadapter, View view, int position, long arg3) {
    TableLayout table = ((TableLayout) view.findViewById(R.id.downloading_progress_holder));
    DownloadItem temp = adapter.items.get(position);

        AsynBackGroundFileLoader bgdownloader = new AsynBackGroundFileLoader(temp, table, UpgradeActivity.this);
        bgdownloader.execute();
    }

    table.setVisibility(View.VISIBLE);
}
于 2011-11-14T09:29:26.487 に答える