android - ListView 中的多个秒表计时器在删除/添加时重置
问题描述
我正在使用 ListView 制作一个多秒表 android 应用程序,我想从 ListView 添加/删除秒表,而不影响之前添加的秒表“倒计时”。但是,每次我添加/删除时,它都会将 ListView 中的前一个秒表活动重置为 0。下面是我的主要活动的代码:
public class MainActivity extends AppCompatActivity {
ListView watchList;
ArrayList<Stopwatch> stopWatches = new ArrayList<>();
StopwatchListAdapter sAdapter;
Stopwatch watch = new Stopwatch();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FloatingActionButton addB = (FloatingActionButton) findViewById(R.id.addWatch);
watchList = (ListView)findViewById(R.id.stopList);
sAdapter = new StopwatchListAdapter(this, R.layout.stop_watch, stopWatches);
watchList.setAdapter(sAdapter);
}
public void addItems(View v) {
sAdapter.add(watch);
}
}
And below is the code for my adapter:
public class StopwatchListAdapter extends ArrayAdapter<Stopwatch>{
private Context sContext;
private int sResource;
ArrayList<Stopwatch> stopwatches;
public StopwatchListAdapter(@NonNull Context context, int resource, @NonNull ArrayList<Stopwatch> objects) {
super(context, resource, objects);
this.sContext = context;
this.sResource = resource;
this.stopwatches = objects;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(sContext);
convertView = inflater.inflate(sResource, parent, false);
Stopwatch watch = new Stopwatch();
Chronometer chronometer = convertView.findViewById(R.id.chron);
watch.setChronometer(chronometer);
Button start = (Button)convertView.findViewById(R.id.start);
Button pause = (Button)convertView.findViewById(R.id.pause);
Button reset = (Button)convertView.findViewById(R.id.reset);
Button remove = (Button)convertView.findViewById(R.id.remove);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
watch.startChronometer(view);
}
});
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
watch.pauseChronometer(view);
}
});
reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
watch.resetChronometer(view);
}
});
remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
stopwatches.remove(position);
notifyDataSetChanged();
}
});
return convertView;
}
}
My stopwatch class is below:
public class Stopwatch {
private boolean running = false;
private Chronometer chronometer;
private long pauseOffset;
public void setChronometer(Chronometer chronometer) {
this.chronometer = chronometer;
}
public void startChronometer(View v) {
if(!running) {
chronometer.setBase(SystemClock.elapsedRealtime() - pauseOffset);
chronometer.start();
running = true;
}
}
public void pauseChronometer(View v) {
if(running) {
chronometer.stop();
pauseOffset = SystemClock.elapsedRealtime() - chronometer.getBase();
running = false;
}
}
public void resetChronometer(View v) {
chronometer.setBase(SystemClock.elapsedRealtime());
pauseOffset = 0;
chronometer.stop();
running = false;
}
}
解决方案
推荐阅读
- javascript - webpack splitChunks:如何将依赖项合并到命名块中?
- java - Java 库 Graphics2D 不将文本显示为正确的字体
- python - 最后一行带有 :_^5 的 print 函数是什么意思?
- javascript - 如何将编辑器添加到特定选项卡的整个工作表保护?
- qt - QT QVulkanWindow 覆盖小部件
- android - 如何在命令行中为服务应用程序从 C++ 源代码构建 apk 包
- git - 默认情况下 git 跟踪分支
- python - 使用 Pyqt Print 支持但出现错误“对象不可调用”
- java - 为什么我不能从泛型参数访问属性?
- angular - 如何验证 Angular 2 表单