闲来没事写了个模拟下载进度条显示的demo,点击开始下载,然后进度逐渐增加。代码如下:
TestActivity.java如下
package test.demo.com; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.ListView; import android.widget.ProgressBar; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Timer; import java.util.TimerTask; /** * Created by Administrator on 2016/1/15. */ public class TestActivity extends Activity { private ListView lv; private TestAdapter mAdapter; private Map<Integer, ProgressBar> map; private Map<Integer, Integer> progress; private List<Integer> positionList; @Override protected void onCreate (Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView (R.layout.activity_test); lv = (ListView) findViewById (R.id.text_lv); List<Integer> test = new ArrayList<> (); test.add (1); test.add (1); test.add (1); test.add (1); map = new HashMap<> (); progress = new HashMap<> (); positionList = new ArrayList<> (); mAdapter = new TestAdapter (this, test, R.layout.item_test, new TestAdapter.myOnClick () { @Override void myclick (View v, Integer position) { map.put (position, (ProgressBar) v); progress.put (position, 0); positionList.add (position); start (); } }); lv.setAdapter (mAdapter); } private void start () { new Timer ().schedule (new TimerTask () { @Override public void run () { for (Integer item : positionList) { map.get (item).setProgress (progress.get (item)); progress.put (item, progress.get (item) + 5); } } }, 1000, 1000 * 5); } }
adapter如下:
package test.demo.com; import android.content.Context; import android.view.View; import java.util.List; /** * Created by Administrator on 2016/1/15. */ public class TestAdapter extends MyBaseAdapter<Integer> { public TestAdapter (Context context, List<Integer> datas, int layoutId, myOnClick onClick) { super (context, datas, layoutId); this.onClick = onClick; } private myOnClick onClick; @Override public void convert (ViewHolder holder, Integer integer) { holder.getView (R.id.item_test_pb).setOnClickListener (onClick); holder.getView (R.id.item_test_pb).setTag (position); } public static abstract class myOnClick implements View.OnClickListener { @Override public void onClick (View v) { myclick (v, (Integer) v.getTag ()); } abstract void myclick (View v, Integer position); } }下面是布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ListView android:id="@+id/text_lv" android:layout_width="match_parent" android:layout_height="match_parent"></ListView> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ProgressBar android:id="@+id/item_test_pb" style="?android:attr/progressBarStyleHorizontal" android:layout_width="300dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="10dp"/> </LinearLayout>
代码不多,很简单啦。
效果就是下图那样啦