i have an app Android TV with a calendar and can control by d-pad. It work well in android API 25 and when i update it to android API 26 it cannot work as normal. When calendar open first, it focus on current day and i click d-pad then it lost focus. I can't control by d-pad anymore.
Anybody know why please tell me reason and how to resolve it.
here my code:
my calendar adapter: public class RecyclerAdapter extends RecyclerView.Adapter implements 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);
}
}
}