3

我正在使用協調器佈局和NestedScrollView在我的片段中用於摺疊工具欄動畫。我將視圖動態添加到我的嵌套滾動視圖容器。我已經在NestedScrollView中的視圖上實現了onClickListener,但任何視圖的第一次點擊總是被忽略。當我第二次點擊或超過它的作品。我的XML代碼如下:Android NestedScrollView的子視圖忽略第一次點擊

<?xml version="1.0" encoding="utf-8"?> 
<!-- 
~ Copyright (C) 2015 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. 
--> 

<android.support.design.widget.CoordinatorLayout 
    android:id="@+id/main_content" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true"> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/appbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:fitsSystemWindows="true"> 

    <android.support.design.widget.CollapsingToolbarLayout 
     android:id="@+id/collapsing_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true" 
     app:collapsedTitleTextAppearance="@style/toolBarTitleStyle" 
     app:contentScrim="@color/primary" 
     app:expandedTitleMarginBottom="@dimen/dimen_21dp" 
     app:expandedTitleMarginStart="@dimen/dimen_13dp" 
     app:expandedTitleTextAppearance="@style/ExpandedToolBarTextTheme" 
     app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

     <ImageView 
      android:id="@+id/backdrop" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:fitsSystemWindows="true" 
      android:scaleType="centerCrop" 
      android:src="@drawable/locality_bg" 
      app:layout_collapseMode="parallax" 
      app:layout_collapseParallaxMultiplier="0.2"/> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      app:layout_collapseMode="pin"/> 

    </android.support.design.widget.CollapsingToolbarLayout> 

</android.support.design.widget.AppBarLayout> 

<android.support.v4.widget.NestedScrollView 
    android:id="@+id/scroll_view" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

    <LinearLayout 
     android:id="@+id/base_view_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/black_20pc" 
     android:orientation="vertical"> 

    </LinearLayout> 

</android.support.v4.widget.NestedScrollView> 

<com.locon.housing.views.RobotoMediumButton 
    android:id="@+id/view_listings" 
    android:layout_width="match_parent" 
    android:layout_height="@dimen/dimen_56dp" 
    android:layout_gravity="bottom" 
    android:background="@color/vida_loca" 
    android:gravity="center" 
    android:text="@string/alerts_view_listings" 
    android:textColor="@color/white" 
    android:textSize="@dimen/fontsize_medium"/> 

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab_button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="@dimen/dimen_16dp" 
    android:clickable="true" 
    android:src="@drawable/ic_action_new"/> 

</android.support.design.widget.CoordinatorLayout> 

好像NestedScrollView被攔截的觸摸事件,而不是傳遞給孩子的第一次。 我該如何解決這個問題?

+0

您是否嘗試過擴展'AppBarLayoutBehavior'並覆蓋onInterceptTouchEvent並就不應觸發嵌套滾動的'target'意見'返回FALSE',所以他們可以消耗事件而不觸發嵌套滾動? –

+0

@NikolaDespotoski我將視圖動態地添加到NestedScrollView,所以我不知道它是否會工作。 我試圖添加一個自定義應用程序:layout_behavior,但它給了我一個例外:java.lang.RuntimeException:無法膨脹行爲子類。 你能舉一些你想說的話嗎? –

+3

明白了。該問題是由於Android支持庫(23.0.1)NestedScrollView中的錯誤。它可以在這裏找到:https://code.google.com/p/android/issues/detail?id=178041 這個問題很可能會在下一個版本中修復。直到那麼你可以看到這個帖子的解決方法:http://stackoverflow.com/a/32783524/1999153 –

回答

0

我有同樣的錯誤。升級後支持庫版本到27.0.1的bug已經消失。

參見:https://developer.android.com/topic/libraries/support-library/revisions.html#27-0-1

用戶滾動後,他們不能在RecyclerView單擊一個項目。 (AOSP發行66996774)

看到相同的問題:Android - the item inside RecyclerView can't be clicked after scroll

+2

一個解決方案的鏈接是受歡迎的,但請確保你的答案是有用的沒有它:[添加鏈接上下文] (//meta.stackexchange.com/a/8259),這樣你的同行用戶就會知道它是什麼以及它爲什麼在那裏,然後引用你鏈接的頁面中最相關的部分,以防目標頁面不可用。 [僅僅是一個鏈接的答案可能會被刪除。](// stackoverflow.com/help/deleted-answers) –

相關問題