我目前在底部导航中有3个片段。片段之一包含一个计时器。当您启动计时器,更改片段并返回到计时器片段时,就会出现问题。重置。
主要活动
public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadFragment(new TimerFragment());
BottomNavigationView navigation = findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(this);
}
private boolean loadFragment (Fragment fr) {
if (fr != null){
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragContainer,fr)
.commit();
return true;
}
return false;
}
public boolean onNavigationItemSelected (@NonNull MenuItem item) {
Fragment fr = null;
switch (item.getItemId()){
case R.id.navigation_home:
fr = new TimerFragment();
break;
case R.id.navigation_dashboard:
fr = new ListFragment();
break;
case R.id.navigation_notifications:
fr = new RemindFragment();
break;
}
return loadFragment(fr);
}
}
计时器片段
public class TimerFragment extends Fragment {
TextView tvTimer;
long startTime = 0;
Handler timerHandler = new Handler();
Runnable timerRunnable = new Runnable() {
@Override
public void run() {
long millis = System.currentTimeMillis() - startTime;
int seconds = (int)(millis/1000);
int minutes = seconds / 60;
seconds = seconds % 60;
tvTimer.setText(String.format("%d:%02d", minutes, seconds));
timerHandler.postDelayed(this,500);
}
};
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_timer, null);
tvTimer = (TextView)view.findViewById(R.id.tvTimer);
Button btn = (Button)view.findViewById(R.id.btn1);
btn.setText("Start");
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Button b = (Button) v;
if (b.getText().equals("Stop")){
timerHandler.removeCallbacks(timerRunnable);
b.setText("Start Monitoring");
}
else {
startTime = System.currentTimeMillis();
timerHandler.postDelayed(timerRunnable, 0);
b.setText("Stop");
Toast.makeText(getActivity(),"Monitoring started.",Toast.LENGTH_SHORT).show();
}
}
});
return view;
}
}
我试图做的一件事是add
而不是replace
的片段。它不会在更改片段时重置计时器,而是将片段彼此叠放在一起,看起来像一团混乱。返回TimerFragment将创建另一个计时器,现在有两个计时器。知道该怎么办吗?我该如何编码,以使片段发生变化时计时器不会重置?
我认为hide
和show
可以在这里工作,但是我不确定在有3个片段要切换的情况下如何应用。
答案 0 :(得分:0)
当选择导航时,也许会创建一个新的计时器片段 onNavigationItemSelected中的片段。那是我的猜测。
答案 1 :(得分:0)
我认为捉迷藏可以在这里工作,但我不确定如何申请 在我要切换三个片段的情况下。
您是对的,请按照以下示例将其应用于您的代码。
public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
private final Fragment mTimerFragment = new TimerFragment();
private final Fragment mListFragment = new ListFragment();
private final Fragment mReminderFragment = new RemindFragment();
private Fragment mActiveFragment = mTimerFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportFragmentManager().beginTransaction()
.add(R.id.fragContainer, mReminderFragment, "TimerFragment")
.hide(mReminderFragment)
.commit();
getSupportFragmentManager().beginTransaction()
.add(R.id.fragContainer, mListFragment, "ListFragment")
.hide(mListFragment)
.commit();
getSupportFragmentManager().beginTransaction()
.add(R.id.fragContainer, mTimerFragment, "ReminderFragment")
.commit();
BottomNavigationView navigation = findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(this);
}
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
getSupportFragmentManager().beginTransaction()
.hide(mActiveFragment)
.show(mTimerFragment)
.commit();
mActiveFragment = mTimerFragment;
return true;
case R.id.navigation_dashboard:
getSupportFragmentManager().beginTransaction()
.hide(mActiveFragment)
.show(mListFragment)
.commit();
mActiveFragment = mListFragment;
return true;
case R.id.navigation_notifications:
getSupportFragmentManager().beginTransaction()
.hide(mActiveFragment)
.show(mReminderFragment)
.commit();
mActiveFragment = mReminderFragment;
return true;
}
return false;
}
}