涉及的知识点:ListView的复用,倒计时,启动线程刷新UI
由于项目有个需求,就是点击ListView的某个item的button,开始倒计时。所以花了些时间写了个Demo,先上效果图
倒计时.gif
看起来是不是感觉很简单,但一开始走了很多坑。首先是ListView的复用问题。点击一个item的button开始倒计时,但是把ListView滑下来,下面的item会复用前面的item,导致它没有点击也会倒计时。产生这个原因是因为我在当我点击button时,对TextView设置
Text,也就是倒计时,而那些没有点击的item没有被setText,所以会导致复用,时间错乱。ListView的数据源是动态的话,由于复用往往会使显示的数据错乱。
切入正题,说说实现的思路。
我们使用一个boolean变量标记button的点击事件,一旦点击了,我们就开启倒计时,并标记为true。然后开启一个线程,把ListvView所有的TextView传到这个线程中,通过一个死循环,每隔一秒刷新一次UI,将每个TextView进行setText,如果数据类的成员变量isPressed为true,就设置为倒计时的时间,如果不是,就设为“00:00”,思路就这样。
首先我们需要一个数据类
public class Bean {
private String text;//button的text
private int time;//TextView显示的时间
private boolean isPressed;//标记button是否按下。
private TimerEnitity timerEnitity;//进行倒计时的类
public TimerEnitity getTimerEnitity() {
return timerEnitity;
}
public void setTimerEnitity(TimerEnitity timerEnitity) {
this.timerEnitity = timerEnitity;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public int getTime() {
return time;
}
public boolean isPressed() {
return isPressed;
}
public void setPressed(boolean isPressed) {
this.isPressed = isPressed;
}
public void setTime(int time) {
this.time = time;
}
}
我们还需要一个倒计时的类,通过这个类我们可以实时获取倒计时的时间。我们使用android的倒计时类CountDownTimer,继承它作为内部类,一旦初始化TimerEnitity,我们就开始倒计时,并实时获取倒计时的时间。
public class TimerEnitity {
private int timeLength;
private MyTimer timer;
private Bean bean;
public TimerEnitity(int timeLength,Bean bean) {
this.timeLength = timeLength;
this.bean=bean;
timer = new MyTimer(timeLength,999);
timer.start();
}
public int getsurplusTime(){
return this.bean.getTime();
}
public void setTimeLength(int timeLength) {
this.timeLength = timeLength;
}
public class MyTimer extends CountDownTimer{
public MyTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long l) {
Log.e("time",l+"");
bean.setTime((int) l);//通过系统不断地回调这个方法,time值不断地被改变,我们就可以获取倒计时的时间。
}
@Override
public void onFinish() {
bean.setTime(0);
}
}
}
接下来就是对Activity和Adapter的操作了,先上代码。
public class MainActivity extends AppCompatActivity {
private ListView listView;
private TimeAdapter adapter;
private TimeThread timeThread;
private List<Bean> data = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listview);
initData();
timeThread = new TimeThread();
timeThread.start();
adapter = new TimeAdapter(listView);
listView.setAdapter(adapter);
}
private void initData() {
for (int i = 0; i < 30; i++) {
Bean bean = new Bean();
bean.setText("item" + i);
bean.setTime(0);
bean.setPressed(false);
data.add(bean);
}
}
class TimeAdapter extends BaseAdapter {
private ListView listView;
public TimeAdapter(ListView listView){
this.listView = listView;
}
private LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int i) {
return (Bean)data.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
ViewHolder holder = null;
final Bean bean = (Bean) getItem(i);
if (view == null) {
view = inflater.inflate(R.layout.item, null);
holder = new ViewHolder();
holder.textView = (TextView) view.findViewById(R.id.text);
holder.button = (Button) view.findViewById(R.id.btn);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
timeThread.setTextViewToList(holder.textView,bean);
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
bean.setPressed(true);
TimerEnitity enitity = new TimerEnitity(60000,bean);
bean.setTimerEnitity(enitity);
timeThread.setChangedBean(i,bean);
}
});
holder.textView.setText(MyUtils.timeToString(bean.getTime()));
holder.button.setText(data.get(i).getText());
return view;
}
public class ViewHolder {
public TextView textView;
public Button button;
}
}
}
上面的代码关键在两个地方
第1个地方
timeThread = new TimeThread();
timeThread.start();
上面我们讲过,这个线程一直不断地更新UI,所以应该在onCreate方法里启动
第2个地方
timeThread.setTextViewToList(holder.textView,bean);
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TimerEnitity enitity = new TimerEnitity(60000,bean);
bean.setPressed(true);
bean.setTimerEnitity(enitity);
timeThread.setChangedBean(i,bean);
}
});
holder.textView.setText(MyUtils.timeToString(bean.getTime()));
holder.button.setText(data.get(i).getText());
线程的操作涉及到bean数据类,所以要将Textview和Bean传过去.然后在button的点击事件里,我们标记为true,并启动倒计时。这里需要留意一下,即使我们在onClick方法里将bean的变量值进行修改,比如bean.setPressed(true);bean.setTimerEnitity(enitity);
但是点击事件是不定时的,所以此时的修改并不影响到已经传递到线程里的bean数据,所以需要写个方法将修改后的变量传过去timeThread.setChangedBean(i,bean);
我们来看一下TimeThread的代码,在run方法中,我们通过一个死循环,每隔一秒发送一个消息,在handler中对所有的TextView遍历,点击过的,就将TextView设置倒计时的时间,没有点击的就设置为默认时间。
public class TimeThread extends Thread {
private List<TextView> textViews;
private List<Bean> beans = new ArrayList<>();
private boolean isRun=true;
private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
int cache = -1;
for(int i=0;i<textViews.size();i++){
if (beans.get(i).isPressed()) {
textViews.get(i).setText(MyUtils.timeToString(beans.get(i).getTimerEnitity().getsurplusTime()));
}else textViews.get(i).setText("00:00");
}
}
};
public TimeThread(){
textViews = new ArrayList<TextView>();
}
@Override
public void run() {
while (isRun){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
mHandler.sendEmptyMessage(0);
}
}
public void setTextViewToList(TextView textView,Bean bean){
textViews.add(textView);
beans.add(bean);
}
public void setChangedBean(int postion,Bean bean){
beans.get(postion).setPressed(bean.isPressed());
beans.get(postion).setTimerEnitity(bean.getTimerEnitity());
}
}
最后还需要将时间转化一下格式。
public class MyUtils {
public static String timeToString(int timeLength) {
int min = Math.round((timeLength/1000)/ 60);
int second = Math.round((timeLength/1000) % 60);
Log.e("min",min+"");
Log.e("second",second+"");
if (timeLength <=0) {
return "00:00";
} else if (timeLength >0) {
if (min < 10 && second < 10)
return "0" + min +":"+ "0" + second;
if (min < 10 && second >= 10)
return "0" + min +":"+ second;
if (min >= 10 && second < 10)
return min +":"+ "0" + second;
if (min >= 10 && second >= 10)
return min +":" + second;
}
return null;
}
}
想查看demo请从github下载https://github.com/MRKCH/itemcounter
网友评论