我在代码中放入了很多注释,因此希望它能为您描述我的问题。如果没有,我将在这里简短地总结一下:
我有一个材料日历,如果用户单击任何日期,我会在其中显示一个下拉菜单。如果用户单击下拉菜单内的任何项目,我想更改该下拉菜单。但是,如果我更改显示onDateChanged的dropDown菜单,它将影响所有日期。您可以自己尝试代码,以更好地了解我的问题所在。我希望有人可以帮我解决这个问题!
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MaterialCalendarView calendarView = findViewById(R.id.calendarView);
final PopupMenu dropDownMenu = new PopupMenu(MainActivity.this,calendarView);
final PopupMenu dropDownMenu2 = new PopupMenu(MainActivity.this,calendarView);
calendarView.state().edit()
.setFirstDayOfWeek(DayOfWeek.of(Calendar.SUNDAY))
.setMinimumDate(CalendarDay.from(1950, 1, 1))
.setMaximumDate(CalendarDay.from(2100, 1, 1))
.setCalendarDisplayMode(CalendarMode.MONTHS)
.commit();
calendarView.setDateSelected(CalendarDay.today(), true);
calendarView.setOnDateChangedListener(new OnDateSelectedListener() {
@Override
public void onDateSelected(@NonNull MaterialCalendarView materialCalendarView, @NonNull CalendarDay calendarDay, boolean b) {
// Depending on which date is selected, I wanna display different elements in dropDownMenu
dropDownMenu.getMenu().add("Lunch break");
dropDownMenu.getMenu().add("Meeting with Sarah");
dropDownMenu.getMenu().add("Workout");
dropDownMenu.show();
// When user select any element in the dropdown, I would like to add that into a variable.
dropDownMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
// I want to use this for another dropDownMenu, so I add it inside that one.
dropDownMenu2.getMenu().add(item.toString());
dropDownMenu2.getMenu().add("Remove");
// Now that the item is added into the 2nd dropDown menu, I would like to display that dropDown menu
// if user click on the same date again, offer them to delete the selected item in order to display the
// other dropdown menu again if the user would like to change activity, maybe from "Lunch break" to "Workout",
// and here I'm stuck! I can't find out how to accomplish this.
// Another problem I have, that I've tried, is that if I change dropDown menu, it appears on all dates, so if I change
// dropDownMenu in "onDateChanged", the new dropDown will appear on every date. I would like the first dropDown menu to
// appear on every date, and if user select any item, the new dropDown menu will appear for the selected date only.
return true;
}
});
}
});
}
}
这是我的布局XML:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.prolificinteractive.materialcalendarview.MaterialCalendarView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/calendarView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:mcv_selectionColor="#67F"
app:mcv_showOtherDates="defaults"
app:mcv_dateTextAppearance="@dimen/calendar_text_size"
app:mcv_weekDayTextAppearance="@dimen/calendar_text_size"/>
这是我定义的样式:
<style name="textSizeCalendar" parent="@android:style/Widget.TextView">
<item name="android:textColor">@color/lowDarkfont</item>
<item name="android:textSize">@dimen/calendar_text_size</item>
<item name="android:clickable">true</item>
<item name="android:soundEffectsEnabled">false</item>
</style>