2016-04-24 19 views
5

我有一個問題,似乎隻影響Android 4.x版本,也可能是設備特定的(即它不存在於我的Huawei [email protected]上,但確實存在在三星[email protected]上)。 我有一個ScrollView包含RelativeLayout有4 CardView s。現在在一些4.x設備上,當我嘗試從卡片開始滾動時,滾動事件根本不會發生。如果我觸摸卡之間的小填充或以上的第一個卡(而不是任意兩個之間的)和設備屏幕,我可以滾動內容。ViewPager中的ScrollView在Android 4.x上不滾動

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true"> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        xmlns:app="http://schemas.android.com/tools" 
        xmlns:card_view="http://schemas.android.com/apk/res-auto" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="vertical" 
        android:padding="@dimen/activity_horizontal_margin"> 

     <android.support.v7.widget.CardView 
      android:id="@+id/metricsContainerCard" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      app:cardElevation="2dp" 
      card_view:cardUseCompatPadding="true"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:padding="10dp"> 

       <LinearLayout 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"> 

        <ImageView 
         android:layout_width="20dp" 
         android:layout_height="20dp" 
         android:layout_gravity="center_vertical" 
         android:layout_marginRight="5dp" 
         android:src="@drawable/metrics"/> 

        <TextView 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="@string/metrics" 
         android:textSize="20sp"/> 
       </LinearLayout> 


       <View 
        android:layout_width="match_parent" 
        android:layout_height="1dp" 
        android:background="@color/projectCircleBackgroundShadow"/> 

      </LinearLayout> 

     </android.support.v7.widget.CardView> 

     <android.support.v7.widget.CardView 
      android:id="@+id/warningsContainerCard" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:layout_below="@id/metricsContainerCard" 
      app:cardElevation="2dp" 
      card_view:cardUseCompatPadding="true"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:padding="10dp"> 

       <LinearLayout 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"> 

        <ImageView 
         android:layout_width="20dp" 
         android:layout_height="20dp" 
         android:layout_gravity="center_vertical" 
         android:layout_marginRight="5dp" 
         android:src="@drawable/bug_color"/> 

        <TextView 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="@string/warnings" 
         android:textSize="20sp"/> 
       </LinearLayout> 


       <View 
        android:layout_width="match_parent" 
        android:layout_height="1dp" 
        android:background="@color/projectCircleBackgroundShadow"/> 

      </LinearLayout> 
     </android.support.v7.widget.CardView> 

     <android.support.v7.widget.CardView 
      android:id="@+id/topWarningsContainerCard" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/warningsContainerCard" 
      android:layout_marginTop="10dp" 
      app:cardElevation="2dp" 
      card_view:cardUseCompatPadding="true"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:padding="10dp"> 

       <LinearLayout 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"> 

        <ImageView 
         android:layout_width="20dp" 
         android:layout_height="20dp" 
         android:layout_gravity="center_vertical" 
         android:layout_marginRight="5dp" 
         android:src="@drawable/top_warnings"/> 

        <TextView 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="@string/topWarnings" 
         android:textSize="20sp"/> 
       </LinearLayout> 


       <View 
        android:layout_width="match_parent" 
        android:layout_height="1dp" 
        android:background="@color/projectCircleBackgroundShadow"/> 

      </LinearLayout> 
     </android.support.v7.widget.CardView> 

     <android.support.v7.widget.CardView 
      android:id="@+id/topCriticalItemsContainerCard" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/topWarningsContainerCard" 
      android:layout_marginTop="10dp" 
      app:cardElevation="2dp" 
      card_view:cardUseCompatPadding="true"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:padding="10dp"> 

       <LinearLayout 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"> 

        <ImageView 
         android:layout_width="20dp" 
         android:layout_height="20dp" 
         android:layout_gravity="center_vertical" 
         android:layout_marginRight="5dp" 
         android:src="@drawable/top_critical_items"/> 

        <TextView 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="@string/topCriticalItems" 
         android:textSize="20sp"/> 

       </LinearLayout> 


       <View 
        android:layout_width="match_parent" 
        android:layout_height="1dp" 
        android:background="@color/projectCircleBackgroundShadow"/> 

      </LinearLayout> 
     </android.support.v7.widget.CardView> 
    </LinearLayout> 
</ScrollView> 

Android 5.x和6.x沒有任何問題。

編輯

看來,問題並不僅僅涉及到CardView,因爲我還有一個佈局,而且還當我通過自定義視圖中拖動屏幕甚至不滾動:

<ScrollView 
    android:id="@+id/scrollView" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    tools:context=".projects.details.ProjectDetailsActivity_"> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        xmlns:tools="http://schemas.android.com/tools" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="vertical" 
        android:padding="@dimen/activity_horizontal_margin"> 


     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 

      <TextView 
       android:id="@+id/projectName" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="16dp" 
       android:ellipsize="none" 
       android:gravity="center_horizontal" 
       android:text="Project name" 
       android:textSize="24sp"/> 

      <c.f.q.a.projects.details.components.widgets.ProjectDetailWidget 
       android:id="@+id/projectDetailWidget" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_gravity="center"/> 
     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/timelineWrapperLayout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

      <android.support.v7.widget.CardView 
       android:id="@+id/qualityTimelineCard" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="10dp" 
       app:cardElevation="2dp" 
       card_view:cardUseCompatPadding="true"> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="vertical" 
        android:padding="10dp"> 

        <LinearLayout 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content"> 

         <ImageView 
          android:id="@+id/flipQualityTimelineButton" 
          android:layout_width="30dp" 
          android:layout_height="30dp" 
          android:layout_marginRight="5dp" 
          android:src="@drawable/flip_to_cost"/> 

         <TextView 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_gravity="center_vertical" 
          android:ellipsize="end" 
          android:lines="1" 
          android:text="@string/project_timeline_title" 
          android:textSize="16dp"/> 
        </LinearLayout> 

        <ProgressBar 
         android:id="@+id/projectQualityTimelineProgressBar" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_gravity="center" 
         /> 

        <com.github.mikephil.charting.charts.LineChart 
         android:id="@+id/projectQualityTimeline" 
         android:layout_width="match_parent" 
         android:layout_height="200dp" 
         android:layout_marginTop="16dp" 
         android:tag="@string/project_chart_tag" 
         android:visibility="gone"/> 
       </LinearLayout> 
      </android.support.v7.widget.CardView> 

      <android.support.v7.widget.CardView 
       android:id="@+id/costTimelineCard" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="10dp" 
       android:visibility="gone" 
       app:cardElevation="2dp" 
       card_view:cardUseCompatPadding="true"> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="vertical" 
        android:padding="10dp"> 

        <LinearLayout 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content"> 

         <ImageView 
          android:id="@+id/flipCostTimelineButton" 
          android:layout_width="30dp" 
          android:layout_height="30dp" 
          android:layout_marginRight="5dp" 
          android:src="@drawable/flip_to_quality"/> 

         <TextView 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_gravity="center_vertical" 
          android:ellipsize="end" 
          android:lines="1" 
          android:text="@string/project_cost_timeline_title" 
          android:textSize="16dp"/> 
        </LinearLayout> 


        <ProgressBar 
         android:id="@+id/projectCostTimelineProgressBar" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_gravity="center" 
         /> 

        <com.github.mikephil.charting.charts.LineChart 
         android:id="@+id/projectCostTimeline" 
         android:layout_width="match_parent" 
         android:layout_height="200dp" 
         android:layout_marginTop="16dp" 
         android:tag="@string/project_chart_tag" 
         android:visibility="gone"/> 
       </LinearLayout> 
      </android.support.v7.widget.CardView> 

     </LinearLayout> 

     <android.support.v7.widget.CardView 
      android:id="@+id/vcsChangeCard" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:layout_weight="1" 
      app:cardElevation="2dp" 
      card_view:cardUseCompatPadding="true"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_gravity="left" 
       android:orientation="vertical" 
       android:padding="10dp"> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"> 

        <ImageView 
         android:id="@+id/expandVcsChangeButton" 
         android:layout_width="28dp" 
         android:layout_height="28dp" 
         android:layout_marginRight="5dp" 
         android:src="@drawable/expand_chart"/> 

        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_gravity="center_vertical" 
         android:ellipsize="end" 
         android:maxLines="2" 
         android:text="@string/project_vcschange_title" 
         android:textSize="16sp"/> 
       </LinearLayout> 

       <ProgressBar 
        android:id="@+id/vcsChangeProgressBar" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center"/> 

       <com.github.mikephil.charting.charts.BarChart 
        android:id="@+id/projectVcsChange" 
        android:layout_width="match_parent" 
        android:layout_height="175dp" 
        android:layout_marginTop="16dp" 
        android:tag="@string/project_chart_tag" 
        android:visibility="gone"/> 
      </LinearLayout> 
     </android.support.v7.widget.CardView> 

     <android.support.v7.widget.CardView 
      android:id="@+id/dtnosChangeCard" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:layout_weight="1" 
      app:cardElevation="2dp" 
      card_view:cardUseCompatPadding="true"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:gravity="right" 
       android:orientation="vertical" 
       android:padding="10dp"> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"> 

        <ImageView 
         android:id="@+id/expandDtnosChangeButton" 
         android:layout_width="28dp" 
         android:layout_height="28dp" 
         android:layout_marginRight="5dp" 
         android:src="@drawable/expand_chart"/> 

        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_gravity="center_vertical" 
         android:ellipsize="end" 
         android:maxLines="2" 
         android:text="@string/project_systemchange_title" 
         android:textSize="16sp"/> 
       </LinearLayout> 

       <ProgressBar 
        android:id="@+id/dtnosChangeProgressBar" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center"/> 

       <com.github.mikephil.charting.charts.BarChart 
        android:id="@+id/projectDtnosChange" 
        android:layout_width="match_parent" 
        android:layout_height="175dp" 
        android:layout_marginTop="16dp" 
        android:tag="@string/project_chart_tag" 
        android:visibility="gone"/> 
      </LinearLayout> 
     </android.support.v7.widget.CardView> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 

      <android.support.v7.widget.CardView 
       android:id="@+id/sensorNodeCard" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="10dp" 
       android:layout_weight="1" 
       app:cardElevation="2dp" 
       card_view:cardUseCompatPadding="true"> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:gravity="left" 
        android:orientation="vertical" 
        android:padding="10dp"> 

        <LinearLayout 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content"> 

         <ImageView 
          android:id="@+id/expandSensornodeButton" 
          android:layout_width="28dp" 
          android:layout_height="28dp" 
          android:layout_marginRight="5dp" 
          android:src="@drawable/expand_chart"/> 

         <TextView 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_gravity="center_vertical" 
          android:ellipsize="end" 
          android:maxLines="2" 
          android:text="@string/project_sensornode_title" 
          android:textSize="16sp"/> 
        </LinearLayout> 


        <ProgressBar 
         android:id="@+id/sensorNodeProgressBar" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_gravity="center"/> 

        <com.github.mikephil.charting.charts.RadarChart 
         android:id="@+id/sensorNodeChart" 
         android:layout_width="match_parent" 
         android:layout_height="175dp" 
         android:layout_marginTop="16dp" 
         android:tag="@string/project_chart_tag" 
         android:visibility="gone"/> 
       </LinearLayout> 
      </android.support.v7.widget.CardView> 

      <android.support.v7.widget.CardView 
       android:id="@+id/aggregateNodeCard" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="10dp" 
       android:layout_weight="1" 
       app:cardElevation="2dp" 
       card_view:cardUseCompatPadding="true"> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:gravity="right" 
        android:orientation="vertical" 
        android:padding="10dp"> 

        <LinearLayout 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content"> 

         <ImageView 
          android:id="@+id/expandAggregateButton" 
          android:layout_width="28dp" 
          android:layout_height="28dp" 
          android:layout_marginRight="5dp" 
          android:src="@drawable/expand_chart"/> 

         <TextView 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_gravity="center_vertical" 
          android:ellipsize="end" 
          android:maxLines="2" 
          android:text="@string/project_aggregatenode_title" 
          android:textSize="16sp"/> 

        </LinearLayout> 

        <ProgressBar 
         android:id="@+id/aggregateNodeProgressBar" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_gravity="center"/> 

        <com.github.mikephil.charting.charts.RadarChart 
         android:id="@+id/aggregateNodeChart" 
         android:layout_width="match_parent" 
         android:layout_height="175dp" 
         android:layout_marginTop="16dp" 
         android:tag="@string/project_chart_tag" 
         android:visibility="gone"/> 
       </LinearLayout> 
      </android.support.v7.widget.CardView> 
     </LinearLayout> 
    </LinearLayout> 
</ScrollView> 

我添加了完整的佈局,也在這裏是澄清的圖像(標誌着我,我可以拖動滾動區域):

scrollable areas

EDIT2

也許是同樣重要的是,這些佈局都是ViewPager內部片段。 ViewPager是否有可能阻止ScrollView(以及其中的任何其他視圖)獲得焦點/處理觸摸事件?我ViewPager的代碼是這樣的:

<android.support.v4.view.ViewPager 
    android:id="@+id/projectPager" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 

EDIT3

看來問題必須與ViewPager + (Nested)ScrollView(我試圖交換平原ScrollView這一說法,但都沒有成功)。我創建了我的片段的虛擬佈局,只有承載了父母NestedScrollView,一個LinearLayout和許多大TextView就像這樣:

<android.support.v4.widget.NestedScrollView 
android:id="@+id/scrollView" 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:card_view="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".projects.details.ProjectDetailsActivity_"> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:tools="http://schemas.android.com/tools" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:padding="@dimen/activity_horizontal_margin"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="16dp" 
     android:ellipsize="none" 
     android:gravity="center_horizontal" 
     android:text="Project name" 
     android:textSize="80sp"/> 

... <!-- more TextViews come here --> 

...也滾動不起作用!只有在屏幕的邊緣,如前所述。我不知道怎麼能這樣呢?

+0

從您的編輯看來,它與CardView無關 - 也許您可以重新編輯以省略CardView,並提供有關其餘設置的更多信息。 – ataulm

+0

您是否嘗試設置'CardView'的'android:clickable =「true」'和'android:focusable =「true」'屬性? – fillobotto

+0

是的,我試過了,沒有運氣。我編輯了我的文章更多的細節,也許這些可以幫助。 – Sleeper9

回答

2

解決!我有完全相同的問題。您可能正在使用PageTransformer ViewPager動畫。

禁用自定義頁面變壓器<的Android 4.1解決了這個問題:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 
     // there are problems with this on 4.0.3, probably also on 4.1 
     viewPager.setPageTransformer(true, new DepthPageTransformer()); 
    } 

您的文章和研究爲我節省了大量的時間,謝謝。

其他的解決辦法:

如果你喜歡你的動畫,你也可以嘗試改變你的DepthPageTransformer這樣:https://stackoverflow.com/a/28214802/1310343c

+0

我不敢相信......很多很多謝謝,這也解決了我的問題!:) – Sleeper9

-1

ScrollView必須match_parent height和滾動視圖(LinearLayout)的第一個孩子必須有wrap_content高度

+0

我以前嘗試過(我在錯誤的樣本中還留下了一個android:orientation =「vertical」),但沒有幫助。我目前正在使用Genymotion Google Nexus [email protected]。 – Sleeper9

+0

@ Sleeper9給你的XML佈局完整。 –

+0

我編輯了我的文章,添加了完整的佈局和圖像以供澄清! – Sleeper9

2

你是對的,2個滾動控制是相互競爭的。

改爲使用支持庫中的NestedScrollView。它旨在解決這類問題。

它有許多增強功能,它可以與其它可滾動控件,包括ViewPagerRecyclerView工作(只要您撥打.setNestedScrolling(true))和CoordinatorLayout

基於上述情況,我一直在使用這個新類,並且滾動內的滾動一直在完美運行。我們的產品與控件在ViewPager之內完全兼容,其中一些內嵌了RecyclerView列表。

+1

好吧,我用''交換了'',但仍然沒有任何改變。 :(我也叫'.setNestedScrolling(true)'。是否有需要在XML佈局中設置的東西? – Sleeper9

+0

好的抱歉,在這種情況下沒有幫助。所以任何人都可以編輯和改進 –

+1

但是這個問題一定在這裏......正如我寫的,我的'ScrollView'裏的其他視圖既不會響應tap,pinch等事件,所以ViewPager必須以某種方式消耗這個手勢。 .. – Sleeper9

1

對我來說,這個設置的工作:我有具有以下嵌套滾動型協調員佈局

<android.support.v4.widget.NestedScrollView 
    android:layout_marginTop="8dp" 
    android:fillViewport = "true" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <include layout="@layout/content_app_bar_search_patient_module"/> 

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

<include layout="@layout/content_app_bar_search_patient_module"/>具有具有片段的viewpager這再次包含嵌套滾動視圖

<android.support.v4.widget.NestedScrollView 

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.support.v4.widget.NestedScrollView>