用的recyclerview版本是1.2.1。想通过重写GridLayoutManager这个方法,控制跳出数据列表的时候,聚焦到我自定义的一个控件上。但是发觉无效。
public View onFocusSearchFailed(View focused, int focusDirection, RecyclerView.Recycler recycler, RecyclerView.State state){
View nextView = null;
//TODO 省略.....通过一定的逻辑,给newView赋值
if (nextView != null) {
return nextView;
}
return super.onFocusSearchFailed(focused, focusDirection, recycler, state);
}
大致逻辑是这样,但是发现在官方源码里,会对返回的View,做一个isPreferredNextFocus
的判断,这个判断里,首先会判断return
的对象,是不是null
,或者和当前的对象一样,也就是说,return null
或者return focused
,都会认为无效,会继续执行默认跳转逻辑
if (next == null || next == this || next == focused) {
return false;
}
如果return
的是其他view
,则会判断view
的祖先是否在当前GridLayoutManager
的数据域中,如果否,就认为不是合适的对象,就不会响应用户的控制。
public View findContainingItemView(@NonNull View view) {
ViewParent parent = view.getParent();
while (parent != null && parent != this && parent instanceof View) {
view = (View) parent;
parent = view.getParent();
}
return parent == this ? view : null;
}
我就麻了,究竟怎样才能让我返回的view
可以获得焦点?或者说,怎么让return null
起效?
目前来看,似乎只能是重写focusSearch
方法。。。。