我有一个带有日历的应用程序 Android TV,可以通过 d-pad 进行控制。它在 android API 25 中运行良好,当我将其更新到 android API 26 时,它无法正常工作。当日历首先打开时,它专注于当天,我点击 d-pad 然后它失去了焦点。我不能再用 d-pad 控制了。
任何人都知道为什么请告诉我原因以及如何解决它。
这是我的代码:
我的日历适配器:公共类 RecyclerAdapter 扩展 RecyclerView.Adapter 实现 View.OnClickListener {
private static final int DAY_OFFSET = 1;
private final String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
private final int[] daysOfMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
String date_month_year;
private Activity activity;
private List<String> list;
private int daysInMonth;
private int currentDayOfMonth;
private int currentWeekDay;
private CalendarAdapter.IChangeMonth iChangeMonth;
private int currentMonth;
private Calendar calendar;
public RecyclerAdapter(Context context, int textViewResourceId, int month, int year, CalendarAdapter.IChangeMonth iChangeMonth) {
super();
this.activity = (Activity) context;
this.list = new ArrayList<String>();
calendar = Calendar.getInstance();
setCurrentDayOfMonth(calendar.get(Calendar.DAY_OF_MONTH));
setCurrentWeekDay(calendar.get(Calendar.DAY_OF_WEEK));
this.iChangeMonth = iChangeMonth;
// Print Month
printMonth(month, year);
}
private String getMonthAsString(int i) {
return months[i];
}
private int getNumberOfDaysOfMonth(int i) {
return daysOfMonth[i];
}
private void printMonth(int mm, int yy) {
int trailingSpaces = -1;
int daysInPrevMonth = 0;
int prevMonth = 0;
int prevYear = 0;
int nextMonth = 0;
int nextYear = 0;
currentMonth = mm - 1;
daysInMonth = getNumberOfDaysOfMonth(currentMonth);
// Gregorian GridCalendar : MINUS 1, set to FIRST OF MONTH
GregorianCalendar cal = new GregorianCalendar(yy, currentMonth, 1);
if (currentMonth == 11) {
prevMonth = currentMonth - 1;
daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
nextMonth = 0;
prevYear = yy;
nextYear = yy + 1;
} else if (currentMonth == 0) {
prevMonth = 11;
prevYear = yy - 1;
nextYear = yy;
daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
nextMonth = 1;
} else {
prevMonth = currentMonth - 1;
nextMonth = currentMonth + 1;
nextYear = yy;
prevYear = yy;
daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
}
int currentWeekDay = cal.get(Calendar.DAY_OF_WEEK) - 1;
trailingSpaces = currentWeekDay;
if (cal.isLeapYear(cal.get(Calendar.YEAR)) && mm == 1) {
++daysInMonth;
}
// Trailing Month days
for (int i = 0; i < trailingSpaces; i++) {
list.add(String.valueOf((daysInPrevMonth - trailingSpaces + DAY_OFFSET) + i) + "-GREY" + "-" + getMonthAsString(prevMonth) + "-" + prevYear + "-" + (prevMonth + 1));
}
// Current Month Days
for (int i = 1; i <= daysInMonth; i++) {
if (i == getCurrentDayOfMonth())
list.add(String.valueOf(i) + "-DARKRED" + "-" + getMonthAsString(currentMonth) + "-" + yy + "-" + (currentMonth + 1));
else
list.add(String.valueOf(i) + "-WHITE" + "-" + getMonthAsString(currentMonth) + "-" + yy + "-" + (currentMonth + 1));
}
// Leading Month days
for (int i = 0; i < list.size() % 7; i++) {
list.add(String.valueOf(i + 1) + "-GREY" + "-" + getMonthAsString(nextMonth) + "-" + nextYear + "-" + (nextMonth + 1));
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(activity).inflate(R.layout.calendar_item, parent, false));
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
final String[] day_color = list.get(position).split("-");
String theDay = day_color[0];
String theYear = day_color[3];
String monthNumber = day_color[4];
// Set the Day GridCell
holder.btnGridCell.setText(theDay);
holder.btnGridCell.setTag(theDay + "/" + monthNumber + "/" + theYear);
if (day_color[1].equals("GREY")) {
holder.btnGridCell.setTextColor(0xffe95451);
}
if (day_color[1].equals("DARKRED") && currentMonth == calendar.get(Calendar.MONTH)) {
holder.btnGridCell.requestFocus();
holder.btnGridCell.setTextColor(activity.getResources().getColor(R.color.current_day));
} else if (day_color[1].equals("WHITE") && holder.btnGridCell.getText().equals("1") && currentMonth != calendar.get(Calendar.MONTH)) {
holder.btnGridCell.requestFocus();
}
holder.btnGridCell.setOnClickListener(this);
holder.itemView.setOnClickListener(this);
holder.btnGridCell.setOnLongClickListener(v -> {
if ((position % 7) == 0) {
iChangeMonth.changePrevMonth();
}
if ((position % 7) == 6) {
iChangeMonth.changeNextMonth();
}
return false;
});
holder.btnGridCell.setOnKeyListener((v, keyCode, event) -> {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
case KeyEvent.KEYCODE_DPAD_CENTER:
date_month_year = (String) v.getTag();
try {
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
Date date = formatter.parse(date_month_year);
iChangeMonth.choseDate(date.getTime());
} catch (ParseException e) {
e.printStackTrace();
Toast.makeText(activity, e.getMessage(), Toast.LENGTH_SHORT).show();
}
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
if ((position % 7) == 0) {
iChangeMonth.changePrevMonth();
}
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
if ((position % 7) == 6) {
iChangeMonth.changeNextMonth();
}
break;
}
return false;
}
if (event.getAction() == KeyEvent.ACTION_UP) {
Log.e("eee", "action up: " + holder.getAdapterPosition());
return false;
}
return false;
});
}
@Override
public int getItemCount() {
return list.size();
}
@Override
public void onClick(View view) {
date_month_year = (String) view.getTag();
try {
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
Date date = formatter.parse(date_month_year);
iChangeMonth.choseDate(date.getTime());
} catch (ParseException e) {
e.printStackTrace();
Toast.makeText(activity, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
public int getCurrentDayOfMonth() {
return currentDayOfMonth;
}
private void setCurrentDayOfMonth(int currentDayOfMonth) {
this.currentDayOfMonth = currentDayOfMonth;
}
public int getCurrentWeekDay() {
return currentWeekDay;
}
public void setCurrentWeekDay(int currentWeekDay) {
this.currentWeekDay = currentWeekDay;
}
public class ViewHolder extends RecyclerView.ViewHolder {
private Button btnGridCell;
private View itemView;
public ViewHolder(View itemView) {
super(itemView);
this.itemView = itemView;
btnGridCell = (Button) itemView.findViewById(R.id.calendar_day_gridcell);
}
}
}