我有计时器只有当我们现在参加那个特定的活动时才有效,如果我想切换到其他活动,保持计时器连续工作,这里必须要做的是我的代码,直到现在我已经实现了。 在此先感谢!!!
package com.example.home.outdoor;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.SystemClock;
import android.sax.StartElementListener;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Chronometer;
import android.widget.ImageView;
import android.widget.TextView;
import java.net.URL;
import java.text.NumberFormat;
import static android.support.v7.recyclerview.R.attr.layoutManager;
public class Adap1 extends RecyclerView.Adapter<Adap1.ViewHolder>
{
String a121[] = new String[10];
private static final String TAG = "CustomAdap";
private String[] titles = {"Distance",
"Duration"};
Context avd;
private long timeWhenStopped = 0;
private boolean stopClicked;
private int ads[] = {0, 1};
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
RecyclerView.Adapter adapter;
FloatingActionButton a1s, a2s;
Chronometer chronometer;
public static final int WeatherVe = 0;
public static final int ChronoVe = 1;
public Adap1(String a121[]) {
this.a121 = a121;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View v) {
super(v);
}
}
public class WeatherVe extends ViewHolder {
public TextView itemTitle;
ImageView acv;
public TextView itemg;
public WeatherVe(View v) {
super(v);
itemTitle = (TextView) v.findViewById(R.id.ass);
itemg = (TextView) v.findViewById(R.id.ass1);
}
}
public class ChronoVe extends ViewHolder {
public ChronoVe(View v) {
super(v);
a1s = (FloatingActionButton) v.findViewById(R.id.as);
a2s = (FloatingActionButton) v.findViewById(R.id.as1);
chronometer = (Chronometer) v.findViewById(R.id.ch);
chronometer.setText("00:10");
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v;
if (viewType == WeatherVe) {
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_lay12, parent, false);
return new WeatherVe(v);
} else {
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card123, parent, false);
return new ChronoVe(v);
}
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
if (holder.getItemViewType() == WeatherVe) {
WeatherVe hsolder = (WeatherVe) holder;
// CustomAdap.WeatherViewHolder holder = (CustomAdap.WeatherViewHolder) viewHolder;
hsolder.itemTitle.setText(a121[position]);
hsolder.itemg.setText(a121[position + 1]);
} else {
System.out.println("here dudeeeeeeeeeeeee");
// CustomAdap.ScoreViewHolder holder = (CustomAdap.ScoreViewHolder) viewHolder;
ChronoVe hags = (ChronoVe) holder;
// new chronom().execute();
a1s.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
a2s.setVisibility(View.VISIBLE);
startButtonClick(v);
}
});
a2s.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopButtonClick(v);
}
});
}
}
@Override
public int getItemCount() {
return titles.length;
}
public int getItemViewType(int position)
{
return ads[position];
}
private void showElapsedTime() {
long elapsedMillis = SystemClock.elapsedRealtime() - chronometer.getBase();
// Toast.makeText(ChronoExample.this, "Elapsed milliseconds: " + elapsedMillis,
// Toast.LENGTH_SHORT).show();
}
// the method for when we press the 'start' button
public void startButtonClick(View v) {
int stoppedMilliseconds = 0;
String chronoText = chronometer.getText().toString();
String array[] = chronoText.split(":");
if (array.length == 2) {
stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 1000
+ Integer.parseInt(array[1]) * 1000;
} else if (array.length == 3) {
stoppedMilliseconds = Integer.parseInt(array[0]) * 60 * 60 * 1000
+ Integer.parseInt(array[1]) * 60 * 1000
+ Integer.parseInt(array[2]) * 1000;
}
chronometer.setBase(SystemClock.elapsedRealtime() - stoppedMilliseconds);
chronometer.start();
}
// the method for when we press the 'stop' button
public void stopButtonClick(View v) {
if (!stopClicked) {
chronometer.stop();
showElapsedTime();
}
}
}
答案 0 :(得分:0)
避免重新加载UI
解决方案
private View rootView;// Cache view
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
Log.i(TAG, "onCreateView");
if (rootView == null)
{
rootView = inflater.inflate(R.layout.fragment_1, null);
}
// Cached rootView need to determine whether the parent has been added, if there is parent needs to be deleted from parent, or else will happen this rootview has a parent error.
ViewGroup parent = (ViewGroup) rootView.getParent();
if (parent != null)
{
parent.removeView(rootView);
}
return rootView;
}