我想顯示多個進度欄像Whats App一樣上傳或下載圖片或視頻。並且對bi隱藏進度條完成的進度。多個圖像進度條加載喜歡在Whats App中
我試圖做到這一點使用異步任務但沒有得到精確的解決方案,也用於由代表。沒有得到一個確切的解決方案。
這裏是我的代碼
主要活動
public class MainActivity extends ActionBarActivity {
private ListView mListView;
private ArrayList<String> alst;
private int count = 0;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.listView1);
alst = new ArrayList<String>();
adapter = new MyAdapter(this, R.layout.activity_main, alst);
mListView.setAdapter(adapter);
}
public void onClick(View v) {
count++;
alst.add("" + count);
adapter.notifyDataSetChanged();
}
}
這裏是我的適配器
public class MyAdapter extends ArrayAdapter<String> {
private Context mContext;
public ViewHolder viewHolder;
public MyAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
mContext = context;
}
private class ViewHolder {
ProgressBar progressBar;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
viewHolder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.adapter, parent, false);
viewHolder = new ViewHolder();
viewHolder.progressBar = (ProgressBar) convertView
.findViewById(R.id.progressBar1);
viewHolder.progressBar.getIndeterminateDrawable().setColorFilter(
mContext.getResources().getColor(R.color.actionBar),
android.graphics.PorterDuff.Mode.MULTIPLY);
viewHolder.progressBar.setVisibility(View.VISIBLE);
convertView.setTag(viewHolder);
}
viewHolder = (ViewHolder) convertView.getTag();
TextView textView = (TextView) convertView.findViewById(R.id.textView1);
textView.setText(getItem(position));
new DownloadAsyncTask().execute(viewHolder);
return convertView;
}
public class DownloadAsyncTask extends
AsyncTask<ViewHolder, Void, ViewHolder> {
@Override
protected ViewHolder doInBackground(ViewHolder... params) {
ViewHolder viewHolder = params[0];
for (int i = 0; i < 2500; i++) {
System.out.println(i);
}
return viewHolder;
}
@Override
protected void onPostExecute(ViewHolder result) {
result.progressBar.setVisibility(View.GONE);
}
}
}
而且這裏有進展
讓我知道有價值的解決方案,或者是否有任何圖書館利益
這是行得通的,但是這會爲每個視圖多次調用異步任務。例如,如果列表有4個元素,它將爲第4個元素調用4次。 –