今天遇到個問題,上面是一個搜索框,下面是內容列表,搜索框要和列表一起滾動,因為布局要一些特殊效果,不能把搜索框放到ReyclerView外面,只能把搜索框以多布局方式放到RecyclerView中。
然后我把搜索框放到了RecyclerView中,布局展示沒有問題,但是當輸入框獲取焦點或者內容清空等,導致整個RecyclerView自己上下移動。最后在網上找到可行方法
代碼來源https://blog.csdn.net/zhang106209/article/details/124198068
/**
* 為了解決recycle人View上添加的headView 中的EditText等控件獲取了焦點導致RecyclerView 莫名滾動
*/
class FoucsLinearLayoutManager extends LinearLayoutManager {
public FoucsLinearLayoutManager(Context context) {
super(context);
}
public FoucsLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
public FoucsLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
/**
* public boolean requestChildRectangleOnScreen (View child, Rect rectangle, boolean immediate)
* <p>
* 當組里的某個子視圖需要被定位在屏幕的某個矩形范圍時,調用此方法。重載此方法的ViewGroup可確認以下幾點:
* <p>
* * 子項目將是組里的直系子項
* * 矩形將在子項目的坐標體系中
* 重載此方法的ViewGroup應該支持以下幾點:
* * 若矩形已經是可見的,則沒有東西會改變
* * 為使矩形區域全部可見,視圖將可以被滾動顯示
* 參數
* child 發出請求的子視圖
* rectangle 子項目坐標系內的矩形,即此子項目希望在屏幕上的定位
* immediate 設為true,則禁止動畫和平滑移動滾動條
* <p>
* 返回值
* 進行了滾動操作的這個組(group),是否處理此操作。
*
* @param parent
* @param child
* @param rect
* @param immediate
* @return
*/
@Override
public boolean requestChildRectangleOnScreen(RecyclerView parent, View child, Rect rect, boolean immediate) {
//這里的child 是整個HeadView 而不是某個具體的editText
LogUtil.e("requestChildRectangleOnScreen()====> chlild==" + child.getId() + "parent==" + parent.getId());
return false;
}
@Override
public boolean requestChildRectangleOnScreen(RecyclerView parent, View child, Rect rect, boolean immediate, boolean focusedChildVisible) {
//這里的child 是整個HeadView 而不是某個具體的editText
LogUtil.e("requestChildRectangleOnScreen( focusedChildVisible=)====> chlild==" + child.getId() + "parent==" + parent.getId());
return false;
}
}