解决方法一:
在RecyclerView中设置android:descendantFocusability属性
android:descendantFocusability="blocksDescendants"
属性介绍
1、afterDescendants:子控件优先获得焦点,当没有任何一个子控件获取到焦点时(即子控件不需要获取焦点时),父控件才会获取到焦点; 2、beforeDescendants :父控件会优先于子控件而获取到焦点; 3、blockDescendants:父控件会阻塞子控件获取焦点,即父控件会覆盖子类控件而直接获得焦点(只有父控件获得焦点);
解决方法二:
自定义layoutManager
public class FocusLinearLayoutManager extends LinearLayoutManager {
public FocusLinearLayoutManager(Context context) {
super(context);
}
public FocusLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
public FocusLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public boolean requestChildRectangleOnScreen(RecyclerView parent, View child, Rect rect, boolean immediate) {
LogUtils.e("requestChildRectangleOnScreen()====> chlild==", child.getId() + "parent==" + parent.getId());
return false;
}
@Override
public boolean requestChildRectangleOnScreen(RecyclerView parent, View child, Rect rect, boolean immediate, boolean focusedChildVisible) {
LogUtils.e("requestChildRectangleOnScreen( focusedChildVisible=)====> chlild==", child.getId() + "parent==" + parent.getId());
return false;
}
}
参考:
https://www.jianshu.com/p/827fd1195cc6 https://blog.csdn.net/okg0111/article/details/79886421
|