【问题标题】:Countdown timers in a ListViewListView 中的倒数计时器
【发布时间】:2015-02-17 10:18:04
【问题描述】:

我有一个ListView,上面有定时器的名字,还有定时器的时间,倒计时部分不知道怎么实现。在 ListView 的每一行中,我有两个 TextView:时间和计时器的名称,然后是一个应该启动和停止计时器的 Button。这是我的自定义 ListViewAdapter 类,我相信计时器应该在 OnClick 方法中开始停止,让计时器对每一行做出反应:

public class CustomTimerRowAdapter extends ArrayAdapter<TimerRow> {

Context context;
int height; 

public CustomTimerRowAdapter(Context context, int resourceId,
        List<TimerRow> items) {
    super(context, resourceId, items);
    this.context = context;
    // Height of screen from not Activity subclass
    height = context.getResources().getDisplayMetrics().heightPixels;
}


/* private view holder class */
private class ViewHolder {
    TextView txtTimer;
    TextView txtName;
    Button bStartStop;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    TimerRow rowItem = getItem(position);

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.timer_row, null);
        holder = new ViewHolder();
        // make layout params
        holder.txtTimer = (TextView) convertView.findViewById(R.id.tvTimes);
        holder.txtTimer.getLayoutParams().height = height / 10;
        holder.txtName = (TextView) convertView.findViewById(R.id.tvName);
        holder.txtName.getLayoutParams().height = height / 10;          
        holder.bStartStop = (Button) convertView
                .findViewById(R.id.bStartStopTimer);
        holder.bStartStop.getLayoutParams().height = height / 10;           
        convertView.setTag(holder);

    } else
        holder = (ViewHolder) convertView.getTag();

    holder.txtName.setText(rowItem.getName());
    holder.txtTimer.setText(rowItem.getTimer());
    final String name = holder.txtName.getText().toString();
    holder.bStartStop.setOnClickListener(new OnClickListener() {        

        @Override
        public void onClick(View v) {                   
            // THIS IS WHERE THE TIMER SHOULD START AND STOP.
            Toast.makeText(context, "Selected " + name + " timer.",
                Toast.LENGTH_SHORT).show();
        }
    });             
    return convertView;     
}
}

计时器的长度最长可达 99 小时 59 分 59 秒。计时器被格式化为字符串,如“hh:mm:ss”。

【问题讨论】:

  • 没有任何帮助。
  • 能否请您详细说明您的评论,因为目前还远没有建设性。

标签: java android timer countdowntimer


【解决方案1】:

请使用 RecyclerView 而不是 Listview。在这里可以查看和下载Countdown timers in a ListView的源码

activity_main.xml

<RelativeLayout android:layout_width=”match_parent”
android:layout_height=”match_parent”
xmlns:android=”http://schemas.android.com/apk/res/android”&gt;

<android.support.v7.widget.RecyclerView
android:id=”@+id/recycler_view”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:scrollbars=”vertical” />

</RelativeLayout>

adapter_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="15dp"
    android:padding="10dp"
    android:id="@+id/tv_timer"/>

</LinearLayout>

MainActivity.java

package com.androidsolutionworld.multipletimer;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.LinearLayout;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private ArrayList al_data = new ArrayList<>();
private Adapter obj_adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
    al_data.add("1234");
    al_data.add("1257");
    al_data.add("100");
    al_data.add("1547");
    al_data.add("200");
    al_data.add("500");
    al_data.add("2000");
    al_data.add("1000");

    obj_adapter = new Adapter(al_data);
    LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext(),LinearLayoutManager.VERTICAL,false);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(obj_adapter);
}
}

自定义适配器:

import android.os.CountDownTimer;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;

public class Adapter extends RecyclerView.Adapter{

private ArrayList al_data;

public class MyViewHolder extends RecyclerView.ViewHolder{
    public TextView tv_timer;
    CountDownTimer timer;

    public MyViewHolder (View view){
        super(view);
        tv_timer = (TextView)view.findViewById(R.id.tv_timer);

    }


}

public Adapter(ArrayList al_data) {
    this.al_data = al_data;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_layout,parent,false);


    return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {

    holder.tv_timer.setText(al_data.get(position));

    if (holder.timer != null) {
        holder.timer.cancel();
    }
     long timer = Long.parseLong(al_data.get(position));

    timer = timer*1000;

    holder.timer = new CountDownTimer(timer, 1000) {
        public void onTick(long millisUntilFinished) {
          holder.tv_timer.setText("" + millisUntilFinished/1000 + " Sec");
        }

        public void onFinish() {
            holder.tv_timer.setText("00:00:00");
        }
    }.start();


}

@Override
public int getItemCount() {
    return al_data.size();
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 2012-11-08
    • 2013-12-14
    相关资源
    最近更新 更多