android组件GridLayoutManager的onFocusSearchFailed无法控制焦点到自己的View上?

用的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方法。。。。

阅读 1.1k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题