ScrollView嵌套RecycleView相關(guān)問題

一、關(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;
    }

}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,326評論 25 708
  • 原文鏈接:https://github.com/opendigg/awesome-github-android-u...
    IM魂影閱讀 33,172評論 6 472
  • 最近做了一個Android UI相關(guān)開源項目庫匯總,里面集合了OpenDigg 上的優(yōu)質(zhì)的Android開源項目庫...
    OpenDigg閱讀 17,628評論 6 222
  • 如果有一天我死了,希望記憶里有我的人都像做了夢一樣,夢醒了,只有我不見了,一切依舊平靜。 ...
    忽而半夏aw閱讀 83評論 0 0

友情鏈接更多精彩內(nèi)容