一、關(guān)于NestedScrollView + RecyclerView頁面載入時總是滑動到最底部
項目中遇到頁面載入時總是滑動到最底部,原因是焦點在頁面底部;
方法一:解決方法就是在根布局設(shè)置android:descendantFocusability="blocksDescendants" ;
android:descendantFocusability 有三種值:
beforeDescendants:viewgroup會優(yōu)先其子類控件而獲取到焦點
afterDescendants:viewgroup只有當其子類控件不需要獲取焦點時才獲取焦點
blocksDescendants:viewgroup會覆蓋子類控件而直接獲得焦點
這種方法,會造成頁面中Editext焦點被搶導致無法輸入,需要用到第二種方法。
方法二:對于有Editext的頁面需要在根布局使用 :
android:focusable="true"
android:focusableInTouchMode="true";
二、解決NestedScrollView嵌套RecycleView顯示一行的bug
scrollview嵌套recyclerview不能完全顯示的幾種辦法:
1.http://www.cnblogs.com/woaixingxing/p/6098726.html
2.http://m.itdecent.cn/p/3815d36fd371?nomobile=yes
android:descendantFocusability="blocksDescendants"
該屬性是當一個為view獲取焦點時,定義viewGroup和其子控件兩者之間的關(guān)系。
屬性的值有三種:
beforeDescendants:viewgroup會優(yōu)先其子類控件而獲取到焦點
afterDescendants:viewgroup只有當其子類控件不需要獲取焦點時才獲取焦點
blocksDescendants:viewgroup會覆蓋子類控件而直接獲得焦點
三、解決ScrollView嵌套RecyclerView(橫向)或ListView(橫向)時,橫向滑動不順暢的問題。
/**
* 解決ScrollView與RecyclerView橫向滾動時的事件沖突
*/
public class ScrollRecyclerView extends RecyclerView {
public ScrollRecyclerView(Context context) {
super(context);
}
public ScrollRecyclerView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public ScrollRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
private float lastX, lastY;
@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
boolean intercept = super.onInterceptTouchEvent(e);
switch (e.getAction()) {
case MotionEvent.ACTION_DOWN:
lastX = e.getX();
lastY = e.getY();
break;
case MotionEvent.ACTION_MOVE:
// 只要橫向大于豎向,就攔截掉事件。
// 部分機型點擊事件(slopx==slopy==0),會觸發(fā)MOVE事件。
// 所以要加判斷(slopX > 0 || sloy > 0)
float slopX = Math.abs(e.getX() - lastX);
float slopY = Math.abs(e.getY() - lastY);
// Log.log("slopX=" + slopX + ", slopY=" + slopY);
if((slopX > 0 || sloy > 0) && slopX >= slopY){
requestDisallowInterceptTouchEvent(true);
intercept = true;
}
break;
case MotionEvent.ACTION_UP:
intercept = false;
break;
}
// Log.log("intercept"+e.getAction()+"=" + intercept);
return intercept;
}
}