我正在Android Studio中编写Java,目前正在制作一个日历应用程序,可以保存、显示和编辑事件。我必须添加一些视图,如MonthView WeekView和DailyView。起初,我将其设置为所有活动,当我想返回时,按下后退按钮几乎完成了我的工作。由于一些可能性,我原来只保留了一个活动,并用一些方法做同样的工作,而不是制作活动。我有一个导航抽屉,所以在onItemNavigationClick中用户可以选择想要的视图。所以,我的问题是我找不到执行前一个方法的方法,就像如果我转到周视图并按下后退按钮,回到月视图或日视图。
因为我认为backpressed是回到一个活动,所以我认为它不能帮助我。我会感激任何帮助。
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.menuSchedule:
setAllEvents();
drawerLayout.closeDrawer(GravityCompat.START);
break;
case R.id.daysView:
setDaily();
drawerLayout.closeDrawer(GravityCompat.START);
break;
case R.id.weekView:
setWeek();
drawerLayout.closeDrawer(GravityCompat.START);
break;
case R.id.monthView:
setMonthView();
drawerLayout.closeDrawer(GravityCompat.START);
break;
case R.id.refreshItem:
finish();
startActivity(getIntent());
break;
case R.id.syncItem:
break;
default:
onNavigationItemSelected(item);
}
return true;
}
- 我的set方法如何工作的示例:**
private void setMonthView() {
monthYearText.setText(monthYearFromDate(CalendarUtils.selectedDate));
ArrayList<LocalDate> daysInMonth = daysInMonthArray();
CalendarAdapter calendarAdapter = new CalendarAdapter(daysInMonth, this, getApplicationContext());
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 7);
calendarRecyclerView.setLayoutManager(layoutManager);
calendarRecyclerView.setAdapter(calendarAdapter);
ViewGroup.LayoutParams params = calendarRecyclerView.getLayoutParams();
params.height=1500;
calendarRecyclerView.setLayoutParams(params);
monthListView.setVisibility(View.GONE);
monthYearText.setVisibility(View.VISIBLE);
daysOfWeekDaily.setVisibility(View.GONE);
daysOfWeek.setVisibility(View.VISIBLE);
prevMonth.setVisibility(View.VISIBLE);
nextMonth.setVisibility(View.VISIBLE);
calendarRecyclerView.setVisibility(View.VISIBLE);
nestedScrollView.setVisibility(View.VISIBLE);
}
- 解决方案**
感谢大卫韦瑟,代码的工作方式,我想,这里的源代码我的问题:
public void onMyBackPressed() {
// Pop current view type off the stack
stack.removeFirst();
// Check the previous view type
String previousViewType = stack.peekFirst();
if (previousViewType == null) {
// Nothing to go back to, so finish this Activity
super.onBackPressed();
return;
}
if (previousViewType.equals("daily")) {
setDaily();
} else if (previousViewType.equals("week")) {
setWeek();
} else if (previousViewType.equals("all"))
{
setAllEvents();
}else if (previousViewType.equals("month"))
{
setMonthView();
}
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.menuSchedule:
setAllEvents();
stack.addFirst("all");
drawerLayout.closeDrawer(GravityCompat.START);
break;
case R.id.daysView:
setDaily();
stack.addFirst("daily");
drawerLayout.closeDrawer(GravityCompat.START);
break;
case R.id.weekView:
setWeek();
stack.addFirst("week");
drawerLayout.closeDrawer(GravityCompat.START);
break;
case R.id.monthView:
setMonthView();
stack.addFirst("month");
drawerLayout.closeDrawer(GravityCompat.START);
break;
case R.id.refreshItem:
finish();
startActivity(getIntent());
break;
case R.id.syncItem:
break;
default:
onNavigationItemSelected(item);
}
return true;
}
1条答案
按热度按时间h5qlskok1#
您可以通过构建一个堆栈来表示您正在显示的视图类型来实现这一点。无论何时用户从导航抽屉式菜单中选择某项内容,调用方法来显示视图并将一些内容压入堆栈(
String
或Integer
常量),它表示您正在显示的视图类型。然后重写onBackPressed()
,以便取代默认行为(也就是完成当前的Activity
并返回到前一个Activity
),你这样做:super.onBackPressed()
调用默认行为,这将使用户返回到HOME屏幕或其他屏幕代码示例:
在
switch
语句中,每个不同视图类型如下所示:现在覆盖
onBackPressed()
的行为: