I’ve been working on Android TV for more than half a year since last year, and I’ve encountered a variety of pitfalls, the most serious and most difficult to solve being the focus issue. After reflecting on the pain, I decided to share the various pits I had encountered in TV development so that more people would not climb the pits I had.

List focus issues

In TV development, VerticalGridView is Google’s RecyclerView specially adapted for Android TV. VerticalGridView solves the focus problem of the list very well, but if used improperly, it is easy to appear the focus disorder problem of item sub-items.

In THE TV development as far as possible is not applicable to the RecyclerView has not been optimized!!!!!

Pothole — The item focus cannot be retrieved

I want to go to the list screenPress up, down, left and right keys, item will be able to obtain the focus, to achieve the effect of magnificationBut what I actually did was look like this: pressing the up and down key turned me into scrolling up and down the list instead of zooming in on the item as I wanted.

mList.requestFocus(); View child = mList.getChildAt(0); if (child ! = null){ child.requestFocus(); }Copy the code

In the list, no matter how I tried to get the focus, the item subitem could not get the focus. As it turns out, this works perfectly by adding Android :focusable=”true” to the XML root of the item. Vomiting blood…

.Copy the code

Pit – Error in obtaining the total number of items from a list

In the VerticalGridView, if the number of items is displayed by sliding, an interesting phenomenon occurs when you get the total number of items in the list: no matter which method you call in the VerticalGridView, you can’t get the exact total number of items in the list. If you need to get the total number of items in the list, you can only get the total number by using the Adapter.

As shown in the figure below, the total number of items in my list is 16.



Log.d(TAG, "COUNT_1 ==> " + mList.getLayoutManager().getChildCount());
Log.d(TAG, "COUNT_2 ==> " + mList.getChildCount());
Log.d(TAG, "COUNT_3 ==> " + mAdapter.getItemCount());Copy the code



Of the three methods mentioned above, only the third can accurately obtain the exact number of items in the list. The reasons for this problem are:

  • VerticalGridView.getLayoutManager().getChildCount(); You can only get all currently visible items in the current list.
  • VerticalGridView.getChildCount(); Is the rewrite method of ViewGrounp, and in RecyclerView cache principle, RecyclerView will only show all the current visible views, so the effect of this method is consistent with the result of the first method.

Pothole – Manual request Item focus error

Sometimes, we need to give the specified item focus. If your list has 4 rows, but the current screen only shows 3 rows, then if the item that needs focus happens to be on line 4, then you can use the

mList.getLayoutManager().getChildAt(postion).requestFocus();Copy the code

If the specified item is given focus, a null pointer exception will occur, for the same reason that the list failed to get the number of items. The solution to this problem is to move the VerticalGridView to the item first, and then process the item focus request. The following code looks like this:

mList.scrollToPosition(14);
View v = mList.getLayoutManager().getChildAt(14);
if (v == null) {
  L.d(TAG, "null");
} else {
  v.requestFocus();
}Copy the code

Gothole – A problem caused by adding a LayoutManager to a VerticalGridView

If you use a VerticalGridView or a HorizontalGridView, you don’t need to add a LayoutManager yourself. If you add a LayoutManager manually, and you want the specified Item to get focus, it will cause the Item to lose focus. Then press up, down and left keys, focus and expected inconsistent.

As shown in the figure below, I requested the focus of the 14th item, and finally found that the focus of the 14th item failed to be obtained. After pressing the up, down, and left keys, the focus was inconsistent with the expectation.

Solution: Remove LayoutManager added by hand!!