處理Viewpager2嵌套滑動(dòng)問(wèn)題

使用NestedScrollableHost 將子view中需要滑動(dòng)的recyclerview等滑動(dòng)view 套一層
/*

  • Copyright 2019 The Android Open Source Project
  • Licensed under the Apache License, Version 2.0 (the "License");
  • you may not use this file except in compliance with the License.
  • You may obtain a copy of the License at
  •  http://www.apache.org/licenses/LICENSE-2.0
    
  • Unless required by applicable law or agreed to in writing, software
  • distributed under the License is distributed on an "AS IS" BASIS,
  • WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  • See the License for the specific language governing permissions and
  • limitations under the License.
    */

package androidx.viewpager2.integration.testapp

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.view.ViewConfiguration
import android.widget.FrameLayout
import androidx.viewpager2.widget.ViewPager2
import androidx.viewpager2.widget.ViewPager2.ORIENTATION_HORIZONTAL
import kotlin.math.absoluteValue
import kotlin.math.sign

/**

  • Layout to wrap a scrollable component inside a ViewPager2. Provided as a solution to the problem

  • where pages of ViewPager2 have nested scrollable elements that scroll in the same direction as

  • ViewPager2. The scrollable element needs to be the immediate and only child of this host layout.

  • This solution has limitations when using multiple levels of nested scrollable elements

  • (e.g. a horizontal RecyclerView in a vertical RecyclerView in a horizontal ViewPager2).
    */
    class NestedScrollableHost : FrameLayout {
    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)

    private var touchSlop = 0
    private var initialX = 0f
    private var initialY = 0f
    private val parentViewPager: ViewPager2?
    get() {
    var v: View? = parent as? View
    while (v != null && v !is ViewPager2) {
    v = v.parent as? View
    }
    return v as? ViewPager2
    }

    private val child: View? get() = if (childCount > 0) getChildAt(0) else null

    init {
    touchSlop = ViewConfiguration.get(context).scaledTouchSlop
    }

    private fun canChildScroll(orientation: Int, delta: Float): Boolean {
    val direction = -delta.sign.toInt()
    return when (orientation) {
    0 -> child?.canScrollHorizontally(direction) ?: false
    1 -> child?.canScrollVertically(direction) ?: false
    else -> throw IllegalArgumentException()
    }
    }

    override fun onInterceptTouchEvent(e: MotionEvent): Boolean {
    handleInterceptTouchEvent(e)
    return super.onInterceptTouchEvent(e)
    }

    private fun handleInterceptTouchEvent(e: MotionEvent) {
    val orientation = parentViewPager?.orientation ?: return

     // Early return if child can't scroll in same direction as parent
     if (!canChildScroll(orientation, -1f) && !canChildScroll(orientation, 1f)) {
         return
     }
    
     if (e.action == MotionEvent.ACTION_DOWN) {
         initialX = e.x
         initialY = e.y
         parent.requestDisallowInterceptTouchEvent(true)
     } else if (e.action == MotionEvent.ACTION_MOVE) {
         val dx = e.x - initialX
         val dy = e.y - initialY
         val isVpHorizontal = orientation == ORIENTATION_HORIZONTAL
    
         // assuming ViewPager2 touch-slop is 2x touch-slop of child
         val scaledDx = dx.absoluteValue * if (isVpHorizontal) .5f else 1f
         val scaledDy = dy.absoluteValue * if (isVpHorizontal) 1f else .5f
    
         if (scaledDx > touchSlop || scaledDy > touchSlop) {
             if (isVpHorizontal == (scaledDy > scaledDx)) {
                 // Gesture is perpendicular, allow all parents to intercept
                 parent.requestDisallowInterceptTouchEvent(false)
             } else {
                 // Gesture is parallel, query child if movement in that direction is possible
                 if (canChildScroll(orientation, if (isVpHorizontal) dx else dy)) {
                     // Child can scroll, disallow all parents to intercept
                     parent.requestDisallowInterceptTouchEvent(true)
                 } else {
                     // Child cannot scroll, allow all parents to intercept
                     parent.requestDisallowInterceptTouchEvent(false)
                 }
             }
         }
     }
    

    }
    }

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末刹前,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌悍赢,老刑警劉巖钝域,帶你破解...
    沈念sama閱讀 217,907評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件迄汛,死亡現(xiàn)場(chǎng)離奇詭異膳算,居然都是意外死亡兔魂,警方通過(guò)查閱死者的電腦和手機(jī)族跛,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門闰挡,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人庸蔼,你說(shuō)我怎么就攤上這事解总。” “怎么了姐仅?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,298評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵花枫,是天一觀的道長(zhǎng)刻盐。 經(jīng)常有香客問(wèn)我,道長(zhǎng)劳翰,這世上最難降的妖魔是什么敦锌? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,586評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮佳簸,結(jié)果婚禮上乙墙,老公的妹妹穿的比我還像新娘。我一直安慰自己生均,他們只是感情好听想,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,633評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著马胧,像睡著了一般汉买。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上佩脊,一...
    開(kāi)封第一講書(shū)人閱讀 51,488評(píng)論 1 302
  • 那天蛙粘,我揣著相機(jī)與錄音,去河邊找鬼威彰。 笑死出牧,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的歇盼。 我是一名探鬼主播舔痕,決...
    沈念sama閱讀 40,275評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼旺遮!你這毒婦竟也來(lái)了赵讯?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,176評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤耿眉,失蹤者是張志新(化名)和其女友劉穎边翼,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體鸣剪,經(jīng)...
    沈念sama閱讀 45,619評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡组底,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,819評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了筐骇。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片债鸡。...
    茶點(diǎn)故事閱讀 39,932評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖铛纬,靈堂內(nèi)的尸體忽然破棺而出厌均,到底是詐尸還是另有隱情,我是刑警寧澤告唆,帶...
    沈念sama閱讀 35,655評(píng)論 5 346
  • 正文 年R本政府宣布棺弊,位于F島的核電站晶密,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏模她。R本人自食惡果不足惜稻艰,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,265評(píng)論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望侈净。 院中可真熱鬧尊勿,春花似錦、人聲如沸畜侦。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,871評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)夏伊。三九已至摇展,卻和暖如春吻氧,著一層夾襖步出監(jiān)牢的瞬間溺忧,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,994評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工盯孙, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留鲁森,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,095評(píng)論 3 370
  • 正文 我出身青樓振惰,卻偏偏與公主長(zhǎng)得像歌溉,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子骑晶,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,884評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容