今天遇到個(gè)問(wèn)題,上面是一個(gè)搜索框,下面是內(nèi)容列表,搜索框要和列表一起滾動(dòng),因?yàn)椴季忠恍┨厥庑Ч荒馨阉阉骺蚍诺絉eyclerView外面,只能把搜索框以多布局方式放到RecyclerView中。
然后我把搜索框放到了RecyclerView中,布局展示沒(méi)有問(wèn)題,但是當(dāng)輸入框獲取焦點(diǎn)或者內(nèi)容清空等,導(dǎo)致整個(gè)RecyclerView自己上下移動(dòng)。最后在網(wǎng)上找到可行方法
代碼來(lái)源https://blog.csdn.net/zhang106209/article/details/124198068
/**
* 為了解決recycle人View上添加的headView 中的EditText等控件獲取了焦點(diǎn)導(dǎo)致RecyclerView 莫名滾動(dòng)
*/
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>
* 當(dāng)組里的某個(gè)子視圖需要被定位在屏幕的某個(gè)矩形范圍時(shí),調(diào)用此方法。重載此方法的ViewGroup可確認(rèn)以下幾點(diǎn):
* <p>
* * 子項(xiàng)目將是組里的直系子項(xiàng)
* * 矩形將在子項(xiàng)目的坐標(biāo)體系中
* 重載此方法的ViewGroup應(yīng)該支持以下幾點(diǎn):
* * 若矩形已經(jīng)是可見(jiàn)的,則沒(méi)有東西會(huì)改變
* * 為使矩形區(qū)域全部可見(jiàn),視圖將可以被滾動(dòng)顯示
* 參數(shù)
* child 發(fā)出請(qǐng)求的子視圖
* rectangle 子項(xiàng)目坐標(biāo)系內(nèi)的矩形,即此子項(xiàng)目希望在屏幕上的定位
* immediate 設(shè)為true,則禁止動(dòng)畫和平滑移動(dòng)滾動(dòng)條
* <p>
* 返回值
* 進(jìn)行了滾動(dòng)操作的這個(gè)組(group),是否處理此操作。
*
* @param parent
* @param child
* @param rect
* @param immediate
* @return
*/
@Override
public boolean requestChildRectangleOnScreen(RecyclerView parent, View child, Rect rect, boolean immediate) {
//這里的child 是整個(gè)HeadView 而不是某個(gè)具體的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 是整個(gè)HeadView 而不是某個(gè)具體的editText
LogUtil.e("requestChildRectangleOnScreen( focusedChildVisible=)====> chlild==" + child.getId() + "parent==" + parent.getId());
return false;
}
}